davidplappert
Posts: 3
Joined: Thu Jul 18, 2019 3:35 pm

Use Regular Light Switch To Run Script on Pi When Flipped

Thu Jul 18, 2019 3:43 pm

I see some people using regular light switches to power lights, but I am just looking for the switch to run a script each time it is flipped.

My Project:
I have a projector with an API that I can turn on and off. I would like to use a light switch to turn the projector on when the switch is flipped up, and then off when down. I see doing this with 2 different scripts (startup, shutdown). This way, the remotes stop getting lost!

My issue:
I see a lot of projects using light switches for lights, but I won't have any external power source for this switch. I need to know how to connect a light switch to my pi so that I can run my bash scripts. What parts/relays/wiring/etc ... is required? I have the light switch, and the pi connect to my network right now. I can issue the commands from the pi for the projector to turn on/off and it works. Just need help getting the light switch to talk to the pi.

My goal:
My goal is to run a cat5e/6 cable from the light switch in the classroom to my server room (100 ft) and then have that connect to my pi (via a relay or gpoi??) Basically I would use the 8 individual wires in the cat5e/6 to do what is needed and not use it as a network device. Then my pi would sit in my server room and just run the on/off scripts based on which way the light switch is flipped.

pcmanbob
Posts: 6443
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Use Regular Light Switch To Run Script on Pi When Flipped

Thu Jul 18, 2019 8:11 pm

Hi.

So before going any further is this just a stand alone light switch or does it control the actual mains lighting I the room in question.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

davidplappert
Posts: 3
Joined: Thu Jul 18, 2019 3:35 pm

Re: Use Regular Light Switch To Run Script on Pi When Flipped

Fri Jul 19, 2019 12:01 am

This is a stand-alone light switch that does not control anything else.

Thanks!

PhatFil
Posts: 1309
Joined: Thu Apr 13, 2017 3:55 pm
Location: Oxford UK

Re: Use Regular Light Switch To Run Script on Pi When Flipped

Fri Jul 19, 2019 1:40 am

In that case, Yes..

the difference between a button and a switch may be obvious But most examples and how-to's you will find online will use momentary contact buttons and the logic will be geared towards detecting short button presses rather than continual connections and disconnections.

'Latching Buttons' behave as switches push once to click on and maintain a connection and click off to disengage
So if looking for example code to copy remember the keyword 'Latching'

Python code to respond to a button might look like this

Code: Select all

while True: # Run forever
    if GPIO.input(10) == GPIO.HIGH:
        print("Button was pushed!")
used with a switch you would get a full screen of "Button was Pressed" in no time at all, not very useful even for debugging that ;)

so you may want to consider using a new variable called something like LastSwitchState and add to the code somthing like

Code: Select all

while True: # Run forever

	if GPIO.input(10) != LastSwitchState:

	    if GPIO.input(10) == GPIO.HIGH:
		{
        	print("Switch Switched ON!")
		LastSwitchState = HIGH
		}
	    else
	       {
		print("Switch Switched OFF!")
		LastSwitchState = LOW
		}
	    
just A way, not THE way... just knocked up in the forum post editor psuedo code to illustrate..

davidplappert
Posts: 3
Joined: Thu Jul 18, 2019 3:35 pm

Re: Use Regular Light Switch To Run Script on Pi When Flipped

Fri Jul 19, 2019 3:47 am

What does the wiring look like on the gpio? The light switch has positive, negative and neutral.

User avatar
rpdom
Posts: 14757
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Use Regular Light Switch To Run Script on Pi When Flipped

Fri Jul 19, 2019 6:10 am

davidplappert wrote:
Fri Jul 19, 2019 3:47 am
What does the wiring look like on the gpio? The light switch has positive, negative and neutral.
That doesn't sound like a normal switch. Switches typically just have two connections. Some have three, but they wouldn't be marked as "Positive, Negative and Neutral". Can you post a link to details about this switch, or even pictures of it?

PhatFil
Posts: 1309
Joined: Thu Apr 13, 2017 3:55 pm
Location: Oxford UK

Re: Use Regular Light Switch To Run Script on Pi When Flipped

Fri Jul 19, 2019 6:11 am

davidplappert wrote:
Fri Jul 19, 2019 3:47 am
What does the wiring look like on the gpio? The light switch has positive, negative and neutral.
Strange Positive and negative are generally dc voltage designations, while neutral is a designation usually used for an a/c mains terminal?

But i am about as far from an electrician as you can get, so ?????

To determine which 2 terminals to use, grab a multimeter with a continuity test and touch 2 terminals and if there is no buzz switch the switch.. if it buzzes you can use those terminals
if no meter available, batteries for a 3v supply and a led can be used to create a test circuit, but get a meter you will need one..

(i suspect + and - on your switch are what i would think of as NO and NC to enable the switch to be used as a selector? but i am guessing)

reference any Pi/button example for pi-switch connection, while code handling is a tad different the physical wiring isn't. which way round the switch is used in a DC circuit is irrelevant.

pcmanbob
Posts: 6443
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Use Regular Light Switch To Run Script on Pi When Flipped

Fri Jul 19, 2019 6:34 am

A typical light switch in the UK would have 3 terminals marked common, 1 way , 2 way.

you could connect it to your pi gpio like this

Image

in code you would treat it just like any other switch
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Return to “Automation, sensing and robotics”