Vincintosh
Posts: 5
Joined: Fri Oct 20, 2017 10:45 am

Weird relay behaviour: always on if GPIO is set to OUTPUT, kinda turns off if GPIO is set to INPUT

Fri Oct 20, 2017 10:53 am

Hello everyone, I'm new to the raspberry world, but I've read quite a lot of stuff before starting my project. I need to control a relay with my raspberry through a simple web interface, I just need a button that activates the relay and one that deactivates it. Sound easy, usual stuff.
Anyway, I installed wiringpi so that I can have access to the GPIO without root, loaded the w1-gpio kernel module, set the GPIO pin I wanted to OUT and the relay turns on without me even setting the GPIO pin value to 1. So I set the GPIO pin value to 0 but the relay doesn't turn off. If I disconnect the wire from the GPIO pin, it turns off, so the relay itself seems to be working. If I set the GPIO pin mode to IN the red light on the relay board turns off, but I hear no click sound coming from the relay and it doesn't fully turn off, in fact if I check with a multimeter it is still on. The relay board I'm using is this one http://www.ebay.it/itm/Modulo-Rele-1-ca ... SwbtVZRnuv

Any ideas?
Thank you for your time

klricks
Posts: 4812
Joined: Sat Jan 12, 2013 3:01 am
Location: Grants Pass, OR, USA
Contact: Website

Re: Weird relay behaviour: always on if GPIO is set to OUTPUT, kinda turns off if GPIO is set to INPUT

Fri Oct 20, 2017 12:10 pm

Most relay boards are 'ACTIVE LOW' which means:
0 or LOW (0V) = Relay ON
1 or HIGH (3V3) = Relay OFF
Go here for my RPi writeup. Basic config, Serial Port add-on etc:
http://blackeagle12.net/Comp/RPi/Rpi.html Click contact icon then world icon --->

User avatar
Mortimer
Posts: 804
Joined: Sun Jun 10, 2012 3:57 pm

Re: Weird relay behaviour: always on if GPIO is set to OUTPUT, kinda turns off if GPIO is set to INPUT

Fri Oct 20, 2017 12:12 pm

The input to the relay board is not active-low by any chance?

Did you try setting GPIO output pin to "1" to see if it turns off?


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

Re: Weird relay behaviour: always on if GPIO is set to OUTPUT, kinda turns off if GPIO is set to INPUT

Fri Oct 20, 2017 12:13 pm

Vincintosh wrote:
Fri Oct 20, 2017 10:53 am
Anyway, I installed wiringpi so that I can have access to the GPIO without root, loaded the w1-gpio kernel module, set the GPIO pin I wanted to OUT and the relay turns on without me even setting the GPIO pin value to 1. So I set the GPIO pin value to 0 but the relay doesn't turn off. If I disconnect the wire from the GPIO pin, it turns off, so the relay itself seems to be working. If I set the GPIO pin mode to IN the red light on the relay board turns off, but I hear no click sound coming from the relay and it doesn't fully turn off, in fact if I check with a multimeter it is still on.
Why did you load the w1-gpio module? That is only needed for 1-wire bus devices and is normally set up with an entry in config.txt or via the configuration menus. You don't need it to switch a relay. In fact it could cause problems depending on which GPIO pin you are using.

Can you post an example of code that works/doesn't work and also tell us which physical pin on the connector you are using (not the GPIO number).

User avatar
davidcoton
Posts: 2375
Joined: Mon Sep 01, 2014 2:37 pm
Location: Cambridge, UK

Re: Weird relay behaviour: always on if GPIO is set to OUTPUT, kinda turns off if GPIO is set to INPUT

Fri Oct 20, 2017 1:16 pm

I can't tell from the photo whether the relay board has a transistor (should be OK, should be active high input) or opto-isolator input. If it has an opto-isolator, it doesn't have a 3V3 connection, so it is likely that the input is referenced to 5V. THIS CAN DAMAGE THE PI'S GPIOS. It would also possibly fail to turn off when the input goes high. Try disconnecting the GPIO while leaving 5V and GND connected, switching on and measuring the voltage on the relay input. If it is 5V then this board should not be connected directly to a Pi. If that doesn't help, try posting a better photo of the relay board's components.
"If it ain't broke, fix it until it is."
“Raspberry Pi is a trademark of the Raspberry Pi Foundation”
The Pink Unicorn is a symbol of commitment to sensible discussion of future versions of the Raspberry Pi ™

Vincintosh
Posts: 5
Joined: Fri Oct 20, 2017 10:45 am

Re: Weird relay behaviour: always on if GPIO is set to OUTPUT, kinda turns off if GPIO is set to INPUT

Fri Oct 20, 2017 2:09 pm

Hello everyone and thank you for your attention

