I want my Raspberry Pi to know when I've turned on my light switch. My rpi is always running anyway for another purpose, and in the same room I've got wifi-controlled smart bulbs and a light switch controlling a completely unused outlet near my rpi. I would like my raspberry pi to see when I've turned on/off the light switch, and appropriately send the signal to my light bulbs to turn on/off.

I'm thinking of using an old usb wall charger and a usb cable, connecting the usb cable to a 5v relay from Sainsmart and using that to signal to my pi that the light switch is turned on. Would this work (safely), and if so, how would I connect that to one of the Pi's gpio pins?

  • Possible duplicate of How to measure mains frequency safely via GPIO – sir_ian Feb 20 '17 at 17:11
  • There are lots of very similar questions on this site. I recommend you look at some for advice. – sir_ian Feb 20 '17 at 17:13
  • Can you please clarify what you want to measure? It looks like you turn on a light connected to the normal AC mains supply and you want the Raspberry to detect that you turned the light on manually? In other words, what you really wnat to do is to detect the presence of AC mains with the Raspberry. Also, are you aware on how to safely deal with mains voltage? – kxtronic Feb 20 '17 at 21:49
  • @kxtronic I edited the post to be a little more clear. I'm hoping to avoid dealing with mains voltage, if possible. Basically I want my rpi to know when I'm in the room and in my mind the easiest way is to have it tell if I've turned on a light switch. I'm not trying to measure anything: just a simple on/off. A lot of posts on this subject are the opposite: having an rpi control a light switch. – DavidR Feb 21 '17 at 3:24
  • In that case, wy don use a motion sensor? A PIR for Arduino will cost you only a few bucks and is all low voltage. – kxtronic Feb 24 '17 at 17:21

Sorry to answer my own question: I found a beautiful answer on reddit. In short: take a cheap led night light and replace the led with an optocoupler. The optocoupler will keep the raspberry pi safely isolated from the mains power.

  • Please accept your own answer with a click on the tick on its left side. Only this will finish the question and it will not pop up again year for year. – Ingo Feb 9 at 15:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.