amirj11
Posts: 4
Joined: Sat Mar 30, 2019 3:24 pm

GPIO does not drive relay board properly

Sat Mar 30, 2019 3:29 pm

Hey! I'm trying to work the GPIO with a 220V power relay and C++.
I'm working with this guide: http://hertaville.com/introduction-to-a ... -in-c.html ,
Trying the shell commands first to get to know how to work with the GPIO.

However, the following command: 'echo "out" > /sys/class/gpio/gpio4/direction'
actually turns on the pin, even before i do the 'echo "1" > ... value' command. and I can't seem to turn it off properly. sometimes it will turn off when I do the opposite command: 'echo "in" > sys/class/gpio/gpio4/direction'.

any idea why these commands trigger the pin to go on/off? I thought it was only a settings command and the next one (echo "1" /sys..../value) should be turning it on.
thanks!

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 12351
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: GPIO does not drive relay board properly

Sat Mar 30, 2019 4:54 pm

Nothing to do with the GPIO's "not working properly" everything to do with relay driver boards not designed for 3V3 GPIO's, but only for 5V GPIO's (Arduino's and such).
It a common pitfall, and I wrote about it here: viewtopic.php?f=91&t=83372&p=1225448#p1225448

gordon77
Posts: 4237
Joined: Sun Aug 05, 2012 3:12 pm

Re: GPIO does not drive relay board properly

Sat Mar 30, 2019 6:25 pm

If you connect an led, and resistor, in place of the relay board does that work as expected?

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

Re: GPIO does not drive relay board properly

Sat Mar 30, 2019 9:47 pm

When you issue the command 'echo "out" > /sys/class/gpio/gpio4/direction' it sets the output as low by default.
Now as your relay board is likely to be active low , which means the relay activated when a low signal is applied to the In pin,
Your relay will switch on as soon as you set the gpio pin to output.

Now because the control circuit on the relay board is likely to be 5V the 3.3V of the gpio is not sufficient to switch the relay off as some current will still be flowing from 5V to 3.3V so keeping the relay on.

Read the link in mahjongg's post for more detail.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

amirj11
Posts: 4
Joined: Sat Mar 30, 2019 3:24 pm

Re: GPIO does not drive relay board properly

Sun Mar 31, 2019 2:49 pm

Thanks guys! I hadn't thought about that.
mahjongg - the link you referred to explained it perfectly. I know little about electricity / soldering, I will try to look for some 3.3V relays.
something like this maybe: https://www.aliexpress.com/item/3-3V-5V ... st=ae803_4

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

Re: GPIO does not drive relay board properly

Sun Mar 31, 2019 4:16 pm

amirj11 wrote:
Sun Mar 31, 2019 2:49 pm
Thanks guys! I hadn't thought about that.
mahjongg - the link you referred to explained it perfectly. I know little about electricity / soldering, I will try to look for some 3.3V relays.
something like this maybe: https://www.aliexpress.com/item/3-3V-5V ... st=ae803_4

Be Mindful of the 16ma max limit for a gpio, no information visible to a casual glance of your add on what the relay boards draw when activated,

gordon77
Posts: 4237
Joined: Sun Aug 05, 2012 3:12 pm

Re: GPIO does not drive relay board properly

Sun Mar 31, 2019 4:50 pm

You should only be driving the trigger from the GPIO, but as said above check for <16mA, but the relay will also need 3.3v ensure the pi 3.3v supply is sufficient.

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

Re: GPIO does not drive relay board properly

Sun Mar 31, 2019 5:17 pm

Just be careful with the 3v version of that relay , there is anther post were someone has used on of those and it won't operate correctly when triggered from the 3.3v gpio pin and there is no opto isolator on those relay boards.


other post viewtopic.php?f=37&t=236942&p=1449451#p1449451
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

amirj11
Posts: 4
Joined: Sat Mar 30, 2019 3:24 pm

Re: GPIO does not drive relay board properly

Sun Mar 31, 2019 8:25 pm

Thanks for the clarification! this one appears to have the opto isolator:
https://www.aliexpress.com/item/1PCS-3- ... 4c4d3E8IRP

walterwoj
Posts: 28
Joined: Fri Mar 15, 2019 10:46 pm

Re: GPIO does not drive relay board properly

Sun Mar 31, 2019 8:52 pm

For those who are interested I purchased this relay board and powered it externally according to these instructions and it works perfectly in my testing so far. It does work counter-intuitively (active low) in my opinion but it does work reliably.

Milliways
Posts: 465
Joined: Fri Apr 25, 2014 12:18 am
Location: Sydney, Australia

Re: GPIO does not drive relay board properly

Mon Apr 01, 2019 6:44 am

walterwoj wrote:
Sun Mar 31, 2019 8:52 pm
For those who are interested I purchased this relay board and powered it externally according to these instructions and it works perfectly in my testing so far. It does work counter-intuitively (active low) in my opinion but it does work reliably.
The spec for this relay states "each one needs 15-20mA Driver Current" which would exceed the Pi GPIO maximum - they probably wouldn't draw this much from the Pi because the voltage is too low OR if powered from 5V will damage the GPIO.

The transfer ratio of most opto-couplers is low (although it varies over a wide range for nominally identical parts) - so probably won't reliably drive the relay.

The best way to use these with the Pi is to remove the opto-coupler and drive the base of the transistor directly - or better buy a module (without opto-coupler) designed for 3.3V use.

Any isolation the opto-coupler provides is illusory if the input shares ANY connection with the output (Gnd or power).

Return to “Beginners”