Switching the pin to high, as suggested by Mortimer, won't turn off the relay :(
rpdom wrote:
Vincintosh wrote:
Fri Oct 20, 2017 10:53 am
Anyway, I installed wiringpi so that I can have access to the GPIO without root, loaded the w1-gpio kernel module, set the GPIO pin I wanted to OUT and the relay turns on without me even setting the GPIO pin value to 1. So I set the GPIO pin value to 0 but the relay doesn't turn off. If I disconnect the wire from the GPIO pin, it turns off, so the relay itself seems to be working. If I set the GPIO pin mode to IN the red light on the relay board turns off, but I hear no click sound coming from the relay and it doesn't fully turn off, in fact if I check with a multimeter it is still on.
Why did you load the w1-gpio module? That is only needed for 1-wire bus devices and is normally set up with an entry in config.txt or via the configuration menus. You don't need it to switch a relay. In fact it could cause problems depending on which GPIO pin you are using.

Can you post an example of code that works/doesn't work and also tell us which physical pin on the connector you are using (not the GPIO number).
I'm not using any particular code, since I'm still testing if everything works before implementing it in a php page. I just use "gpio mode 5 out" and "gpio write 5 1/0"
davidcoton wrote: I can't tell from the photo whether the relay board has a transistor (should be OK, should be active high input) or opto-isolator input. If it has an opto-isolator, it doesn't have a 3V3 connection, so it is likely that the input is referenced to 5V. THIS CAN DAMAGE THE PI'S GPIOS. It would also possibly fail to turn off when the input goes high. Try disconnecting the GPIO while leaving 5V and GND connected, switching on and measuring the voltage on the relay input. If it is 5V then this board should not be connected directly to a Pi. If that doesn't help, try posting a better photo of the relay board's components.
I don't actually know how an octo-coupler exactly works, so I'm not sure I understood what you said in the first part of the message. Anyway here's what I did: I connected the relay in the same way I did before, set the pin I'm using to OUT and it turned on, then disconnected the wire that goes from the raspberry pin to the "In1" pin on the relay board and the relay switched back off as espected. Then measured the voltage between the "In1" pin and the "GND" pin on the board and it is around 4.2 volts, but I'm assuming that's because I'm temporarily powering the raspberry from a PC USB port (just to do this exact thing and post on the forum, I don't normally do it, it was just to save time in this precise moment), which outputs around 4.8V so, considering some voltage drop across the breadbord I'm using to test everything and the relay board, I guess this relay has 5V on the input pin... should I buy a new relay board?
Here's a better picture anyway
https://ibb.co/jrzG36

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

Re: Weird relay behaviour: always on if GPIO is set to OUTPUT, kinda turns off if GPIO is set to INPUT

Fri Oct 20, 2017 2:40 pm

Vincintosh wrote:
Fri Oct 20, 2017 2:09 pm
I'm not using any particular code, since I'm still testing if everything works before implementing it in a php page. I just use "gpio mode 5 out" and "gpio write 5 1/0"
So you are using WiringPi 5, which is GPIO 24 (BCM) or pin 18 on the board. The w1-gpio module shouldn't affect that pin at all.

I can understand the relay partly turning off it the GPIO is set to input as the pin will be floating in an undetermined state.

Looking at that picture, the relay board doesn't have an opto-isolator/coupler, so you don't have to worry about that, but it is possible it needs a 5V signal which the Pi can't supply from the GPIO which has a 3.3V maximum output.

User avatar
davidcoton
Posts: 2375
Joined: Mon Sep 01, 2014 2:37 pm
Location: Cambridge, UK

Re: Weird relay behaviour: always on if GPIO is set to OUTPUT, kinda turns off if GPIO is set to INPUT

Fri Oct 20, 2017 3:09 pm

rpdom wrote:
Fri Oct 20, 2017 2:40 pm
I can understand the relay partly turning off it the GPIO is set to input as the pin will be floating in an undetermined state.

Looking at that picture, the relay board doesn't have an opto-isolator/coupler, so you don't have to worry about that, but it is possible it needs a 5V signal which the Pi can't supply from the GPIO which has a 3.3V maximum output.
A pin set to input is in high impedance state. That will not of itself turn the relay off -- it is a symptom of pulling a 5V bias to 3V3 through the GPIO protection diode. VERY BAD.

So if the input pin is being pulled to 5V, you have two choices:
  1. Throw that board and buy one that really works with a Pi.
  2. Build a transistor driver that will keep you Pi GPIO safe while switching the 5V for the relay.
