mayburyds
Posts: 17
Joined: Wed Sep 05, 2012 7:06 pm

Sainsmart relay

Thu Oct 04, 2012 6:22 pm

Hi,

Real noob when it comes to using the GPIO on the RPI! In fact never done anything like this before.

Been reading a lot about using the GPIO and I'm now totally confused!

What I'm looking to do is attached an 8 channel 5v Sainsmart relay card to the RPI.

I have read that you need a "buffer" to protect the RPI when using the GPIO, then I have read that you can use pin 2 (5v) and the pin 6 (ground) off the RPI on to the relay board. Then somewhere on the forum someone is using the same relay board but using the 3.3v and a transitor to boost up to 5v.

My questions are do I need some sort of buffer in-between the RPI and the relay board or can I use pin 2 and 6 on a Raspberry pi V2 to power the relay board or both?

Many thanks.

Frank B
Posts: 61
Joined: Fri Sep 14, 2012 8:02 pm
Location: Germany

Re: Sainsmart relay

Thu Oct 04, 2012 6:56 pm

mayburyds wrote: What I'm looking to do is attached an 8 channel 5v Sainsmart relay card to the RPI.
Can you provide a link to this card? Maybe to the schematics?

mayburyds
Posts: 17
Joined: Wed Sep 05, 2012 7:06 pm

Re: Sainsmart relay

Fri Oct 05, 2012 7:36 am

Hi,

This the card I have -

http://www.sainsmart.com/8-channel-dc-5 ... logic.html

There is a link on the site for more info but it only relates to the relay on the card.

I went for this card after seeing this on youtube -

http://www.youtube.com/watch?v=8X6PgYaegz0

the comments reference uisng the 5v and ground pins on the GPIO -

"Once I got the relay's I cut two floppy ribbon cables and wired the pins to the GPIO pins on the Raspberry Pi on the relay it was just 5V R1 R2 R3 R4 R5 R6 R7 R8 GND."

there is further comment from someone who has used the card -

"Just managed to get it working now, the critical thing is that the GPIO pins must be 'off'' to get the relay to switch on. So for example: RPi-Pin2 (5v) -> Relay-VCC, RPi-Pin6 (GND) -> Relay-GND, RPi-Pin11 (GPIO17, wiringPi 0) -> IN1. Using wiringPi command line: gpio mode 0 out // set port to output mode; gpio write 0 0 // switch on relay 1; gpio write 0 1 // switch off relay 1; Hope this helps someone :-)"

Thanks for your help.

pjc123
Posts: 921
Joined: Thu Mar 29, 2012 3:37 pm

Re: Sainsmart relay

Fri Oct 05, 2012 11:39 am

I have the same exact relay and there are two issues:

1) The relay card is active low. In other words you have to output a zero from the GPIO port to turn on the relay.

2) I was not able to get it working using 3.3v for Vcc and Jd-Vcc because there was not enough power to drive either the opto or the relays properly, so I used the pi's 5v line.

To resolve these issues I used a transistor between each GPIO output and each relay input. The 2.2k resistor and transistor separate the GPIO 3.3v output and the 5v lines and also protect the GPIO from damage. I then attached the jumper between Vcc and JD Vcc so it supplies 5v to both the optoisolator and the coil; that defeats the purpose of the opto, but at least it gets everything working. (In reality, in my circuit, I have two different 5v lines because I have another power supply as part of a different part of the project that I am building, so I do not use the Vcc - JdVcc jumper. I use the pi's 5v to operate the opto and a different 5v supply to operate the coils. That way I get true opto isolation and don't bog down the 5v line of the pi with current draw. Each coil takes about 60 ma to operate, so turning them all on is a pretty hefty 480ma load).
relay.jpg
relay.jpg (16.9 KiB) Viewed 57538 times
My Raspberry Pi Project Page:

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

mayburyds
Posts: 17
Joined: Wed Sep 05, 2012 7:06 pm

Re: Sainsmart relay

Fri Oct 05, 2012 1:13 pm

pjc123

Many thanks for the useful information.

From what you have said it would be best to go for an additional power supply.

I have a spare 5v 1000ma supply that I can use.

Is this correct - the 5v line (pin 2) and GND (pin 6) from the RPI goes to the GND and VCC on the header pins on the card and the power from the additional power supply to the GND and JD-VCC on the card?

Would this provide enough power to switch all 8 relays?

