aogriffiths
Posts: 3
Joined: Fri May 09, 2014 8:49 am

Re: How to wire a Raspberry Pi to a Sainsmart 5v Relay Board

Thu May 15, 2014 12:22 pm

Hi All

Just a quick note to say I found this thread very useful. I've done a similar design myself, used the Raspberry Pi to drive a two sainsmart 5V relays. My design is largely based on Mike's drawings and the comments here. I'd love to get your feedback, it's described in this post:

http://www.raspberrypi.org/forums/viewt ... 01#p550901

Regards
Adam

techkilljoy
Posts: 1
Joined: Mon May 19, 2014 7:50 pm

Re: How to wire a Raspberry Pi to a Sainsmart 5v Relay Board

Mon May 19, 2014 7:52 pm

Hello! I have some students very interested in this project but Im no EE and the conceptual breadboard wiring has me a little stumped. Any way to post a photo of what the physical board looks like?

mchlwllmthms
Posts: 36
Joined: Tue Mar 05, 2013 11:42 pm

Re: How to wire a Raspberry Pi to a Sainsmart 5v Relay Board

Mon May 19, 2014 11:07 pm

techkilljoy wrote:Hello! I have some students very interested in this project but Im no EE and the conceptual breadboard wiring has me a little stumped. Any way to post a photo of what the physical board looks like?
Here's a photo of what my finished board looked like and tried to annotate it.

Image

I ran +5V/Ground from the powersupply to this board (where it says "GND & +5V HERE"), and then used the two headers to output power to the Raspberry Pi (I had soldered some headers at TP1 & TP2 on the Pi so I could power it without having to use the Micro USB port).

What you can't see is that the leads of the 2.2k and 10k resistors at each input are bent towards each other and soldered together.

Anyways, I hope that helps. Let me know if you have any questions.

User avatar
Richard-TX
Posts: 1549
Joined: Tue May 28, 2013 3:24 pm
Location: North Texas

Re: How to wire a Raspberry Pi to a Sainsmart 5v Relay Board

Wed May 21, 2014 3:51 am

I am not sure why everyone has problems with the SS relay boards. I drive them directly from either the Pi or from a MCP23017 without issue. I do jumper out the silly LEDs on the board but other than that they work flawlessly.
Richard
Doing Unix since 1985.
The 9-25-2013 image of Wheezy can be found at:
http://downloads.raspberrypi.org/raspbian/images/raspbian-2013-09-27/2013-09-25-wheezy-raspbian.zip

explainer
Posts: 9
Joined: Tue Mar 25, 2014 7:50 pm

Re: How to wire a Raspberry Pi to a Sainsmart 5v Relay Board

Sun May 25, 2014 2:40 pm

I have a similar setup, just that I am using 2 of the 4-relay Sainsmart boards. I will study your schematic closely and try to map that to my system (and my brain)... I get brain fade when I try to get into hardware.

I am much more competent in software than hardware. I have built a Ruby-on-Rails web app, WaterMan, which manages all the scheduling of sprinkling cycles. It is working fine in simulation, turning LEDs on/off on a breadboard while following the schedule. I can access the web app from any host in my home network, and am planning to add a domain name so that I can access the system remotely. If you want access to the code, I can provide the github url.

BStewartGT
Posts: 1
Joined: Tue Jun 17, 2014 4:01 pm

Re: How to wire a Raspberry Pi to a Sainsmart 5v Relay Board

Tue Jun 17, 2014 4:18 pm

Based on my review of the Sainsmart (or similar Amazon available relay boards) I have confirmed there is one other, safe, and arguably more simple solution:
- Wiring GPIO directly to the relay board (while some boards may work) is a bad idea since there will not be enough voltage to fully bias the octo-coupler.
- Use of an external transistor or Darlington pair is one viable solution, but is a painful addition to what should be a turn-key singleboard solution.

