av4625
Posts: 62
Joined: Sat Apr 01, 2017 11:06 am

Relay not working as expected

Fri Jun 16, 2017 3:44 pm

I have a relay with a red power light and a green light that tells you if its switched on or not. Im using wiringpi also.

I have the relay connected to 5v, gnd and gpio23.

Code: Select all

gpio mode 4 out
This seems to switch the relay on, (green light on and a click noise) I expected this to set the gpio pin.

Code: Select all

gpio write 4 1
This does nothing

Code: Select all

gpio write 4 0
This also does nothing

Code: Select all

gpio mode 4 in
This seems to switch the relay off, (green light off and a click noise)

I thought the write commands where meant to switch the relay?

Even when the light changes some times the pins dont change (Im using a multimeter to test which pins are live when the light changes)


Edit: So changing mode to IN and OUT seems to switch relay even when using GPIO library in python, Was I wrong in thinking that writing a 1 or 0 (changing output from HIGH to LOW) to the pin is meant to switch the relay?

Relay - https://www.amazon.co.uk/gp/product/B01 ... UTF8&psc=1

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

Re: Relay not working as expected

Fri Jun 16, 2017 6:32 pm

Hi.

The relay card you have is designed for devices that use 5v GPIO, the Pi has 3.3V GPIO.
many of these relays also work active LOW, which means a LOW out will switch it on and a HIGH out will switch it off but seeing as the PI is 3.3V GPIO it does not have the voltage to switch the relay card off.

To drive you relay card from the PI GPIO you need to use a transistor circuit like this.
Image
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

av4625
Posts: 62
Joined: Sat Apr 01, 2017 11:06 am

Re: Relay not working as expected

Fri Jun 16, 2017 10:35 pm

Thanks for that! I will give that a try! I was so confused that when I turn the mode from in to out that the relay switches. I thought the vcc and in on the relay was connected someway making it a faulty relay (I was getting 5volts on the in pin on the relay when power and ground was connected and before connecting the gpio pin using a multimeter).

Because I thought it was faulty I went to maplin and got a sealed reed relay and it seems to work. (4 pins)

Is there a type of relay similar to the one that i linked above that will work without a transistor? All the tutorials I see online use relays similar to the one i linked but don't need a transistor (I thought thats what I was buying, Im new to electronics).

Thanks for your help

Edit: As I don't have any transistors, I plugged 3.3v power into the VCC of the relay and it works :) although writing 0 turns it on and writing 1 turns it off but it still works. I will have to purchase some transistors and some more resistors!

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

Re: Relay not working as expected

Sat Jun 17, 2017 5:56 am

Hi.

You need to look for relay cards that have opto-isolator inputs and say they are 3.3v compatible.
I hope you are not trying to drive that relay you got from maplins directly from the GPIO unless its a very small one you will overload the GPIO pins.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

User avatar
Gert van Loo
Posts: 2487
Joined: Tue Aug 02, 2011 7:27 am
Contact: Website

Re: Relay not working as expected

Sat Jun 17, 2017 7:17 am