Thanks again.

pjc123
Posts: 921
Joined: Thu Mar 29, 2012 3:37 pm

Re: Sainsmart relay

Fri Oct 05, 2012 8:53 pm

I will dig up the schematics for the relay card so you can better see what is going on. You really don't need the extra power supply if you don't have the pi maxed out like mine is (High power usb wifi dongle, usb hard drive, etc.). The 5v line from the pi should be fine. Whatever you do, don't hook up the GPIO outputs and the 5v to the relay card without using a transistor or equivalent. If you do that, then you will be applying 3.3 v to the opto in one direction (when it is logic 1 or high) and 5v from the pi power supply to the opto in the other direction......not good.
My Raspberry Pi Project Page:

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

pjc123
Posts: 921
Joined: Thu Mar 29, 2012 3:37 pm

Re: Sainsmart relay

Fri Oct 05, 2012 11:48 pm

OK, here we go.................

I have attached links to the schematics and spec sheets which I found after a long Google session when I first bought the relay card. Follow along, looking at the schematic as you read the rest of this, so you understand the various designations on the relay board that I am talking about. I must say that I was also very confused as well at first, reading that some people just hooked it up and it worked while others just couldn't get it to work. I think some of the confusion may be that there are a couple of these relay boards around from different manufacturers, and they don't all work the same, and people are using it with many different products, like Arduino for example, and each has different voltage levels on their GPIO output lines (CMOS vs TTL).

I was hoping when I bought the relay board that I could just hook it up to the GPIO ports and it would just work. So, the first thing I tried was to connect the 3.3v GPIO pin of the pi to Vcc of the relay (Pin 10 of the 10 pin header), then removed the jumper from pins 2 and 3 of the 3 pin relay header, connected the 5v GPIO pin of the pi to JD-Vcc of the relay (Pin 3 of the 3 pin header) so it operates the coil, and hooked the ground to pin 1 of the 10 pin relay header. Unfortunately, because there is a 1k ohm resistor in series with an indicator led and the opto's led, there is just not enough voltage at 3.3v to turn on the led in the optos (I measure only .49 ma flowing through the opto). So some relays worked, some didn't, some barely turned on.

Since that didn't work I connected the 5v GPIO pin of the pi to Vcc of the relay (Pin 10 of the 10 pin header), put the jumper back on pins 2 and 3 of the 3 pin relay header so that it would operate both the opto as well as the coil of the relay, and hooked the ground to pin 1 of the 10 pin relay header. I then attached the output of the GPIO to the base of an npn transistor using a 2.2 k resistor. I see others have used values as high as 10k or more (Since there is only 1.17ma flowing through the base, I really don't care about the value). The collector goes to output of the opto (IN1, IN2, etc). The emitter goes to ground. Now, it is active high instead of low, which makes a lot more sense; I don't want to have to continuously apply power to the GPIO data ports to keep the relays off. So, when I send a 1 or high to the GPIO port, this causes current to flow through the base of the transistor, 1.88 ma to flow through the opto, the opto lights up, and the relay turns on with a very solid loud click every time with every relay.

If you decide to use a different power supply to power the coils, it is as simple as removing the jumper from pins 2 and 3 of the 3 pin relay header, connecting the GPIO 5v line to to pin 2 of the header to operate the optos, and connecting the external 5v power supply to pin 3 of the header to operate the coils.

As a side note, the ground pins on both headers are connected together on the circuit board, so you do not need to attach to both of them.

You might come up with a better way to get it working, but that is what worked for me.

Relay Card Schematic
http://drop.st/yQbM8J

Relay Card Specifications
http://drop.st/M978gk

Man, I have to take a pee after all of that !
My Raspberry Pi Project Page:

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

mayburyds
Posts: 17
Joined: Wed Sep 05, 2012 7:06 pm

Re: Sainsmart relay

Sat Oct 06, 2012 8:27 am

pjc123

Many thanks for the information and sharing your experiences.

Just to clarify a couple of points -

You need a transistor/resistor for each relay switch, in this case 8?

Apart from the advantages of not having to power the relays all the time and setting them high instead of low to switch them on, which makes a lot of sense, do you need the transistors/resistor if you use two power supplies?

Thanks again.

pjc123
Posts: 921
Joined: Thu Mar 29, 2012 3:37 pm

Re: Sainsmart relay

Sat Oct 06, 2012 11:19 am

