Relays for dummies

Hi folks,

My wife reminded me yesterday that I bought - more than a year ago - a series of relay devices, a.o. to control the garage doors via Node-RED. And this time she is right: I should finish that project in the next weeks, otherwise it will never happen ... But I really need some help on the hardware part :woozy_face:

My garage port opener can currently already be controlled via a manual switch:


Input 10 is normally high (23,3 VDC), but with the manual switch I can pull the voltage down shortly:


As soon as there is a falling edge on pin 10, the port will start moving (up or down).

1. Controlling via Node-RED
By having an extra relay (in parallel of the manual switch) between pin 1 and 10, I should be able to control the port via the GPIO pins of my Raspberry:


Since I wanted to have some nice din-rail relays, a collegue of mine advised me to buy these (datasheet link is on that page). But unfortunately he doesn't work here anymore, so now I'm stuck.

  • Should I add optocouplers to protect my Raspberry?
  • Should I add fuses in case the wire is damaged?
  • How should i protect my garage port opener from me destructing it?
  • Since the wire between my Raspberries and the opener is about 35 meters, should I do anything special?
  • Anything else?

2. Measuring via Node-RED
I would like to read the state of pins 5 and 6 of my garage port opener, to show on my dashboard whether the garage port is currently open or closed. But these signals are also 23,3 VDC. But not sure how to read those via GPIO. Can I use my Din-Rail relays also to accomplish this. Would be nice if everything was din-rail …

Thanks a lot for saving my mariage !!


I would use opto isolators both ways. 1to protect the Pi output and isolate from the relay and 2 to receive the inputs and drive the pi inputs. For the inputs the thing to check is what voltage is left after your 35m of cable, and then use a simple resistor divider to drive the opto appropriately. ( if not sure assume it’s a led that needs 2.1v @ 10ma)


I would use 5V relay module.
Pi uses 5V, so why not use the same (not from Pi GPIO, but from the Pi power supply).

Caution when choosing relay module, there are active low, active high and active low/high modules.
I have a good experience with low/high trigger module (usually a red PCB).


The image is attached, the input part is the same, the output part is not important.
You need to connect the middle jumper pin to GND. Top jumper pin unsolder or break.
Therefore, the module output (relay) is powered by 5V DC (DC+, DC- on PCB), and the input (IN on PCB) is driven with 3.3V from Pi GPIO (active when high).

There is no reason for additional fuse.

Put the relay as close as possible to the Pi. I do not think that 35m will be a problem for only make a contact.

What is voltage on the pin5 & pin6 when door is open/close/between open and close?
State is permanent or like impuls?

1 Like

I would go one stage further and use an inexpensive Wemos D1 Mini or equivalent to operate the relay (via an optocoupler) and sense the garage door position switches (rather than connecting them to a RPi).

If you flash the Wemos with ESP Easy then you'll have everything you need (WiFi connectivity, MQTT, switch inputs, outputs, LCD drivers, etc...).

Two reasons - (one) you can leave your Raspberry Pi in a central location (not necessarily in the garage) and (two) if you mess things up with voltage levels etc.. you will only destroy a GBP 2 pound Wemos rather than an expensive RPi.