--> The other, and best solution I would argue, is to solder a shorting wire across each of the LED's on your relay board AND using a separate 5vdc source to JD-VCC and tie the RPi ground to the separate 5vdc source (doing BOTH of these are required). The RPi GPIO will supply 3.3vdc over a 1k resister + octocoupler + red led. If you work the math, shorting the led out will recoup enough voltage drop to now apply about 1.2volts across the octocoupler and 2.1v across the 1k Resister=> only about 4mW of power across that R, which is still completely fine for it. In short, you lose the visual signaling of the leds on the board, but your RPi's 3.3vdc will now be enough to switch on the octocoupler (and thus the Relay) adequately. You must also be sure to use a Seperate 5vdc source to the JD-VCC pin- this will ensure isolation from the RPi, and be able to handle the current for the relays- i.e. 4 relay board in my case took nearly 1/2 an amp if all 4 are turned on at the same time- THIS will be an issue for your RPi and its power supply... and besides, you negate the benefit of the optocoupler otherwise. This mod is very simple, clean, and easy. Hope this helps

Molmol
Posts: 1
Joined: Thu Jul 24, 2014 9:52 pm

Re: How to wire a Raspberry Pi to a Sainsmart 5v Relay Board

Thu Jul 24, 2014 9:59 pm

Is it possible to send the link to the google doc again? The doc seems to be missing --

Thank you!!!

mchlwllmthms
Posts: 36
Joined: Tue Mar 05, 2013 11:42 pm

Re: How to wire a Raspberry Pi to a Sainsmart 5v Relay Board

Thu Jul 24, 2014 10:56 pm

Molmol wrote:Is it possible to send the link to the google doc again? The doc seems to be missing --

Thank you!!!
The document is still there and shows it is available publicly...
https://drive.google.com/file/d/0B5-HND ... sp=sharing

If that doesn't work, let me know and I can e-mail you the PDF.

gstein
Posts: 4
Joined: Sat Aug 02, 2014 8:36 am

Re: How to wire a Raspberry Pi to a Sainsmart 5v Relay Board

Sat Aug 02, 2014 8:50 am

mchlwllmthms wrote:Well, you could connect your GPIO pins directly to the sainsmart headers without any intermediary circuit, which should work. Others may argue, however, that you should still put a 10k pull-down resistor from each GPIO pin to ground between the GPIO and the relay board to prevent triggering the relays in unknown states. I.e., without a pulldown resistor, it may cause some of the relays to temporarily activate during booting of the RPi. Again, I'm not 100% on whether that's necessary or not, but that's my guess, and it wouldn't hurt to add.
The board inputs are active LOW, so that pull-down resistor will trip the relay(!)

If you simply connect the GPIO pins to the relay board, then a floating GPIO condition will NOT provide current to the optoisolator, and cannot trip the relay. No pull-down or pull-up resistors are needed.

Do the direct connection to simplify the hardware, and tweak the software to invert the bits that are delivered to the GPIO. Much easier!

Cheers,
-g

pasivnyprijem
Posts: 7
Joined: Wed Sep 10, 2014 7:13 am

Re: How to wire a Raspberry Pi to a Sainsmart 5v Relay Board

Mon Sep 22, 2014 6:49 pm

Hi Folks

Im dead already, I did the wiring the 5v and ground I got from an external USB (5v). NPN trasistor I used is 2N2222A and the fun comes when I want to activate the relay the control led on the relay turns weakly on and nothing happens. No idea whats going on anybody its like the NPN would somehow limit the flow from the colletor to the emitter (when I connect manualy those 2 points the relay works just fine). operating voltage as described already 2.7v around the NPN.

User avatar
Richard-TX
Posts: 1549
Joined: Tue May 28, 2013 3:24 pm
Location: North Texas

Re: How to wire a Raspberry Pi to a Sainsmart 5v Relay Board

Tue Sep 23, 2014 7:50 am

If you jumper out the LEDs on the relay board it will work better.
Richard
Doing Unix since 1985.
The 9-25-2013 image of Wheezy can be found at:
http://downloads.raspberrypi.org/raspbian/images/raspbian-2013-09-27/2013-09-25-wheezy-raspbian.zip

pasivnyprijem
Posts: 7
Joined: Wed Sep 10, 2014 7:13 am

Re: How to wire a Raspberry Pi to a Sainsmart 5v Relay Board

