northstory
Posts: 21
Joined: Mon Feb 25, 2013 7:53 pm
Location: United States, Utah

Question about using the RPi with a relay.

Mon Feb 25, 2013 11:21 pm

Image
Bigger Image here: http://oi49.tinypic.com/2cpoxgl.jpg

I am no electrician or engineer. Is this safe? I don't want to fry my Pi.

Orange - 3.3v
Red - 5v
Black - Ground
Green - GPIO Out's

Thanks in advance.

P.S. This is the info on the relay: http://www.sainsmart.com/4-channel-5v-r ... logic.html

User avatar
joan
Posts: 14016
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Question about using the RPi with a relay.

Mon Feb 25, 2013 11:59 pm

I'm not sure. I think perhaps not.

You seem to be applying 5V to one pin of a jumper. Without seeing the specs of the board I'm not sure what that will do.

northstory
Posts: 21
Joined: Mon Feb 25, 2013 7:53 pm
Location: United States, Utah

Re: Question about using the RPi with a relay.

Tue Feb 26, 2013 12:07 am

Joan,

Thanks for your reply. The way I understand it, if you remove the jumper you can use a separate power supply to run the relays. Since the board requires 5v and the GPIO's only put out 3.3v, I thought this would work??? Also, it looks like the schematic for the board is in one of the images of the link I posted. Here it is again: http://www.sainsmart.com/4-channel-5v-r ... logic.html

pksato
Posts: 295
Joined: Fri Aug 03, 2012 5:25 pm
Location: Brazil

Re: Question about using the RPi with a relay.

Tue Feb 26, 2013 12:52 am

Wrong connection.
And, it can destroy RPi.
But, is not your fault. It's is poor documented, and designed.
This relay board is designed to use with 5V logic, like Arduindo. And, negative activation logic.

To do correct connection, remove GND and 5V line.
Maintain rest of connections.
3v3 to VCC and GPIO to IN1 to IN4.
Get another 5V PSU, or from same PSU that power RPi, but before micro USB connection.
From extra PSU, Connect GND (0V) to GND and 5V to JD-VCC. Make this connection before to RPi, with extra PSU on, check voltage between GND and VCC on 6pin header, must be ZERO volts. If you read 5V, some wrong on documentation that I read to make this text.
Now, connect GPIO pins to RPi.
Remember, relay are activated by low level logic.
3v3, high level, relay deactivated.
0V, low level, relay activated.

edit:
I recommend to use some logic level translator, like a 74LS07 (74HC07), a hex open collector inverter.

northstory
Posts: 21
Joined: Mon Feb 25, 2013 7:53 pm
Location: United States, Utah

Re: Question about using the RPi with a relay.

Tue Feb 26, 2013 1:05 am

pksato,

Thanks for your detailed reply, it is much appreciated.

How is another external power supply any different than going out of the RPi's 5v pin to the JD-VCC? Is the isolation of the power supply what is protecting the Pi?

pksato
Posts: 295
Joined: Fri Aug 03, 2012 5:25 pm
Location: Brazil

Re: Question about using the RPi with a relay.

Tue Feb 26, 2013 1:26 am

northstory wrote:pksato,
How is another external power supply any different than going out of the RPi's 5v pin to the JD-VCC? Is the isolation of the power supply what is protecting the Pi?
Yes and No. :?:
Real reason to use external PSU, is what the 5V from GPIO don't have sufficient power to energize 4 relay coils.

northstory
Posts: 21
Joined: Mon Feb 25, 2013 7:53 pm
Location: United States, Utah

Re: Question about using the RPi with a relay.

Tue Feb 26, 2013 1:54 am

Sorry, I guess I'm still a bit confused. The 5v pin isn't enough, but a 5v power supply is? How is that different? Please forgive my ignorance :)

pksato
Posts: 295
Joined: Fri Aug 03, 2012 5:25 pm
Location: Brazil

Re: Question about using the RPi with a relay.

Tue Feb 26, 2013 2:06 am

5V from RPI GPIO header. Pin 2 or 4.
Current from this 5V is limited by F3 polyfuse.
You can't draw more that around 100mA from these pins.
Relay SRD-05VDC-SLC coil draw 71.4mA.

To power relay board, need 285.6mA. (with all relay activated)

northstory
Posts: 21
Joined: Mon Feb 25, 2013 7:53 pm
Location: United States, Utah

