rooster
Posts: 100
Joined: Thu Aug 02, 2012 8:09 pm

driving a relay

Mon Sep 17, 2012 9:18 pm

Hi

I found this nice circuit when looking at powering a relay using a GPIO pin. I don't know enough about the relays, but this seems such a nice solution to most powering issues I wondered if there are relays that can be bought which contain the transistor. And that you just need to connect from the PI, GND, +5V and pin and from the real world, the 2 or 3 connections from the device to be powered?

Thanks

R

rooster
Posts: 100
Joined: Thu Aug 02, 2012 8:09 pm

Re: driving a relay

Mon Sep 17, 2012 9:33 pm

ok, to half answer my own question, I realised I'd bought one of these a few weeks ago in the hope it might be useful and then forgot about it...

http://www.ebay.co.uk/itm/2-Channel-DC- ... 163wt_1045

but...how do I know if the PI can provide the power to the relays? I assume the GPIO pin is fine because its supplying a small current to the transistor (so will be less than the 20mA), but I can't see a) the current the relay needs to operate or b) the current that the PI can supply.

Thanks

geoffr
Posts: 89
Joined: Wed Aug 22, 2012 11:25 am
Location: Melbourne, VIC

Re: driving a relay

Mon Sep 17, 2012 10:39 pm

I've been on my own journey over the past couple of weeks of working out how best to drive relays from a Pi. I also started out looking at relay boards, such as the one you found on eBay. The first challenge is that most commercially available relay boards expect 5V logic, while the GPIOs on the Pi are 3.3V. That means that you will need to do a level conversion between the Pi and the relay board.

Preferably, it will also help if you can find the schematic of the board you want to buy, in order to work out whether it is suitable. Especially, you want to be careful about the current on the GPIO pins (aside from voltage level issues.)

In the end, I decided that I was better off using something I have built myself. If you just want to use the GPIOs to driver relays, you should be able to build something relatively easily using something like a ULN2803A Darlington transistor array.

In the end I have opted for a slightly more complex path by using the I2C bus, which I felt afforded me a more elegant and extensible solution. There are also lots of cheap IO expanders available for the I2C bus - I have used the MCP23017 (just around $1 a pop). I can post a schematic of what I have built so far tonight.

TarjeiB
Posts: 157
Joined: Thu Jul 12, 2012 3:33 pm

Re: driving a relay

Tue Sep 18, 2012 7:49 am

rooster wrote:ok, to half answer my own question, I realised I'd bought one of these a few weeks ago in the hope it might be useful and then forgot about it...

http://www.ebay.co.uk/itm/2-Channel-DC- ... 163wt_1045

but...how do I know if the PI can provide the power to the relays? I assume the GPIO pin is fine because its supplying a small current to the transistor (so will be less than the 20mA), but I can't see a) the current the relay needs to operate or b) the current that the PI can supply.

Thanks
Check out my experiences in this thread with that exact relay.

Short summary, the Pi will power it fine from GND and +5V pins, but the trouble is that it will always be ON if you feed it anything from 0V-4.9V on the IN1 and IN2 pins, and only turn OFF if you put 5V or more to the IN1 and IN2 pins.

The Pi only gives 3.3V on the GPIO pins so it will never turn the relay off.
Someone else used a transistor in front of the relay to fix this, but that's another story :)

JoeDaStudd
Posts: 274
Joined: Fri Jan 27, 2012 1:34 pm

Re: driving a relay

Wed Sep 19, 2012 10:54 am

Its probably worth getting one of these and seeing if its possible to convert the one you've got as the pcb looks very similar.

rooster
Posts: 100
Joined: Thu Aug 02, 2012 8:09 pm

Re: driving a relay

Wed Sep 19, 2012 1:06 pm

> Check out my experiences in this thread with that exact relay.

> Short summary, the Pi will power it fine from GND and +5V pins, but the trouble is that it will always be > ON if you feed it anything from 0V-4.9V on the IN1 and IN2 pins, and only turn OFF if you put 5V or > more to the IN1 and IN2 pins.
>
>The Pi only gives 3.3V on the GPIO pins so it will never turn the relay off.
>Someone else used a transistor in front of the relay to fix this, but that's another story :)

I've been trying with the transistor relay circuit you have and can't get it to work. I am switching the GPIO from 3V to 0V and switiching on and off an LED. But when connected to the relay curcuit it won't switch. although connecting to the +5V or gnd directly operates the relay.