Tue Sep 23, 2014 8:02 am

thanks richard dont think that should be the best way :) I was thinking that the GPIO is not providing enough power for the NPN but I measured it as well its providing 3.3V so the only idea is that NPN's are defect (but all 4 pcs?) or Im using the wrong NPN's 2N2222A and its not switching correctly the emmiter and collector

skunkbad
Posts: 6
Joined: Mon Feb 23, 2015 4:14 am

Re: How to wire a Raspberry Pi to a Sainsmart 5v Relay Board

Thu Mar 05, 2015 5:32 am

I realize this is an old thread, but have some questions.

The image showing the correct way to use the SainSmart relay names the transistor as 2N222, but the radio shack link is to an assortment of transistors, and 2N222 is not included. What would be the best transistor to use from radio shack?

Is the image wrong, and the transistor should be 2N2222? 2N2222 is actually one of the transistors in the assortment.

gstein
Posts: 4
Joined: Sat Aug 02, 2014 8:36 am

Re: How to wire a Raspberry Pi to a Sainsmart 5v Relay Board

Thu Mar 05, 2015 11:40 pm

Yup, use that one. The 2N2222 is a general purpose transistor, perfectly suited for this application. You can pick them up at SparkFun: https://www.sparkfun.com/products/12852 ... or pretty much everywhere else. I bet you could find them really cheap on eBay (I source a lot of parts from Chinese vendors via eBay).

gstein
Posts: 4
Joined: Sat Aug 02, 2014 8:36 am

Re: How to wire a Raspberry Pi to a Sainsmart 5v Relay Board

Thu Mar 05, 2015 11:48 pm

I continue to believe the easiest method is to recognize that the Sainsmart inputs are ACTIVE LOW. And the RPi GPIOs can easily sink enough current. You'll need a separate 5V supply as noted elsewhere, to provide enough power for the relay coils, and you can use this as the high-side of the optoisolators. On the RPi, enable the GPIO LOW to turn ON the relay, and disable the GPIO (leave it in high impedance) to turn OFF the relay.

In my use of these boards, I have a microcontroller running at 5V, so it's all the same supply. But as long as you tie the RPi's ground to the 5V supply's ground, then this approach works. You don't have to modify the relay board, so you will get your indicator lights, and you don't need any extra parts (transistors).

User avatar
blachanc
Posts: 451
Joined: Sat Jan 26, 2013 5:03 am
Location: Quebec,canada(french)

Re: How to wire a Raspberry Pi to a Sainsmart 5v Relay Board

Thu Mar 12, 2015 3:12 pm