Re: Question about using the RPi with a relay.

Tue Feb 26, 2013 2:22 am

Ah ok, that makes more sense, thank you! However, say I would only ever need to activate one relay at a time, does my logic work in that case? (minus the fact that I drew the line to the VCC instead of the JD-VCC).

User avatar
Jim Manley
Posts: 1600
Joined: Thu Feb 23, 2012 8:41 pm
Location: SillyCon Valley, California, and Powell, Wyoming, USA, plus The Universe
Contact: Website

Re: Question about using the RPi with a relay.

Tue Feb 26, 2013 4:06 am

You also need to be careful about back-EMF pulses onto the Pi's 5 volt power bus (EMF is electro-motive force - what spins motors, activates solenoids and relays, etc.). Whenever a relay coil is deenergized, the magnetic field established in the coil collapses, inducing a potentially damaging current and/or voltage onto the supply pin and other connected devices. The GPIO pins are not protected from such surges (or anything else, for that matter) and one needs to be careful to provide buffering (as do products such as the GertBoard input-output expansion board).
The best things in life aren't things ... but, a Pi comes pretty darned close! :D
"Education is not the filling of a pail, but the lighting of a fire." -- W.B. Yeats
In theory, theory & practice are the same - in practice, they aren't!!!

timmoore46
Posts: 266
Joined: Tue Jul 17, 2012 4:36 pm

Re: Question about using the RPi with a relay.

Tue Feb 26, 2013 8:55 am

A reverse biased Diode type 1N4001 across the relay coil is vital !

the white ring on the diode must be +ve e.g. connected to the +5v supply.

Otherwise anything connected to the relay will be killed by the voltage spike.

Hope that supplies the fix.

:D

Tim

gkirke
Posts: 3
Joined: Tue Feb 26, 2013 4:57 am
Location: Canada

Re: Question about using the RPi with a relay.

Tue Feb 26, 2013 8:58 am

I agree with all the feedback you have been provided and had some other points for your consideration:

1. Remove two connections in your drawing…
You suggested in one response (~9:22 pm) that all your connections in your drawing are correct, except you needed to remove Vcc.

“minus the fact that I drew the line to the VCC instead of the JD-VCC”

Careful here…your drawing shows two ties between the pi and Relay Module…VCC and the common relay GND…which can be a path for the aforementioned EMF feedback.

Remove both the Vcc and GND connections between the Relay Module and the Rpi.

Summary…do not have any Vcc or GND connections between the pi and Relay Module.

2. Don’t trust the Relay Module Schematic…
After reviewing the schematic for the Relay Module, I would be concerned with the accuracy. It does not seem to make sense that the Relay Module header “J?” would have a pin tied to one opto isolator, rather one would expect that header “J?” would be the relay's power connection for JD-Vcc and GND?

Further thoughts, while your suggestion that relay power can be obtained from GPIO header, given only one relay will be under power at any given time, please do not consider this as a valid option and consider the following thoughts:
1. Your program locks up or is buggy which might enable more than one or all relays to energized at the same time, do you really want to service your pi?

2. What is the in-rush current (peak) that any relay might require?

3. Other risks:
a. Increase current, one of the relays develops a short or the clamping diode leaks?
b. Relay energized by board component failure, a transistor shorts and energizes one relay and maintains this state and then at some time your program enables another relay, net effect more than one relay energized simultaneously.

Consider all eventualities (the what if) for greater dependency and perhaps peace of mind… :)
Cheers,

GK

gkirke
Posts: 3
Joined: Tue Feb 26, 2013 4:57 am
Location: Canada

Re: Question about using the RPi with a relay.

Tue Feb 26, 2013 9:02 am

Sorry...quick correction to my just prior response...

Where I suggest removing both GND and Vcc, please note Vcc means JD-Vcc!!!
Cheers,

GK

0117blocky
Posts: 43
Joined: Mon Apr 30, 2012 8:03 am

Re: Question about using the RPi with a relay.

Tue Feb 26, 2013 10:04 am

The relay board is designed to run from 5v including the opto isolators, 3v3 is not enough voltage for reliable operation. There are two leds and a resistor in series (one led is inside the opto isolator.)

The stated drive current is 15 t0 20mA for each channel, which I assume is the opto isolator part. That is Too high for the the RPI to sink on its gpio pins in my opinion.