This is confusing. I thought the 3 V was a signal and small current through the transistor which then sends 5V across the relay coild to do the switch. :(

fruitloaf
Posts: 84
Joined: Sun Feb 05, 2012 11:41 am

Re: driving a relay

Wed Sep 19, 2012 1:07 pm

I have that relay and yes you need a transistor in front of it to get it to go off. Not a big problem but if you're going to that trouble you might as well just buy the relays themselves and solder them to some vero board.

In my case I just soldered some vero to the pin header of the board with a couple of transistors on it to get it to work as I didn't pay attention to the circuit before I ordered.

rooster
Posts: 100
Joined: Thu Aug 02, 2012 8:09 pm

Re: driving a relay

Wed Sep 19, 2012 1:09 pm

> The Pi only gives 3.3V on the GPIO pins so it will never turn the relay off.
> Someone else used a transistor in front of the relay to fix this, but that's another story :)

but that circuit has a transistor in front of it which is why it should work - but doesn't :(

techpaul
Posts: 1512
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK
Contact: Website

Re: driving a relay

Wed Sep 19, 2012 1:21 pm

Perhaps if you post the exact circuit you are using with all part numbers and values for all parts someone can see where you are going wrong.

To me it suggests you can switch up to 20mA with transistor, but your relay coil needs a LOT more current so either the transistor is not going on hard enough to allow that much current through or you have another mismatch somewhere.

All relays and transistors are not the same, otherwise we would not have over 100,000 different ones
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/

rooster
Posts: 100
Joined: Thu Aug 02, 2012 8:09 pm

Re: driving a relay

Wed Sep 19, 2012 1:37 pm

Hi techpaul

This is the module

http://www.ebay.co.uk/itm/2-Channel-DC- ... f3305#shId

the circuit diagram is shown towards the bottom of the page. I was connecting VCC and GND to the +5V and GND of the PI and IN0 to GPIO0. The relay works when IN0 is directly connected to the GND or +5V but wont be driven by the 3.3V of the GPIO

Russ

fruitloaf
Posts: 84
Joined: Sun Feb 05, 2012 11:41 am

Re: driving a relay

Wed Sep 19, 2012 1:38 pm

circuit.png
circuit.png (13.17 KiB) Viewed 21646 times
Here's the circuit I use (one for each relay). Any npn transistor should work - I'm going from memory so I don't remember the resistor values but this page should have info on how to calculate values.

fruitloaf
Posts: 84
Joined: Sun Feb 05, 2012 11:41 am

Re: driving a relay

Wed Sep 19, 2012 1:39 pm

Be aware that you would normally want a diode in there too when driving a relay but the board already has one - you could use this directly with a relay with the addition of one.

rooster
Posts: 100
Joined: Thu Aug 02, 2012 8:09 pm

Re: driving a relay

Wed Sep 19, 2012 2:02 pm

hi fruitloaf

does that give the voltage input into the relay as 5V, 0V when the gpio is 0V, 3.3V?

I thought the YWRobot was that circuit. It has a resistor and transistor heading off to the relay which I assumed would work. But your circuit should turn my 3.3V into the 5V that would work?

R

rooster
Posts: 100
Joined: Thu Aug 02, 2012 8:09 pm

Re: driving a relay

Wed Sep 19, 2012 2:05 pm

hi fruitloaf

could you link to the page for the R values?

Thanks

R

techpaul
Posts: 1512
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK
Contact: Website

Re: driving a relay

Wed Sep 19, 2012 2:06 pm

rooster wrote:Hi techpaul

This is the module

http://www.ebay.co.uk/itm/2-Channel-DC- ... f3305#shId

the circuit diagram is shown towards the bottom of the page. I was connecting VCC and GND to the +5V and GND of the PI and IN0 to GPIO0. The relay works when IN0 is directly connected to the GND or +5V but wont be driven by the 3.3V of the GPIO

Russ
Because you need to 'boost' the 3V3 level to 5V to turn OFF the input on the relay card, Use something similar to fruitloaf circuit, but it depends on what the actual relay card has on its front end.

The photos of the ebay items do not match the circuits on the same page. Some show optocoupler on circuit and photo obviously does not have an opto-coupler just a transistor. Whatever type it is you need to either
  • For opto-coupler input type cards turn off(isolate) the input or take to 5V
    For transistor input types yopu must take the input to 5V to turn the card's transistor off
You can tell if you have an opto-coupler type as there will be a square black chip with FOUR silver pins on it between the inputs and relays.

Suggested driving circuits ONLY FOR THESE TYPES OF RELAY CARDS will NOT work for all relay types. This is supplied on as is basis and test by making circuit first and applying 0V then 3V3 instead of GPIO signal to prove it works first.
Attachments
Relaydrivecct.gif
Relaydrivecct.gif (12.63 KiB) Viewed 21636 times
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/

rooster
Posts: 100
Joined: Thu Aug 02, 2012 8:09 pm

Re: driving a relay

Wed Sep 19, 2012 2:07 pm

and where/which direction would the diode go on your circuit diagram?

Thanks

techpaul
Posts: 1512
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK
Contact: Website

Re: driving a relay

Wed Sep 19, 2012 2:08 pm

rooster wrote:and where/which direction would the diode go on your circuit diagram?

Thanks
Your relay cards have the diode on already.

The suggested circuits go between the GPIO output and your relay card
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/

rooster
Posts: 100
Joined: Thu Aug 02, 2012 8:09 pm

Re: driving a relay

Wed Sep 19, 2012 2:19 pm

I think my problem is that a friend recommended I get this circuit

http://www.google.co.uk/imgres?imgurl=h ... BA&dur=241

and I thought the YWRobot was that essentially (and I think the YWRobot I have is the photo rather than the circuit) - can't see any opto-thingammies!

I shall go to Maplin and try and make the transistor circuit to make my 3.3V -> 5V!

Thanks

R

rooster
Posts: 100
Joined: Thu Aug 02, 2012 8:09 pm

Re: driving a relay

Wed Sep 19, 2012 2:21 pm

techpaul wrote:
rooster wrote:and where/which direction would the diode go on your circuit diagram?

Thanks
Your relay cards have the diode on already.

The suggested circuits go between the GPIO output and your relay card
ok, thanks. if maplin has transistors at least i can get this to work this afternoon!

Thanks

R

fruitloaf
Posts: 84
Joined: Sun Feb 05, 2012 11:41 am

Re: driving a relay

Wed Sep 19, 2012 2:21 pm

rooster wrote:and where/which direction would the diode go on your circuit diagram?

Thanks

You don't need it but I believe it goes between the GPIO resistor and the transistor.

fruitloaf
Posts: 84
Joined: Sun Feb 05, 2012 11:41 am

Re: driving a relay

Wed Sep 19, 2012 2:22 pm

rooster wrote:hi fruitloaf

could you link to the page for the R values?

Thanks

R
Techpauls image has an identical circuit with resistor values - I'm pretty certain they're the ones I used.

rooster
Posts: 100
Joined: Thu Aug 02, 2012 8:09 pm

Re: driving a relay

Wed Sep 19, 2012 9:09 pm

great, thanks for both your help. i am now driving the YWRobot relay with the GPIO 3.3V jumped to 5V using the diagram above

Thanks!

techpaul
Posts: 1512
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK
Contact: Website

Re: driving a relay

Wed Sep 19, 2012 9:49 pm

Glad to hear it.

Unfortuinately those ebay relays do not appear to be well documented.
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/

User avatar
Bencom
Posts: 56
Joined: Wed Mar 07, 2012 10:40 pm
Location: New Zealand
Contact: Website

Re: driving a relay

Wed Sep 19, 2012 11:40 pm

The YwRobot relay circuit can be found here.
http://arduino-direct.com/sunshop/index ... tail&p=181
This shows that they use a pnp transistor with high side relay.
These boards will subject the RPi to 4.28 of so volts on the gpio pin it is connected to.
More threads.
http://www.raspberrypi.org/phpBB3/viewt ... 58#p175858
http://www.raspberrypi.org/phpBB3/viewt ... 84#p173684
http://www.raspberrypi.org/phpBB3/viewt ... 57#p170357
This relay may fit the RPi
http://www.aliexpress.com/item/5V-12V-o ... 69677.html
I have a YwRobot 4 Relay and although it works is is very bad for the RPi.
I have a opto module on the way but this will require a buffer just like the pnp relay.
Perhaps the relay above will be the solution. Thankfully my board survived the ordeal.

techpaul
Posts: 1512
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK
Contact: Website

Re: driving a relay

Wed Sep 19, 2012 11:47 pm

Bencom wrote:The YwRobot relay circuit can be found here.
http://arduino-direct.com/sunshop/index ... tail&p=181
Like nearly all sites including ebay ones the phot does not match the circuit.

That one shows 1 relay picture and transistor style circuit for FOUR relays.

Some of them show 8 channel with obvious opto in photo and circuit for transistors if lucky it has the same numebr of relays.
I have a YwRobot 4 Relay and although it works is is very bad for the RPi.
I have a opto module on the way but this will require a buffer just like the pnp relay.
Perhaps the relay above will be the solution. Thankfully my board survived the ordeal.
Yes you will need a buffer in simplest form on my diagrams for EACH relay.
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/

Return to “Automation, sensing and robotics”