mayburyds wrote: You need a transistor/resistor for each relay switch, in this case 8?
Yes. Since simple transistors do not come in an IC package (Actually they do from a couple of manufacturers but are very expensive) you could get a 8 channel darlington pair IC, like a ULN2803, which would make wiring a little simpler, but you will be wasting unnecessary power. The other option is to buy one of the add-on boards that do the work for you.
mayburyds wrote: Apart from the advantages of not having to power the relays all the time and setting them high instead of low to switch them on, which makes a lot of sense, do you need the transistors/resistor if you use two power supplies?
Yes. It keeps the 3.3 CMOS output of the GPIO digital lines isolated from the 5V line of the pi. Also, keep in mind that the GPIO 5V line of the pi is not protected by a fuse, so check and then recheck your wiring before turning everything on. I am actually ordering a couple of small fuses to put on my 5v line when I place my next order.
My Raspberry Pi Project Page:

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

mayburyds
Posts: 17
Joined: Wed Sep 05, 2012 7:06 pm

Re: Sainsmart relay

Sat Oct 06, 2012 1:43 pm

pjc123,

Think I'm good to go now!

Many thanks again for the information you have passed on.

Cheers.

pjc123
Posts: 921
Joined: Thu Mar 29, 2012 3:37 pm

Re: Sainsmart relay

Sun Oct 07, 2012 8:57 pm

Have fun.
My Raspberry Pi Project Page:

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

mayburyds
Posts: 17
Joined: Wed Sep 05, 2012 7:06 pm

Re: Sainsmart relay

Sun Nov 11, 2012 7:49 pm

pjc123,

Just to let you know that I finally managed to get the relay board up and running thanks to your help.

I wasn't sure what to do about the programming side but came across wiringpi from this site -

https://projects.drogon.net/raspberry-p ... ingle-led/

which simplified things no end.

Thanks again for all your help and information.

Cheers.

pjc123
Posts: 921
Joined: Thu Mar 29, 2012 3:37 pm

Re: Sainsmart relay

Mon Nov 12, 2012 1:04 pm

Great to hear you are up and running.

Wiringpi is one of two "C" programming libraries that I found a while ago for the pi. I have to agree that the libraries make communicating with the GPIO a very simple task. For no technical reason I chose the following library, and it works great as well:

http://www.open.com.au/mikem/bcm2835/index.html

I picked a "C" library because I have used that language extensively in the past. There is also a python library:

http://pypi.python.org/pypi/RPi.GPIO

I just ordered parts to transfer the transistor circuitry along with some other circuits to a solder-able circuit board, so I can mount it along with the relay card, a power supply, switches, antenna, connectors, etc., etc,. etc, into an aluminum chassis for a project that I am building.
My Raspberry Pi Project Page:

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

chisleu
Posts: 14
Joined: Sat Oct 27, 2012 5:35 pm

Re: Sainsmart relay

Thu Dec 13, 2012 4:42 am

I love Google... :)

So I have one of these beauties as well and I'm trying to hook it up.

Since the drawings from the previous posts are down, I wanted to be sure I have this right.

I want the switch the gpio output pin to high and have the relay turn on. Since the sainsmart wants the input to go low to have the relay turn on, I need to use a transistor in a funny way, amirite?

I have the transistor on my protoboard like so:

GPIO->10k resistor->base
5v -> sainsmart input pin and transistor collector
0v -> emitter

Is this right? The ground will counteract the +5v through the transistor when I activate the base pin, correct?

It seems that this would cause the transistor to load up to max (depending on the base resistor) and pull like 20MA or something all the time. Being the thrifty nudist I am, it seems like I need a resistor between the +5v and the transistor somewhere.

I'm an electronics newbie, so any help is appreciated! I've already fried 1/2 the pins on one of my RPi's and don't want a repeat on the new unit! haha

Thanks!

chisleu
Posts: 14
Joined: Sat Oct 27, 2012 5:35 pm

Re: Sainsmart relay

Thu Dec 13, 2012 5:43 am

I'm sorry for the useless post.... I reread the posts again and found what I was looking for.

The collector on the transistor goes directly to the pin and merely switches the ground on to drop that pin so the relay switches on!!

Fun times! I've got a test circuit running so now I just need to pick up another board to build transistor circuits for all the relays!

Hopefully I can find some sort of connector to I don't have to solder to this hacked up floppy drive cable.