On your wiring layout you had the RPi 3v3 and 5v rails shorted via the vcc pins.

It would appear that there is a common ground, which means the input and the output circuits are not electrically isolated. This somewhat negates the advantage of using an opto isolator.

The only thing I would connect from that board to the Rpi is a ground wire and use buffers and a 5V supply.

I agree with all that's been previously stated.

User avatar
Redrobes
Posts: 80
Joined: Mon Dec 26, 2011 9:19 pm
Location: S.W. UK
Contact: Website

Re: Question about using the RPi with a relay.

Tue Feb 26, 2013 11:14 am

From the circuit diagram the opto isolators are driven from the GPIO pins so the 5V / 3.3V mismatch would only cause a difference in current through the opto isolator LED & visual LED. Its impossible to say whether 3.3V is going to trigger the opto but it has a good chance of working ok. If they did not then you could solder 4 resistors in parallel with the opto LED resistors to lower them such that more current would flow through them and definitely make that work. (See my post about selecting a resistor for an LED here -> http://www.raspberrypi.org/phpBB3/viewt ... 82#p295982). You would need to have a total resistance such that it puts 17mA through that LED. If it does it now with 5V on it then its probably got a 100 ohm resistor on it. To do that with 3.3V it would need another 100 ohm resistor in parallel with that one to make that happen. Exact values depend on the LED in the opto and the visual LED in series tho. The suggestion of putting a hex schmitt trigger IC or a logic level changer in front of the system is probably a good idea in this case to get it to 5V. If they had used only one LED in the Opto device then you might be able to skip it.

Personally I think you can drive the VCC from the Raspi VCC ok but the JD-VCC would need its own power supply. The circuit diagram shows that it does already include the back emf diodes and in any case they are on the other side of the opto isolator. I dont believe its possible that the relay coils will fry the pi.

From a safety standpoint though, if you are connecting high voltage to the other sides of the relays then it may be a requirement to not have the boards digital side powered from the Raspi VCC and have the opto isolators powered from something else then the only connection to the Pi is the LED side of the opto isolators which is the safe way to do it.

pjc123
Posts: 913
Joined: Thu Mar 29, 2012 3:37 pm
Contact: Website

Re: Question about using the RPi with a relay.

Tue Feb 26, 2013 12:51 pm

See my posts here where I supply detailed information about how this board works and an even more detailed discussion on how to safely connect it to the pi. I have been using the 8 channel version of this board for several months now (With the caveat that there are apparently several versions of this board from different manufacturers with some slight differences). I am using the Sainsmart version.:

http://www.raspberrypi.org/phpBB3/viewtopic.php?t=19222

http://www.raspberrypi.org/phpBB3/viewt ... 45&t=30790

EDIT: I see from your multiple posts that it is indeed a Sainsmart relay; this is why you should not double post.
My Raspberry Pi Project Page:
https://www.flaminghellmet.com/launch/

northstory
Posts: 21
Joined: Mon Feb 25, 2013 7:53 pm
Location: United States, Utah

Re: Question about using the RPi with a relay.

Tue Feb 26, 2013 3:18 pm

Ok, sorry guys, I'm really an electronics n00b. Pictures do me way better as well. Would one of you be kind enough to draw it on my picture? Thanks in advance.

Image

northstory
Posts: 21
Joined: Mon Feb 25, 2013 7:53 pm
Location: United States, Utah

Re: Question about using the RPi with a relay.

Tue Feb 26, 2013 3:27 pm

This is how I interpreted pksato's directions. Is this correct?

Image

I'm also still finding mixed information all over: This blog post makes me think I can still accomplish this without the external power supply: https://coderwall.com/p/izzsig but is he just doing it wrong and still putting his Pi in danger?

pjc123
Posts: 913
Joined: Thu Mar 29, 2012 3:37 pm
Contact: Website

Re: Question about using the RPi with a relay.

Tue Feb 26, 2013 4:02 pm

northstory wrote:This is how I interpreted pksato's directions. Is this correct?

Image

I'm also still finding mixed information all over: This blog post makes me think I can still accomplish this without the external power supply: https://coderwall.com/p/izzsig but is he just doing it wrong and still putting his Pi in danger?
Northstory. I guess you didn't read my posts, although I understand you are new to electronics and it may just be gibberish. I went through the same pains as you with tons of incorrect information everywhere, and that is why I had to do some experimenting with the board myself.

pksato wrote: 3v3 to VCC and GPIO to IN1 to IN4.

Won't work. This is what I originally tried many months ago. Between the 1k resistor, in line with the optosisolator's LED, in line with the external LED, there just isn't enough voltage to overcome all of that and reliably light the opto's LED in order to turn on its internal transistor sufficiently. Sometimes the relay works, other times it does not. You need 5V for Vcc, however you need to isolate that from the GPIO with a transistor, darlington pair, etc., so Northstory, if that is something that you do not want to do, then do it at your own risk; it may or may not damage the pi as explained in the threads I pointed to. As far as an external power supply, it is only necessary if you are turning on multiple relays, and depends on how much current the rest of the pi is drawing. Each relay coil takes about 60ma, so doing the math, it adds up very quick. I have situations where I turn on all 8 relays simultaneously, so I have a separate 5V power supply for JD-Vcc.
My Raspberry Pi Project Page:
https://www.flaminghellmet.com/launch/

northstory
Posts: 21
Joined: Mon Feb 25, 2013 7:53 pm
Location: United States, Utah

Re: Question about using the RPi with a relay.

Tue Feb 26, 2013 4:34 pm

pjc123 wrote:
Northstory. I guess you didn't read my posts, although I understand you are new to electronics and it may just be gibberish. I went through the same pains as you with tons of incorrect information everywhere, and that is why I had to do some experimenting with the board myself.

pksato wrote: 3v3 to VCC and GPIO to IN1 to IN4.

Won't work. This is what I originally tried many months ago. Between the 1k resistor, in line with the optosisolator's LED, in line with the external LED, there just isn't enough voltage to overcome all of that and reliably light the opto's LED in order to turn on its internal transistor sufficiently. Sometimes the relay works, other times it does not. You need 5V for Vcc, however you need to isolate that from the GPIO with a transistor, darlington pair, etc., so Northstory, if that is something that you do not want to do, then do it at your own risk; it may or may not damage the pi as explained in the threads I pointed to. As far as an external power supply, it is only necessary if you are turning on multiple relays, and depends on how much current the rest of the pi is drawing. Each relay coil takes about 60ma, so doing the math, it adds up very quick. I have situations where I turn on all 8 relays simultaneously, so I have a separate 5V power supply for JD-Vcc.
I actually did read through both of those post's long before I posted this... the problem is, you're right, a lot of it is simply over my head. Would you be kind enough to walk me through exactly what I need to buy and how to wire it up so I can do this the RIGHT way? It seems like there are mixed feelings in the posts you mentioned about what is right and wrong still. But you seem to have it figured out. In the mean time, I've been reading the book "Make: Electronics" to try and understand how it all works and give me at least a basic knowledge of this stuff. Thanks for your reply and patience with n00bies like myself.

pjc123
Posts: 913
Joined: Thu Mar 29, 2012 3:37 pm
Contact: Website

Re: Question about using the RPi with a relay.

Tue Feb 26, 2013 4:58 pm

northstory wrote: I actually did read through both of those post's long before I posted this... the problem is, you're right, a lot of it is simply over my head. Would you be kind enough to walk me through exactly what I need to buy and how to wire it up so I can do this the RIGHT way? It seems like there are mixed feelings in the posts you mentioned about what is right and wrong still. But you seem to have it figured out. In the mean time, I've been reading the book "Make: Electronics" to try and understand how it all works and give me at least a basic knowledge of this stuff. Thanks for your reply and patience with n00bies like myself.
I just don't have the time right now, but if I get a break I can try and whip something up. Where are you located (Country) and where would you buy parts from (online, local store like Radio Shack, etc.). Would you be doing this on a breadboard (easiest), or would you want to solder it all up so it is transportable? Do you just want to use the pi's power supply and maybe just activate one relay at a time? What kind of funds do you have to spend (As in real electonic equipment and parts, average stuff or ebay junk)? Then there is the whole software aspect that you will need to learn in order to active the GPIO ports, unless you have done that already.
My Raspberry Pi Project Page:
https://www.flaminghellmet.com/launch/

northstory
Posts: 21
Joined: Mon Feb 25, 2013 7:53 pm
Location: United States, Utah

Re: Question about using the RPi with a relay.

Tue Feb 26, 2013 5:12 pm

pjc123 wrote: I just don't have the time right now, but if I get a break I can try and whip something up. Where are you located (Country) and where would you buy parts from (online, local store like Radio Shack, etc.). Would you be doing this on a breadboard (easiest), or would you want to solder it all up so it is transportable? Do you just want to use the pi's power supply and maybe just activate one relay at a time? Then there is the whole software aspect that you will need to learn in order to active the GPIO ports, unless you have done that already.

I am in the US in Utah. I could go to radio shack if need be or just order parts off amazon or ebay. I already have a breadboard, some resistors and an LED or two. I would eventually want to solder it up, but starting on the breadboard will most likely help me to understand what's going on the best. I am completely fine with using another external power supply if it gives me added flexibility. If that complicates things, then I'd be happy to just start by using the Pi's and never activating more than one relay at a time. As far as the software side is concerned, there are no issues there. I have a LAMP environment up and running already with a web page that can successfully flip the GPIO pins as needed. I'm a computer science graduate and a web developer by day so that part of it comes relatively easy for me. I just feel like a complete moron with the electronics side. I really do appreciate your help and time!

Edit: Budget wise, there's not really any concerns besides justifying the cost.

pjc123
Posts: 913
Joined: Thu Mar 29, 2012 3:37 pm
Contact: Website

Re: Question about using the RPi with a relay.

Tue Feb 26, 2013 5:35 pm

northstory wrote:
pjc123 wrote: I just don't have the time right now, but if I get a break I can try and whip something up. Where are you located (Country) and where would you buy parts from (online, local store like Radio Shack, etc.). Would you be doing this on a breadboard (easiest), or would you want to solder it all up so it is transportable? Do you just want to use the pi's power supply and maybe just activate one relay at a time? Then there is the whole software aspect that you will need to learn in order to active the GPIO ports, unless you have done that already.

I am in the US in Utah. I could go to radio shack if need be or just order parts off amazon or ebay. I already have a breadboard, some resistors and an LED or two. I would eventually want to solder it up, but starting on the breadboard will most likely help me to understand what's going on the best. I am completely fine with using another external power supply if it gives me added flexibility. If that complicates things, then I'd be happy to just start by using the Pi's and never activating more than one relay at a time. As far as the software side is concerned, there are no issues there. I have a LAMP environment up and running already with a web page that can successfully flip the GPIO pins as needed. I'm a computer science graduate and a web developer by day so that part of it comes relatively easy for me. I just feel like a complete moron with the electronics side. I really do appreciate your help and time!

Edit: Budget wise, there's not really any concerns besides justifying the cost.
Oh OK, then this will not be a big deal. Sometimes I am helping out kids, so it helps to know what level I am dealing with, and you already have a breadboard, so it makes it easy. I will make something up today or tomorrow. I got most of my parts from Mouser Electronics online (I am also in the US and they are a supplier here), so if you just want to do that, I can also give you the exact part numbers as well.
My Raspberry Pi Project Page:
https://www.flaminghellmet.com/launch/

northstory
Posts: 21
Joined: Mon Feb 25, 2013 7:53 pm
Location: United States, Utah

Re: Question about using the RPi with a relay.

Tue Feb 26, 2013 5:49 pm

pjc123 wrote: Oh OK, then this will not be a big deal. Sometimes I am helping out kids, so it helps to know what level I am dealing with, and you already have a breadboard, so it makes it easy. I will make something up today or tomorrow. I got most of my parts from Mouser Electronics online (I am also in the US and they are a supplier here), so if you just want to do that, I can also give you the exact part numbers as well.
I'm happy to order from Mouser so that there is no inconsistency with what you tell me to get vs. what I actually get. Thanks again for your time, I hope to be able to pass on the wisdom with others who are trying to accomplish similar objectives.

And seriously... sometimes those kids piss me off for all the wrong reasons :arrow: http://www.huffingtonpost.com/2011/01/1 ... 10023.html

but I suppose I might as well be a kid when it comes to the electronics side of things :oops:

User avatar
joan
Posts: 14016
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Question about using the RPi with a relay.

Tue Feb 26, 2013 5:57 pm

I haven't double-checked but have you said what the relays will be used for?

For low voltage/amperage/DC solutions I'd use a ULN2003A (GBP 0.40).

Return to “General discussion”