gstein wrote:I continue to believe the easiest method is to recognize that the Sainsmart inputs are ACTIVE LOW. And the RPi GPIOs can easily sink enough current.
I tend to agree with you, and even the spec seems to say the same thing. But the interface to PI proposed by sainsmart does uses external transistors. How confusing :(

What I am contemplating as a project is:
Instructables Web-Controlled-8-Channel-Powerstrip

official 8-channel-dc-5v sainsmart link

8 Relay SPEC Module.rar
Attachments
sainsmart.jpg
Spec snapshot
sainsmart.jpg (54.01 KiB) Viewed 6771 times
Autism/Asperger syndrome: what is your score on this quiz?
http://www.raspberrypi.org/forums/viewtopic.php?f=62&t=70191

anathanatoid
Posts: 5
Joined: Wed Dec 25, 2013 8:19 pm

Re: How to wire a Raspberry Pi to a Sainsmart 5v Relay Board

Sat Apr 04, 2015 5:18 pm

Because I always burn myself when I use a soldering iron, I went CS and not EE, so anything electrical that doesn't have three prongs is pretty much a mystery to me.

Before I forget to say it, this is a great post -- very clear and informative. Thanks for taking the time to put it all together.

I have two questions:

1. Is it possible to use the built-in pull-up/pull-down resistors on the GPIO pins to remove the need for the 10K ohm resistors?

2. 2N222's seem to be obsolete -- I'm considering either 2N3904 or 2N2222A as a replacement -- any problems with that?

Thanks again.

gstein
Posts: 4
Joined: Sat Aug 02, 2014 8:36 am

Re: How to wire a Raspberry Pi to a Sainsmart 5v Relay Board

Sat Apr 04, 2015 8:57 pm

anathanatoid wrote:...
I have two questions:

1. Is it possible to use the built-in pull-up/pull-down resistors on the GPIO pins to remove the need for the 10K ohm resistors?

2. 2N222's seem to be obsolete -- I'm considering either 2N3904 or 2N2222A as a replacement -- any problems with that?

Thanks again.
1. That would not be advisable. Consider when your RPi first boots up. The GPIO pins will be a High-Z state until you get a chance to configure them. The transistor could turn on or off the relay, you can't be sure. Worse, it could flop around randomly flipping the relay. The 10k resistor ensures the relay remains OFF until the RPi specifically turns it on.

2. As I stated in my March 5 post, there must have been a typo. The 2N2222 is what you want.

And note that since you have a separate 5V supply, there is a *much* easier way to connect your relay board to your RPi. See my earlier posts, where you simply turn on the relay by setting the GPIO to a LOW state (this seems to confuse people, so they throw transistors at the problem, to keep "GPIO HIGH means relay ON"). No additional parts are needed when using ACTIVE LOW logic.

Cheers,
-g

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

Re: How to wire a Raspberry Pi to a Sainsmart 5v Relay Board

Mon Jun 22, 2015 9:33 pm

I have a post on this topic on a different thread, if you want to take a look:

viewtopic.php?f=45&t=30790&p=776884#p776884

Regards, mcy.

BNZ
Posts: 9
Joined: Tue Jun 30, 2015 9:22 pm

Re: How to wire a Raspberry Pi to a Sainsmart 5v Relay Board

Tue Jun 30, 2015 9:27 pm

Hi All.

I am a complete electronics noob and i have found this post incredibly useful as I have a 4 channel sainsmart 5v relay that I could only trigger one channel via 3.3v.

I have replicated the circuit in the first post (except I use USB and 5v from GPIO pin 2). What I don't know how to calculate despite my google efforts is how I calculate the value required where you have a 2.2K resistor for my 2N3904 Transistor.

I have blindly tested this with a 4.6K resistor (I dont have any 2.2k!) and this worked, but I want to understand how this value is calculated and what I should be using.

Thanks!

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

Re: How to wire a Raspberry Pi to a Sainsmart 5v Relay Board

Wed Jul 01, 2015 2:10 pm

BNZ wrote:Hi All.

I am a complete electronics noob and i have found this post incredibly useful as I have a 4 channel sainsmart 5v relay that I could only trigger one channel via 3.3v.

I have replicated the circuit in the first post (except I use USB and 5v from GPIO pin 2). What I don't know how to calculate despite my google efforts is how I calculate the value required where you have a 2.2K resistor for my 2N3904 Transistor.

I have blindly tested this with a 4.6K resistor (I dont have any 2.2k!) and this worked, but I want to understand how this value is calculated and what I should be using.

Thanks!
Let's see how your circuit with the 4.6K resistor will behave:

We look for the "worst case", to see if the current through the relay will be sufficient to trip it.

The GPIO pin specs on page https://www.raspberrypi.org/help/quick-start-guide/ tell us that when high, a pin will at worst provide 2.40V. (This is the Voh rating. You will notice that there are three numbers, depending on how the pin was programmed. We will take the worst case.) The transistor base-emitter voltage will be approximately 0.6V when on. That means you will have 2.4-0.6=1.8V across your 4.6K resistor. So, the current through the resistor (and therefore the transistor base) will be 1.8V/4.6K=0.4ma.

Now the transistor specs for 2N3904 (http://www.st.com/web/en/resource/techn ... 002987.pdf) state that the current gain (h_fe or simply beta) of the transistor varies between 50 and 300. It is usually safe to assume a value of 100, although if you really want to be pessimistic you should choose 50. This means, the collector current may be up to 0.4ma x 100 = 40ma.

If this is sufficient current for your relay, you should be ok. For a 5V relay, 40ma would mean a 5V/40ma = 125ohm coil resistance. If your coil resistance is larger than this, you are ok: The transistor will saturate, and a relay current less than 40ma will flow. Otherwise, the relay has been designed for a larger current, and it will operate only if you are lucky. (Not only that, but some voltage will remain on the transistor, and extra power will be dissipated in the transistor as heat, which may cause problems if the remaining voltage is high.)

Ooops. I thought you will be driving the relay directly with your transistor. (So the paragraph above is not valid, but I will not remove it since you may be intrested in that case as another design example.) If you will be driving the relay-module with the transistor (as you mention in your post) then all you need is only a 2ma current. (See my post above.) You are safe by a wide margin.

The original design with 2.2K resistor generates approximately twice the current, so it will allow for collector currents as high as 80ma.

Remember that this is a "worst case" scenario, you may get even a higher collector current allowance depending on device behavior.

BNZ
Posts: 9
Joined: Tue Jun 30, 2015 9:22 pm

Re: How to wire a Raspberry Pi to a Sainsmart 5v Relay Board

Sun Jul 05, 2015 2:20 pm

Thanks Mcy.

I will keep reading this until I understand it!

bboot
Posts: 2
Joined: Wed Jul 08, 2015 11:16 am

Re: How to wire a Raspberry Pi to a Sainsmart 5v Relay Board

Wed Jul 08, 2015 11:22 am

mcy wrote: The original design with 2.2K resistor generates approximately twice the current, so it will allow for collector currents as high as 80ma.
80 mA is not too much ? In this video (https://www.youtube.com/watch?v=b6ZagKR ... e=youtu.be), I saw the reference of the optocoupler : EL817. On this page : http://eu.mouser.com/ProductDetail/Ever ... 2bZw%3D%3D i saw that it allows a maximum of 60mA.

Or maybe i'm wrong ?

Thanks.

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

Re: How to wire a Raspberry Pi to a Sainsmart 5v Relay Board

Thu Jul 09, 2015 11:33 am

bboot wrote:
mcy wrote: The original design with 2.2K resistor generates approximately twice the current, so it will allow for collector currents as high as 80ma.
80 mA is not too much ? In this video (https://www.youtube.com/watch?v=b6ZagKR ... e=youtu.be), I saw the reference of the optocoupler : EL817. On this page : http://eu.mouser.com/ProductDetail/Ever ... 2bZw%3D%3D i saw that it allows a maximum of 60mA.

Or maybe i'm wrong ?

Thanks.
80 mA is the maximum current that will be sinked by the transistor collector, for the given base current. The actual current that will flow is determined by what is connected to the collector. It will be approximately 2 mA for the relay board. Up to 80 mA current, the transistor simply acts as a switch to the ground (emitter) and the current is limited by the load (i.e. whatever is connected between the transistor collector and the power supply).

For currents less than 80 mA the transistor will "saturate", meaning that the collector will be approximately at ground (emitter) potential, so the current x voltage on the transistor will be small and there will be little power (heating) going to the transistor. However if a load with a low resistance value is connected to the collector, the transistor will still sink no more than 80 mA. This will have some negative consequences: (1) The load is probably designed to operate with a larger current, so it may not function properly. (2) The voltage drop on the load will be smaller than the supply voltage, the remaining voltage will appear on the transistor. Then current x voltage on the transistor may now be appreciable and cause overheating. For driving such loads, you would need a transistor with higher ratings.

Regards - mcy

bboot
Posts: 2
Joined: Wed Jul 08, 2015 11:16 am

Re: How to wire a Raspberry Pi to a Sainsmart 5v Relay Board

Thu Jul 09, 2015 4:26 pm

mcy wrote: 80 mA is the maximum current that will be sinked by the transistor collector, for the given base current. The actual current that will flow is determined by what is connected to the collector. It will be approximately 2 mA for the relay board. Up to 80 mA current, the transistor simply acts as a switch to the ground (emitter) and the current is limited by the load (i.e. whatever is connected between the transistor collector and the power supply).
...
Thanks a lot for the answer ! You really helped me, I understand now. :)

Return to “Interfacing (DSI, CSI, I2C, etc.)”