pjc123
Posts: 921
Joined: Thu Mar 29, 2012 3:37 pm

Re: Sainsmart relay

Thu Dec 13, 2012 12:42 pm

chisleu wrote: The collector on the transistor goes directly to the pin and merely switches the ground on to drop that pin so the relay switches on!!
Correct.
chisleu wrote: Hopefully I can find some sort of connector to I don't have to solder to this hacked up floppy drive cable.
Are you connecting the cable to a solderless breadboard or a soldered circuit card? I have ordered parts to build both and could supply you with parts numbers if you wish. If it is a solderless breadboard, Adafruit makes a connector for that purpose if you don't want to make your own.
chisleu wrote:Since the drawings from the previous posts are down, I wanted to be sure I have this right.
If someone has a free method to repost the drawings, without needing for me to login, and without a timeout, I will repost them.
My Raspberry Pi Project Page:

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

ryanjennings
Posts: 8
Joined: Mon Jul 09, 2012 4:14 pm

Re: Sainsmart relay

Fri Dec 14, 2012 2:42 pm

Would a 2N2222 instead of the bc547b work for this setup?

Thanks,

Ryan

pjc123
Posts: 921
Joined: Thu Mar 29, 2012 3:37 pm

Re: Sainsmart relay

Fri Dec 14, 2012 5:10 pm

ryanjennings wrote:Would a 2N2222 instead of the bc547b work for this setup?

Thanks,

Ryan
Sure, they are both basic NPN style transistors. You are pretty much just using them as a switch. The 2N2222 can handle a lot more collector current and costs more, but if you already got 'em use 'em.
My Raspberry Pi Project Page:

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

User avatar
dliloch
Posts: 173
Joined: Wed Jun 27, 2012 6:28 pm
Location: cleveland, ohio usa

Re: Sainsmart relay

Thu Jan 31, 2013 6:42 pm

hello,
your description of how to operate the sainsmart relay board worked perfectly .. I have a 2 relay board. my only question is why use the resistor? I used a 10k resistor just because that is what I saw others doing .. you indicate that you don't care about the value .. so do you really need the resistor? (obviously I know nothing about electronics so take that into account) .
thanks..
pjc123 wrote:OK, here we go.................

I have attached links to the schematics and spec sheets which I found after a long Google session when I first bought the relay card. Follow along, looking at the schematic as you read the rest of this, so you understand the various designations on the relay board that I am talking about. I must say that I was also very confused as well at first, reading that some people just hooked it up and it worked while others just couldn't get it to work. I think some of the confusion may be that there are a couple of these relay boards around from different manufacturers, and they don't all work the same, and people are using it with many different products, like Arduino for example, and each has different voltage levels on their GPIO output lines (CMOS vs TTL).

I was hoping when I bought the relay board that I could just hook it up to the GPIO ports and it would just work. So, the first thing I tried was to connect the 3.3v GPIO pin of the pi to Vcc of the relay (Pin 10 of the 10 pin header), then removed the jumper from pins 2 and 3 of the 3 pin relay header, connected the 5v GPIO pin of the pi to JD-Vcc of the relay (Pin 3 of the 3 pin header) so it operates the coil, and hooked the ground to pin 1 of the 10 pin relay header. Unfortunately, because there is a 1k ohm resistor in series with an indicator led and the opto's led, there is just not enough voltage at 3.3v to turn on the led in the optos (I measure only .49 ma flowing through the opto). So some relays worked, some didn't, some barely turned on.

Since that didn't work I connected the 5v GPIO pin of the pi to Vcc of the relay (Pin 10 of the 10 pin header), put the jumper back on pins 2 and 3 of the 3 pin relay header so that it would operate both the opto as well as the coil of the relay, and hooked the ground to pin 1 of the 10 pin relay header. I then attached the output of the GPIO to the base of an npn transistor using a 2.2 k resistor. I see others have used values as high as 10k or more (Since there is only 1.17ma flowing through the base, I really don't care about the value). The collector goes to output of the opto (IN1, IN2, etc). The emitter goes to ground. Now, it is active high instead of low, which makes a lot more sense; I don't want to have to continuously apply power to the GPIO data ports to keep the relays off. So, when I send a 1 or high to the GPIO port, this causes current to flow through the base of the transistor, 1.88 ma to flow through the opto, the opto lights up, and the relay turns on with a very solid loud click every time with every relay.

