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

Re: Safely Interfacing 8 Relay Module via GPIO

Wed Jan 23, 2013 11:24 pm

gordon@drogon.net wrote:
pjc123 wrote:Sorry I brought it up. I was just wondering of these claims of people directly connecting the board to the pi without additional circuitry. They must be sinking into the pi, and yes, as I suspected that is bad. Outputting a "LOW' or "HIGH" to the transistor from the GPIO works fine for me; it has been running for several months now, so I am good.
Ah ok. Thought you were in the middle of building it up.

Glad it's working OK.

-Gordon
The part I am building now is transferring that tested working circuit from a prototype breadboard to one of those pre-padded copper circuit cards so I can bolt it into a chassis with a bunch of other stuff (connectors, fuses, small lambda switching power supply, 12V to 5V convertor, fan, various switches, pi, relays, external wifi connector, bunch of LED's, etc).
My Raspberry Pi Project Page:

https://www.flaminghellmet.com/launch/

deathtrap
Posts: 7
Joined: Wed Jan 23, 2013 2:57 am

Re: Safely Interfacing 8 Relay Module via GPIO

Wed Jan 23, 2013 11:47 pm

There are a few different theories out there, is running the GPIO outputs into a breadboard, through a ULN2803 and out to the relay module safe practice? I found this diagram and explanation which helps make some sense of all this. http://www.petervis.com/Raspberry_PI/Ra ... acing.html

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

Re: Safely Interfacing 8 Relay Module via GPIO

Thu Jan 24, 2013 12:10 am

deathtrap wrote:There are a few different theories out there, is running the GPIO outputs into a breadboard, through a ULN2803 and out to the relay module safe practice? I found this diagram and explanation which helps make some sense of all this. http://www.petervis.com/Raspberry_PI/Ra ... acing.html
Instead of using a single transistor, the ULN2803 uses a darlington transistor pair; a bit overkill for the relay card, but it is doing the exact same thing that I am doing with a single transistor. Pricewise the ULN2803 is approximately double the price of 8 individual transistors (depends on the transistor). The advantage is that you don't have to deal with individual transistors because everything is in one package and just easier to wire up on a breadboard. Keep in mind that there is a 2.7k ohm resistor built into each input of that chip, so other than the 10K ohm additional resistor from each GPIO input to ground that Gordon recommended, you don't need any other resistors.
My Raspberry Pi Project Page:

https://www.flaminghellmet.com/launch/

User avatar
aTao
Posts: 1093
Joined: Wed Dec 12, 2012 10:41 am
Location: Howlin Eigg

Re: Safely Interfacing 8 Relay Module via GPIO

Thu Jan 24, 2013 6:51 am

For this particular application you would never see 5V0 at the RPi GPIO pin.
There are 2 diodes and a resistor between the power rail and the input.
So 5V0 - 2 diode drops clamped to 3V3 by the RPi inputs all through a 1k resistor wont push enough to fry your RPi.
>)))'><'(((<

User avatar
alexeames
Forum Moderator
Forum Moderator
Posts: 2869
Joined: Sat Mar 03, 2012 11:57 am
Location: UK
Contact: Website

Re: Safely Interfacing 8 Relay Module via GPIO

Thu Jan 24, 2013 10:39 am

gordon@drogon.net wrote:I do wonder about the "belt & braces" approach here though - opto isolators *and* relays. Since when did relays stop becoming good isolators between high voltage and low voltage systems.. Ah well!
Since all risk became unacceptable :lol:
Alex Eames RasPi.TV, RasP.iO

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

Re: Safely Interfacing 8 Relay Module via GPIO

Thu Jan 24, 2013 1:00 pm

aTao wrote:For this particular application you would never see 5V0 at the RPi GPIO pin.
There are 2 diodes and a resistor between the power rail and the input.
So 5V0 - 2 diode drops clamped to 3V3 by the RPi inputs all through a 1k resistor wont push enough to fry your RPi.
I suspected that the reason would be something like that, although I am sure it was not by design, and people are not damaging their pi strictly due to "dumb luck". I also imagine it would be best practice not to rely on this.
My Raspberry Pi Project Page:

https://www.flaminghellmet.com/launch/

User avatar
abishur
Posts: 4477
Joined: Thu Jul 28, 2011 4:10 am
Location: USA
Contact: Website

Re: Safely Interfacing 8 Relay Module via GPIO

Thu Jan 24, 2013 2:54 pm

pjc123 wrote:
aTao wrote:For this particular application you would never see 5V0 at the RPi GPIO pin.
There are 2 diodes and a resistor between the power rail and the input.
So 5V0 - 2 diode drops clamped to 3V3 by the RPi inputs all through a 1k resistor wont push enough to fry your RPi.
I suspected that the reason would be something like that, although I am sure it was not by design, and people are not damaging their pi strictly due to "dumb luck". I also imagine it would be best practice not to rely on this.
And this is the only point I've been attempting (successfully or not ;-) ) to make. If something says it is going to be sending 5V into your pi, it's better not to trust some diodes to drop it down to a safe range for the pi, especially if the next board revision only comes with a single diode in the mix keeping the voltage up in the 4v3-4v5 range. It is better to treat it as if the maximum possible voltage is going to go into the pi at some point and put the appropriate protection in place.

And I'm not saying go paranoid nutso here :-P There's no need to have three layers of opto-isolation and then a darlington transistor array, just some real basic protection in the very likely event that you were the lucky person who got a bad component on the board and got too high a voltage going into their pi when everyone else got off scotch free .
Dear forum: Play nice ;-)

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

Re: Safely Interfacing 8 Relay Module via GPIO

Thu Jan 24, 2013 3:44 pm

abishur. wrote:... It is better to treat it as if the maximum possible voltage is going to go into the pi at some point and put the appropriate protection in place.
I say better to match the spec at the interface point and know it is being used correctly.
And I'm not saying go paranoid nutso here :-P There's no need to have three layers of opto-isolation and then a darlington transistor array, just some real basic protection in the very likely event that you were the lucky person who got a bad component on the board and got too high a voltage going into their pi when everyone else got off scotch free .
The converse argument is do not put too many layers in as the layers can cause problems and the more components and connections gives you more points of failure.

Covering a single fault and some double fault conditions is good practise, beyond that you are trying to protect from -

"it being a Wednesday, high solar radiation, cat jumping on the shelf knocking off a box of paper clips and a glass of water onto the circuit" :lol:
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/

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

Re: Safely Interfacing 8 Relay Module via GPIO

Thu Jan 24, 2013 3:45 pm

I originally brought this topic up because for over half a year now, when people ask me how to hook up the relay card I tell them to do it with a transistor. But then I get from some people "I saw on the Internet that people were hooking it directly up to the pi, or Arduino, or whatever". I suspect for some it is the extra work, cost, lack of technical know how or laziness that stops them from hooking things up properly. Add to that the complication of slightly different variations on the relay board.

So I want to thank everyone, so they can hear it from others than me, so when it comes up again I can simply point them to this thread. Then they can make their own decision. Like someone said, its your pi, and you can do whatever you want with it; the foundation would love to sell you more units.

Time to get back to soldering.
My Raspberry Pi Project Page:

https://www.flaminghellmet.com/launch/

smanders
Posts: 6
Joined: Sun Jun 24, 2012 2:15 pm

Re: Safely Interfacing 8 Relay Module via GPIO

Fri Feb 01, 2013 9:43 pm

So I am trying to understand how the GPIO (in) would be syncing 5V. I have put together a quick circuitlab.com layout to try and simulate it. I know its probably not right. What are your suggestions to tweak it? By the way, I found the opticoupler from another circuitlab project. The behavior of the opticoupler is the lower circuit, then tied into the upper circuit through the "custom" chip.

Let me know your thoughts.

https://www.circuitlab.com/circuit/6dr2 ... -isolator/

Thanks,
smanders

fixedd
Posts: 3
Joined: Sun Feb 17, 2013 6:07 pm

Re: Safely Interfacing 8 Relay Module via GPIO

Wed Mar 27, 2013 4:58 am

For what it's worth, I laid out a board to drive the non-SSR version using transistors to pull the input pins to ground. The schematic/layout is available at https://github.com/fixedd/RPi_SainSmart_Interface if anyone is still interested.

User avatar
gordon@drogon.net
Posts: 2020
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: Safely Interfacing 8 Relay Module via GPIO

Wed Mar 27, 2013 7:22 am

fixedd wrote:For what it's worth, I laid out a board to drive the non-SSR version using transistors to pull the input pins to ground. The schematic/layout is available at https://github.com/fixedd/RPi_SainSmart_Interface if anyone is still interested.
Personally, I think you should have just made one board with the relays/SSRs on it yourself. You're building a circuit to interface the Pi to a relay board designed for 5v arduinos - just make a new relay board designed to work at 3.3v.

-Gordon
--
Gordons projects: https://projects.drogon.net/

Hempel
Posts: 1
Joined: Sun Apr 14, 2013 11:26 am

Re: Safely Interfacing 8 Relay Module via GPIO

Sun Apr 14, 2013 1:21 pm

fixedd wrote:For what it's worth, I laid out a board to drive the non-SSR version using transistors to pull the input pins to ground. The schematic/layout is available at https://github.com/fixedd/RPi_SainSmart_Interface if anyone is still interested.
Hi, I'm not sure if I'm getting this right so please correct me if I'm wrong but doesn't this project kind of do the same as a ULN2803 would do?

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

Re: Safely Interfacing 8 Relay Module via GPIO

Mon Apr 15, 2013 11:49 am

Hempel wrote:
fixedd wrote:For what it's worth, I laid out a board to drive the non-SSR version using transistors to pull the input pins to ground. The schematic/layout is available at https://github.com/fixedd/RPi_SainSmart_Interface if anyone is still interested.
Hi, I'm not sure if I'm getting this right so please correct me if I'm wrong but doesn't this project kind of do the same as a ULN2803 would do?
As was mentioned many times before..........Yes.
My Raspberry Pi Project Page:

https://www.flaminghellmet.com/launch/

bocifus
Posts: 1
Joined: Wed Mar 04, 2015 3:07 am

Re: Safely Interfacing 8 Relay Module via GPIO

Wed Mar 04, 2015 5:22 am

First of all - thanks! All of you have really opened up new worlds. I hope this thread is still active. Anyway - I have just embarked on this little project using all of the same (or at least similar) hardware. I have gotten it going in several configurations, all of which I'm sure are not recommended (I've read the 3.3v to 5v issue on several of the threads).

I've never really done circuits before, and have only ever really worked in software userland. That said, forgive me if my questions seem silly.

Gordon mentioned the ULN2803 (which I think is something like this: http://www.mouser.in/Semiconductors/Dis ... yvZ1z0y4di ). Let me start off with my goals:
I'm trying to switch AC 230v (Euro) power via the pi and some temp/RH sensors. I've not fried my pi yet (fingers crossed), however this thread is interesting because I want to go on holiday and make sure my winter garden is taken care of (and be lazy when I get back). Also, I figured the opto-couplers were in there because of the AC / DC switching.

Anyway, in the data sheet, it says, "the ULN2308A device has a 2.7 kOhm series based resistor for each Darlington pair for operations with TTL or 5v CMOS devices."

Perhaps I'm getting hung up on the mentioning of 5v CMOS things, but ... if the GPIO pin is 3.3v, does that mean anything - e.g. if the pin isn't high, is there 5v going to it? Or, does that just mean that it will be 1.22 mA going from the GPIO pin to ground (the setting the pin to low to turn on the relay confuses me)?

I suppose I'm also worried about drawing too much current from the GPIO pins. Is that something to be concerned about? There was a thread about this which I can't find anymore, but in it, iirc, it was mentioned there's a max limit on the current from GPIOs - something to the effect of 15-20 mA, w/ some 50 or or 60 for the whole "bank" [which I assume is 1-17].

So, the ULN2308 is there to separate the 3.3v from 5v and to amplify the current from the GPIO pin to the optocoupler (x8)?

hopefully all this makes sense.
thanks

mcy
Posts: 4
Joined: Fri Jun 19, 2015 8:33 am

Re: Safely Interfacing 8 Relay Module via GPIO

Fri Jun 19, 2015 9:19 am

Apparently there are quite a few of these modules which look "exactly alike". I suspect they would all have similar electrical properties, but you can run a simple test to see if they are safe to connect to a Pi directly. This is what the "Pi side" of circuitry of the module looks like:

5V ------/\/\/\------|>|------|>|------- control pin

There are two forward biased diodes (the LED and the one in the optocoupler) and a resistor (1K in my case) in the path from the 5V supply to the pin for connection to the Pi. The diodes seem to cause a sufficient voltage drop to make the connection safe.

o Connect a 5V supply to the module.

o Measure the voltage between any one of the 8 control pins (for connection to the Pi outputs) to the ground. In my case this was 2.8V, indicating that a total of 2.2V remain as the junction potentials on the diodes. (LEDs and the optocoupler LEDs -probably a GaAs version- have larger junction voltages than ordinary diodes.) This 2.8V is the highest potential that this pin will see, unless current is forced *into* the module - even then, it will be limited by the voltage produced by Pi.

o Next, set your multimeter into the current measuring mode and measure the current from this pin to the ground. You will notice that the relay trips, and the LED will light up. This corresponds to the maximum current that the Pi pin will have to sink. In my case this is about 2ma. (There is a 1K resistor in the path - there is no way this current can be 20ma. Other interesting things would happen if a 20ma current were to flow through a LED so small.)

So, for *my* module, I saw no reason why not to connect it directly to the Pi pins directly. Obviously, now a "0" on the pin means "on" and a "1" (or tri-state) on the pin means "off".

Regards - mcy.

remy_david
Posts: 3
Joined: Thu Dec 26, 2013 4:40 pm

Re: Safely Interfacing 8 Relay Module via GPIO

Sun Mar 06, 2016 2:36 pm

I did those measures on my 2 relay sainsmart board.
Turns out, the voltage drop is only 1.2V (4.1V at input with 5.3V at Vcc), and the current is 2mA, so I wonder if I can connect it directly. Will 4.1V@2mA damage the GPIO ?

Return to “HATs and other add-ons”