For option 2, you will need a resistor of about 3K3 from the GPIO output to the base of an NPN transistor (2N3704 or similar). Emitter to ground. Collector to relay input. Relay supply (5V and ground) from Pi as before.
Check the board has a protection diode across its coil (IN4001 or similar, wired to conduct with reverse voltage ("back EMF" that appears when the coil is switched off). If not, provide one. (I still need a better photo to check this.)
Job done.
"If it ain't broke, fix it until it is."
“Raspberry Pi is a trademark of the Raspberry Pi Foundation”
The Pink Unicorn is a symbol of commitment to sensible discussion of future versions of the Raspberry Pi ™

Vincintosh
Posts: 5
Joined: Fri Oct 20, 2017 10:45 am

Re: Weird relay behaviour: always on if GPIO is set to OUTPUT, kinda turns off if GPIO is set to INPUT

Fri Oct 20, 2017 4:52 pm

davidcoton wrote:
Fri Oct 20, 2017 3:09 pm
rpdom wrote:
Fri Oct 20, 2017 2:40 pm
I can understand the relay partly turning off it the GPIO is set to input as the pin will be floating in an undetermined state.

Looking at that picture, the relay board doesn't have an opto-isolator/coupler, so you don't have to worry about that, but it is possible it needs a 5V signal which the Pi can't supply from the GPIO which has a 3.3V maximum output.
A pin set to input is in high impedance state. That will not of itself turn the relay off -- it is a symptom of pulling a 5V bias to 3V3 through the GPIO protection diode. VERY BAD.

So if the input pin is being pulled to 5V, you have two choices:
  1. Throw that board and buy one that really works with a Pi.
  2. Build a transistor driver that will keep you Pi GPIO safe while switching the 5V for the relay.
For option 2, you will need a resistor of about 3K3 from the GPIO output to the base of an NPN transistor (2N3704 or similar). Emitter to ground. Collector to relay input. Relay supply (5V and ground) from Pi as before.
Check the board has a protection diode across its coil (IN4001 or similar, wired to conduct with reverse voltage ("back EMF" that appears when the coil is switched off). If not, provide one. (I still need a better photo to check this.)
Job done.
I'm sorry to disappoint you if this explaination took some time and/or research, but it really goes beyond my understanding and I think I'm going to buy another relay board. Just to be sure, is this one ok? Elegoo 4 Channel DC 5V Modulo Relay con ... 6zb096702S

User avatar
davidcoton
Posts: 2375
Joined: Mon Sep 01, 2014 2:37 pm
Location: Cambridge, UK

Re: Weird relay behaviour: always on if GPIO is set to OUTPUT, kinda turns off if GPIO is set to INPUT

Fri Oct 20, 2017 7:58 pm

That looks more promising. You will need to remove the link between JD-Vcc and Vcc, then connect JD-Vcc to 5V and Vcc to 3V3. That will run the relays at 5V and the opto-isolator inputs at 3V3, which is Pi-safe.
Some relay boards wired as thta one appears to be won't switch on from 3V3, if this is a problem an extra resistor will solve the problem.
Interestingly that board appears to use an opto--isolator followed by a transistor to drive the relay. Strange, but not important in this context. The input is probably active low.
"If it ain't broke, fix it until it is."
“Raspberry Pi is a trademark of the Raspberry Pi Foundation”
The Pink Unicorn is a symbol of commitment to sensible discussion of future versions of the Raspberry Pi ™

Vincintosh
Posts: 5
Joined: Fri Oct 20, 2017 10:45 am

Re: Weird relay behaviour: always on if GPIO is set to OUTPUT, kinda turns off if GPIO is set to INPUT

Fri Oct 20, 2017 9:22 pm

davidcoton wrote:
Fri Oct 20, 2017 7:58 pm
That looks more promising. You will need to remove the link between JD-Vcc and Vcc, then connect JD-Vcc to 5V and Vcc to 3V3. That will run the relays at 5V and the opto-isolator inputs at 3V3, which is Pi-safe.
Some relay boards wired as thta one appears to be won't switch on from 3V3, if this is a problem an extra resistor will solve the problem.
Interestingly that board appears to use an opto--isolator followed by a transistor to drive the relay. Strange, but not important in this context. The input is probably active low.
Alright, thank you I ordered it. How can I tell wheter or not I need that extra resistor and where should it go?

User avatar
davidcoton
Posts: 2375
Joined: Mon Sep 01, 2014 2:37 pm
Location: Cambridge, UK

Re: Weird relay behaviour: always on if GPIO is set to OUTPUT, kinda turns off if GPIO is set to INPUT

Fri Oct 20, 2017 9:56 pm

Vincintosh wrote:
Fri Oct 20, 2017 9:22 pm
Alright, thank you I ordered it. How can I tell wheter or not I need that extra resistor and where should it go?
Try it. If it doesn't work (experience with similar boards suggests it will) ask again.
"If it ain't broke, fix it until it is."
“Raspberry Pi is a trademark of the Raspberry Pi Foundation”
The Pink Unicorn is a symbol of commitment to sensible discussion of future versions of the Raspberry Pi ™

Vincintosh
Posts: 5
Joined: Fri Oct 20, 2017 10:45 am

Re: Weird relay behaviour: always on if GPIO is set to OUTPUT, kinda turns off if GPIO is set to INPUT

Sat Oct 21, 2017 7:52 am

davidcoton wrote:
Fri Oct 20, 2017 9:56 pm
Vincintosh wrote:
Fri Oct 20, 2017 9:22 pm
Alright, thank you I ordered it. How can I tell wheter or not I need that extra resistor and where should it go?
Try it. If it doesn't work (experience with similar boards suggests it will) ask again.
Ok thanks. It should arrive on monday: I will let you know

Return to “Troubleshooting”

Who is online

Users browsing this forum: Bing [Bot], Kleinhirn and 54 guests