How to connect relay to raspberry pi

Hi,
I know this is an off topic question but since its related to home automation and openhab i am posting it here.

I need some help regarding connecting raspberry pi to relay module.What all things should i consider while connecting it to the mains?

Also i need some help in connecting the relay module in such a way that if a switch is turned on/off from the switch board then the corresponding gpio pin should be updated. Is this possible?

Because currently what i have seen is that you can only control the lights from the app or web interface by sending a command to the gpio pin.

I hope i am clear.

Thanks

In terms of openHAB, controlling a relay by GPIO is pretty much the same as controlling a light bulb or LED by GPIO. Have you looked at more generic examples of controlling a relay by GPIO from your Raspberry Pi? That’s probably the best place to start with this.

Hi Nishant

Follow these instructions: http://mcuoneclipse.com/2015/12/24/blinky-led-with-openhab-on-raspberry-pi/

You will be able to switch a relay the same way as you switch on an LED.

Regards

@trokkie Thank you for the link.
From my understanding controlling a relay from gpio is relatively easy. But what my concern is , what if I turn on the light manually from the switch in my room. Will that affect the relay? And will raspberry pi be notified regarding that? If not then how should I deal with that situation

@danielwalters86 Thank you for the information.
Yes I just looked into some examples of controlling the realy from gpio. But how to deal with situations in which light is turned on from the mains. That ways relay and gpio won’t know about it.
Could you provide me with some live in examples of this if possible?

I’m not an electrician nor an electrical engineer but I imagine designing that circuit isn’t trivial for a beginner (which I assume you are based on your question). The challenge I see here is getting some sort of feedback from your HIGH voltage side to your LOW voltage side (back into your Raspberry Pi/openHAB), in the absence of power on your HIGH side.

Have you looked at any off the shelf alternatives which may suit your needs without you doing it yourself?

There is no way for the relay to report that the light was turned on some other way. You will need something other sensor set up to report its status. There are a range of options depending on your situation and what it is you are controlling. For example, I have a relay that controls my garage door openers. I use a reed switch to report whether the garage door is open or closed. If you are controlling a light you can use a Photoresistor and a capacitor to detect when the light is on. You could use a current sensor to detect when a device is drawing power.

However, I will second @danielwalters86’s suggestion. If your main goal is to do home automation, it would be worth while to look into off the shelf solution. It will cost more but save tons of time, work, and frustration. But if you want to learn DIY electronics I can’t think of a better first project.

I can, one that doesn’t involve live mains!

1 Like

There is no easy way doing this without changing your existing wiring. The easiest foolproof way is to wire your circuit the same way as for a passage where you have 2x switches controlling one light. where on switch is your physical switch and the other is your relay. See this link Weiterleitungshinweis.

I would personally rather do the switching just through the relay and use the switch in such a way to pulse the raspberry to change the GPIO output.

@rlkoshak @danielwalters86 @trokkie That is quite a good amount of information for me to start with. and its true that home automation is totally about having off the shelf solutions. However, i am a newbie in electronics and seeking for some advice on how to approach towards solving common problems which are feasible enough.
Thank you so much for the valuable information.