If you decide to use a different power supply to power the coils, it is as simple as removing the jumper from pins 2 and 3 of the 3 pin relay header, connecting the GPIO 5v line to to pin 2 of the header to operate the optos, and connecting the external 5v power supply to pin 3 of the header to operate the coils.

As a side note, the ground pins on both headers are connected together on the circuit board, so you do not need to attach to both of them.

You might come up with a better way to get it working, but that is what worked for me.

Relay Card Schematic
http://drop.st/yQbM8J

Relay Card Specifications
http://drop.st/M978gk

Man, I have to take a pee after all of that !

pjc123
Posts: 921
Joined: Thu Mar 29, 2012 3:37 pm

Re: Sainsmart relay

Thu Jan 31, 2013 8:45 pm

dliloch wrote:hello,
your description of how to operate the sainsmart relay board worked perfectly .. I have a 2 relay board. my only question is why use the resistor? I used a 10k resistor just because that is what I saw others doing .. you indicate that you don't care about the value .. so do you really need the resistor? (obviously I know nothing about electronics so take that into account) .
thanks..
Great!

The resistor limits the current that flows from the GPIO port through the base of the transistor then through the emitter of the transistor to ground. Too low a resistor value and you can damage either the GPIO output or the transistor because too much current is flowing. Too high a resistor value and the current is too low to turn on the transistor properly. I have tried values as high as 22k and it still worked. So when I stated I don't care about the value I should have said that there is a wide acceptable range that does not draw too much from the GPIO pin yet still turns on the transistor enough and reliably; the real danger using too low a resistor value.

Since this post, gordon@drogon.net made a good suggestion to improve the circuit. Because the GPIO's are set up as inputs at boot, it is a good idea to also put a 10k resistor from each GPIO port to ground (Before the 2.2K resistor). This ensures that the GPIO ports are at ground and not floating at boot (set to a "LOW" condition). Without the resistor, the voltage from the floating GPIO pin could be high enough to trip the relay circuit.
My Raspberry Pi Project Page:

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

pjc123
Posts: 921
Joined: Thu Mar 29, 2012 3:37 pm

Re: Sainsmart relay

Fri Feb 01, 2013 12:23 am

To continue with my last post, I have not tried it yet with the 10k resistor because I am going to test it after I solder up everything (although I expect no problems), but here is where to put it since "a picture is worth a thousand words" as they say:
relay2.png
relay2.png (18.5 KiB) Viewed 56093 times
My Raspberry Pi Project Page:

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

glukoz
Posts: 4
Joined: Thu Jan 17, 2013 5:08 pm

Re: Sainsmart relay

Fri Feb 08, 2013 10:21 pm

I want to set the same connection between Rpi and relay and I have one doubt regarding ground wires. Can ground wires in circuit be connected to multiple ground pins in Rpi or should they all be connected to one ground pin? If multiple, does it really matter which ground pins I will choose? If only one, which pin should I choose for that purpose?

pjc123
Posts: 921
Joined: Thu Mar 29, 2012 3:37 pm

Re: Sainsmart relay

Fri Feb 08, 2013 11:15 pm

glukoz wrote:I want to set the same connection between Rpi and relay and I have one doubt regarding ground wires. Can ground wires in circuit be connected to multiple ground pins in Rpi or should they all be connected to one ground pin? If multiple, does it really matter which ground pins I will choose? If only one, which pin should I choose for that purpose?
Although I never measured it, I suspect that all those GPIO grounds are just connected together on the circuit board. I just connected all the grounds of my circuit and the relay card to pin 6 Ground of the GPIO header, choosing that ground pin for no particular reason. Since you are probably using relays for isolation, you don't want to connect the relay output grounds to that ground, but I imagine you know that already.
My Raspberry Pi Project Page:

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

glukoz
Posts: 4
Joined: Thu Jan 17, 2013 5:08 pm

Re: Sainsmart relay

Sun Feb 10, 2013 3:32 pm

I eventually connected ground wires to multiple ground pins on Pi and circuit works perfectly. Thank you for help.

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

Re: Sainsmart relay

Wed Mar 27, 2013 8:22 pm

Thanks for this whole interchange, all. I was able to use it to lay out a board that actually worked :)

Schematic/layout is available at https://github.com/fixedd/RPi_SainSmart_Interface if anyone is interested.

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