You are very likely to be OK as I see a transistor/FET on the board (but I can't be 100% sure unless I have a schematic.)
This means it will need only a very low voltage (~1Volt) to switch and thus it will work from 3V3.
In that case you also should connect it back to the 5V as on 3V3 it may not work reliably.
The coil voltage will be 5V.

If you really want to be sure connect the control input to a potentiometer and measure when it switches on:
pot.png
pot.png (3.79 KiB) Viewed 9606 times

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

Re: Relay not working as expected

Sat Jun 17, 2017 8:13 am

Gert van Loo wrote:You are very likely to be OK as I see a transistor/FET on the board (but I can't be 100% sure unless I have a schematic.)
This means it will need only a very low voltage (~1Volt) to switch and thus it will work from 3V3.
In that case you also should connect it back to the 5V as on 3V3 it may not work reliably.
The coil voltage will be 5V.

If you really want to be sure connect the control input to a potentiometer and measure when it switches on:
pot.png
If you read the first post, has already said it did not switch when connected to 5v would switch on but not off.
many of these relays do not work with the Pi seems to be a common question asked, hence having the diagram already drawn showing how to use transistor to drive relay, also using 5v to power relay puts 5v on GPIO pin, again op has already found this himself.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

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

Re: Relay not working as expected

Sat Jun 17, 2017 8:23 am

I read it as it would switch but only using mode not write

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

Re: Relay not working as expected

Sat Jun 17, 2017 12:12 pm

So How these relay cards work is, control pin (In) is looking for a ground so sink current and so turn on the relay.
when you set the GPIO to output and set it low current can flow from the In pin at 5V to the 0V provided by the GPIO pin.
To turn the relay off you need to stop this current flow by raising the GPIO pin to high but because the Pi GPIO is only 3.3v a small current can still flow so holding the relay on.
By changing the GPIO pin form output to input you change its impedance input being higher impedance than output and so prevent the current flow and so switching the relay off.

I believe this has been explained may times before on this very forum.
Last edited by pcmanbob on Sat Jun 17, 2017 12:25 pm, edited 1 time in total.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

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

Re: Relay not working as expected

Sat Jun 17, 2017 12:20 pm

Thanks for the explanation, I knew the relay voltage bit (I modified my relay module for that reason), but hadn't seen the GPIO mode bit before :D

av4625
Posts: 62
Joined: Sat Apr 01, 2017 11:06 am

Re: Relay not working as expected

Sat Jun 17, 2017 8:59 pm

Thanks for all the explanations guys as I say im new to electronics. I am not turning mode from in to out to switch the relay in the long term incase it damaged the pi, I just tried it.

Also I wont be powering the relay from the 3.3v incase I damage something, again I just tried it. (Am I likey to damage something powering the relay from 3.3v)

http://www.maplin.co.uk/p/5v-spst-seale ... elay-jh12n

This is the reed relay that I got, I am switching it using GPIO (3.3v) is this safe todo? Theres 5v going through it and its being switched by 3.3v. Seems to work fine!

So just a few questions to clear it up for myself.
  • Its not safe to use the original 5v relay I had and try and use GPIO to switch it as this puts 5v on the GPIO pin even if its set to out?
  • It is not safe/reliable to power the original 5v relay from 3.3v?
  • It is safe to use the original relay if I set it up with the transistor as the diagram above?
  • Is the 4 pin reed relay safe to use by switching from the 3.3v GPIO? (It doesn't have constant 5v power)
  • Is there a book or anything that is able to teach me electronics at this scale? Possibly using raspberry pi? As I wouldnt have known that the transistor needs resistors at either side or what type of transistor to use if the above diagram wasnt as detailed.



Thanks everyone
Last edited by av4625 on Sun Jun 18, 2017 12:36 am, edited 1 time in total.

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

Re: Relay not working as expected

Sat Jun 17, 2017 11:15 pm

Don't confuse the coil voltage with the control inputs. Relays are designed with various coil voltages 5V, 6V, 12V, 24V etc.
The coil must be powered by the voltage it was designed to use. Since the RPi has a 5V pin it is convenient to use that for a 5V relay as long as there is not too many relays or other things using the 5V. Any other voltage would require a separate power supply to operate the coils.
Note that the reed relay in your link operates at 12V so would need a 12V power supply.

One common misconception is that the relay outputs voltage to the relay contacts to power whatever is connected to the relay....... A relay does not do that. The relay is only a switch and whatever device you connect needs its own power source. If the device happens to use the same voltage as the coils then one power supply can do both, otherwise a second power source is needed.

The control inputs are totally separate. Usually the relay boards are designed to use 5V TTL though some boards designed for automotive applications may take 12V or 24V inputs. Since the relay control is input the RPi GPIO needs to be set to output. In 5V TTL logic anything above ~2V is interpreted as high, so the 3V3 that the RPi puts out should be OK.... BUT not always. There are many examples of relay boards working fine in tutorials and YT videos etc. Some relay boards don't work without adding a transistor circuit.

Also don't forget that a ground wire is always required between RPi and relay board.

As mentioned most relay boards are ACTIVE LOW.
Unless specified otherwise my response is based on the latest and fully updated Raspbian Buster w/ Desktop OS.

av4625
Posts: 62
Joined: Sat Apr 01, 2017 11:06 am

Re: Relay not working as expected

Sun Jun 18, 2017 12:37 am

Sorry i linked the wrong relay above, i changed the link to the 5v version that i bought.

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

Re: Relay not working as expected

Sun Jun 18, 2017 1:15 am

av4625 wrote:Sorry i linked the wrong relay above, i changed the link to the 5v version that i bought.
The reed relay is a 'raw' relay and would need a protection diode across the coil to prevent back EMF.
You would also need to add the control circuitry such as a transistor or opto isolator and associated components.
...... All this stuff that is already included in the off the shelf relay boards.... Plus relay boards have the screw terminals for easy connection to whatever device.
Unless specified otherwise my response is based on the latest and fully updated Raspbian Buster w/ Desktop OS.

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

Re: Relay not working as expected

Sun Jun 18, 2017 10:00 am

What ever you do don't connect the bear relay to your GPIO especially if its connected between the 5v pin and a GPIO pin as you will damage your Pi.

you need to drive the relay using a transistor, opto - isolator or if using multiple relays a ULN2803A darlington array.

example transistor circuit.
Image

example opto -isolator circuit.
Image
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

av4625
Posts: 62
Joined: Sat Apr 01, 2017 11:06 am

Re: Relay not working as expected

Sun Jun 18, 2017 9:04 pm

ImageIMG_0521 by [url=https://www.flickr.com/photos/[email protected]/]

Yellow - 5v going in
Blue - 5v going through / coming out when switched (Hard to see blue, its between the white and black)
White - Connected to a GPIO pin
Black - Ground

The 5v and GPIO aren't connected this way.

This is not safe with out the other components mentioned above?

I am using the relay for the same purpose as the guy in this video (It is used to automatically press the sync button on a wii balance board)
https://www.youtube.com/watch?v=LGdF0THwzuQ&t=478s

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

Re: Relay not working as expected

Mon Jun 19, 2017 9:39 am

av4625 wrote:ImageIMG_0521 by [url=https://www.flickr.com/photos/[email protected]/]

Yellow - 5v going in
Blue - 5v going through / coming out when switched (Hard to see blue, its between the white and black)
White - Connected to a GPIO pin
Black - Ground

The 5v and GPIO aren't connected this way.

This is not safe with out the other components mentioned above?

I am using the relay for the same purpose as the guy in this video (It is used to automatically press the sync button on a wii balance board)
https://www.youtube.com/watch?v=LGdF0THwzuQ&t=478s
As a minimum you need to fit a back emf diode to protected your GPIO from the high reverse voltage that occurs on relay deactivation, in theory you should only be drawing about 7mA to power the relay which is OK seeing as you are powering it with 3.3v .
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

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

Re: Relay not working as expected

Mon Jun 19, 2017 9:47 am

Here's one relay board I modified to work with the Pi. Works OK for me but do it at your own risk !!

https://www.amazon.co.uk/XCSOURCE-Chann ... elay&psc=1

Add a 4.7k resistor EITHER on top of the board or below, NOT both. It is between pin1 on the opto-coupler and ground.
Attachments
mod2b.jpg
modified relay
mod2b.jpg (55.82 KiB) Viewed 9335 times

Return to “HATs and other add-ons”