Just my two-penny worth (but as you know I'm a fan of the Wemos D1 Minis - ha, ha, ha).


As I understand, raspberry is not in garage (35m),so maybe there is no wifi in garage....

Phew a 35m run for the switching wires is asking for trouble as the wires are bound to pick-up noise as well as suffering a voltage drop from one end to the other.

I ran a CAT cable out to my garage then used a cheap wireless router to create a WiFi access point.

Or you could use a WiFi over mains wiring pair of units to get WiFi to the garage.

Once you've got WiFi there you can do all sorts of things... like rigging-up an internet radio to listen to music whilst you are working.


Me too, the resistance in the 35mts of cable (70mts in real terms) will have an effect, but due to the low current requirements of a opto-isolator LED, I don't think that the voltage drop would be too much of a problem if you used appropriate cable.
There maybe a common ground between Pins 1 & 6, in which case you could probably use a 2 core screened cable (the screen being ground). This would help screen interference being picked up by the length of cable.

Instead of using a resistor divider to protect the inputs (at both ends), I'd be tempted to use a simple zenor diode/resistor circuit, to ensure that the respective LED voltages are not exceeded. See this example which uses just 2 components.

Assuming a trigger voltage of 23V, and you need 3.3v @ 50mA to drive the opto-isolator LED, something like the sketch below would ensure that you didn't blow the opto-isolator LED's.

The value of R includes the resistance of the cabling.
Using Ohm's Law - Resistance =Volts/Current
We need to drop 19.7V across R (leaving 3.3V for the opto)
So 19.7/0.05 = 394 Ohm
That resistor value is a maximum value, because the zener diode will also drain current if the voltage exceeds 3.3V, so I would suggest a value of 270 Ohm which would provide 50mA for the opto, and 23mA shunted by the zener.
The maximum power dissipated by the zener would be Volts x Current so 3.3 x 0.023 = 75.9mW. (something like this would suffice).

This is just an example - it would need tweaking depending upon the spec of the opto-isolator.


Morning guys,
You have no idea how MUCH I appreciate all your input!! The wife was surprised that I finally started at least a discussion on the forum about her garage port request. So I assume she won't divorce from me yet. Perhaps next week, when I don't manage to put your knowledge into practice :rofl:

@dceejay: Ok I understand that, but do you have any proposal to protect the pcb of my garage port opener. Those are pretty expensive devices, and it won't be appreciated if that is somehow damaged, e.g. by short circuit of the wiring …

@markost: that is indeed a good proposal, but I think I need to give you some more history ... Initially (more than a year ago …) I bought some similar stuff on Aliexpress. But I asked here for my birthday an official electricity closet and a whole box of din-rail relays:

And I even have CAT6 cables installed from my garage port openers to this closet. So that is the reason I really want those din rail relays working… In my local store there are other Finder relay modules that fit into the blue sockets, but not sure which one I would have to buy. For example:


I figured out that I can determine the port status, by measuring the voltage levels of both pins:


So if I could read pins 5 and 6 via GPIO into Node-RED, then I know enough ...

@dynamicdave: Ah I didn't know that yet :rofl:
Indeed that is also something I have been thinking about. Working distributed certainly has a lot of advantages, but I would really like to have all the relays in the (meanwhile famous) central closet. That is one of my many brain aberrations…

@Paul-Reed: do you have any idea whether this is available (e.g. on Ali) as a module, or do I have to start soldering? Perhaps a board with N of these modules somehow… Would be a bit disappointed in my closet became filled with self-soldered stuff.
Thanks for the circuit diagram!

Hmm I don't have a shielded cat cable… But some time ago, a network guy was doing a lot of cabling work in our office, and then I asked him how he would do the wiring (to avoid interference). He proposed to solder together both wires of a twisted pair, to connect them both to the same pin. So I did it like he told me:


And nobody mentions 'fuses'. Isn't that required when using optocouplers perhaps? What happens when somebody accidentally cuts to the wires some day?

I'm not too sure about that idea/suggestion.
The whole idea of a twisted pair is to counteract the effect of induced voltages (e.g. noise).
i.e. the magnetic field around each twisted wire should cancel out.

To handle the door (1. question) I would definitely put relay module in the house (closer to the raspberry pi).

This is really a small module. For a price of US $ 0.63 & free shipping ...
It is very easy to mount and the wire connections are by screw terminal, almost plug & play :smiley:

No zener diode and complication...

The transmission of such a low voltage (3.3V) to 35m does not make me the best option.

Preferably take a 24V voltage to/from the garage. Much less sensitive, and there is no equipement in the electricity closet :grin:

Look at the picture.

Some usual values for optocoupler are:

Vf= 1.0V - 1.4V
If=50mA but this is maximum at constant powering. Only 2-3mA is enough for normal operation.

Also, 50mA on Pi GPIO is dangerous!


"Why don't I set all my pads to the maximum current!
Two reasons:

  1. The raspberry-Pi 3V3 supply was designed with a maximum current of ~3mA per GPIO pin. If you load each pin with 16mA the total current is 272mA. The 3V3supply will collapse under that!

  2. Big current spikes will happen especially if you have a capacitive load. That will"bounce" around all the other pins near it. It is likely to cause interference with theSD-card or even the SDRAM behaviour.

What is a safe current?

All the electronics of the pads are designed for 16mA. That is a safe value under which youwill not damage the device. Even if you set the drive strength to 2mA and then load it so16mA comes out that will not damage the device. Other than that there is no guaranteedmaximum safe current."

Who said anything about 50mA on Pi GPIO?
I said 50mA to drive the opto-isolator, by way of example.
Depending upon the spec of the opto isolator, that figure could be much lower.

But in any event, if a GPIO pin is set as an input, it will only draw whatever current it needs to influence the change regardless of what current is available, and would not try and sink 50mA!
So not dangerous.
This only becomes an issue if your using a GPIO pin as an output, and try to draw more current than the pin/device can provide.

Seems it should be 'garage door' instead of 'garage port' in english, but seems most of you know what I mean :wink:

@dynamicdave: Do you have any other proposal how I could connect the CAT cable to my garage 'door' opener? Perhaps adding a ground to every twisted pair perhaps (??):


@markost: I hate to admit, but seems like you are right. When I use my din-rail relais, I still need to put the optocouplers somewhere… I have bought in the past boards on Ali with 8 relais and optocouplers, so just need to figure out how I can mount these a bit 'professionally' in my close.

Woow!!!! That is surely something that can get me started doing some experiments. Thank you so much for spending your time on my question!!! Hopefully other people - doing similar stuff - can reuse your diagram also …

I don't want to be greedy, but if you ever have some spare time to make another sketch (about reading the pin 5/6 with 23 VDC via GPIO) then I think I have everything to get started :flushed:. In return I will publish some new nodes in the near future. That is the only thing I can offer at this very moment as a compensation :joy:

Hi Bart - I think that would be a much better solution.


Ok, you are on the garage side (open/close state detection).
I was thinking about opening / closing the door ... from the Pi side to the garage electronic...

Sorry for misunderstanding...[quote="Paul-Reed, post:13, topic:13714, full:true"]

Who said anything about 50mA on Pi GPIO?
I said 50mA to drive the opto-isolator, by way of example.
Depending upon the spec of the opto isolator, that figure could be much lower.

But in any event, if a GPIO pin is set as an input, it will only draw whatever current it needs to influence the change regardless of what current is available, and would not try and sink 50mA!
So not dangerous.
This only becomes an issue if your using a GPIO pin as an output, and try to draw more current than the pin/device can provide.

I know the difference between I / O current, I was be on the Output current side .... :+1:


Door FULL close:

P5 = LOW => Relay_1 OFF => GPIO_X = LOW
P6 = HIGH => Relay_2 OFF => GPIO_Y = LOW

Door FULL open:

P5 = HIGH => Relay_1 ON => GPIO_X = HIGH
P6 = LOW => Relay_2 ON => GPIO_Y = HIGH

Different trigger levels for Relay 1 / Relay 2.

How I suppose the door is closed most of the time, both relays are energized only when the door is open.

Eco friendly mode :parrot: :deciduous_tree: :smile:

1 Like

@markost: nice drawing!! Thanks a lot! Never thought this morning that this discussion would evolve so fast...

My last questions for today:

  • The pullup resistors in your diagram, are those the internal raspberry resistors?
  • What would be the easiest way to protect pin 5/6, i.e. something near the door opener? Don't want to damage my door opener... 'Some' (fast?) fuse perhaps, that I put inline with this?

you can use internal pullup (I prefer external...)

Fuse is OK (some small, like 50-100mA).

I'm just thinking about protecting the long wires (to the house) ...
Maybe zener or TVS voltage diode slightly higher than 3.3V / 24V, but I can not reliably recommend something ...
Maybe somebody with more experience in practice recommends something specific ....