control a relay from the pi via GPIO

Hi! I got a pi model B that I want to control a relay.

I've got one just like the ome on the picture in the link here

I connect just as the picture, +5v to vcc and ground to ground. The green diod then ligths up.

So I connect a gpio to the middle pin marked Inl. GPIO low = nothing happens GPIO high = nothing happens.

I grab a voltmeter and measure between the GPIO and ground, and I get ca 0 V resp 3.3 V. So - correct pin, but no action in the relay.

Ok, thinking 3.3 might be to small to drive the relay, testing +5 V on the middle pin. Still nothing. Frustrated - testing ground to middle pin - and then the relay clicks, and the red diod lights up, somewhat brighter than the green one.

Surley - this is not supposed to happen - or ?

I got 3 of those relays, and all three behave the same - ground the middle pin and then action.

What am I doing wrong here?

--
--
Reply to
Björn Lundin
Loading thread data ...

Mostly you aren't reading the specifications of your components. Cough up a part number and maker of the relay and I'll see if I can find a data sheet that explains how the relay is intended to be used.

--

Rick C
Reply to
rickman

I dug on the Internet a bit and it looks like your device is an HL-51 which is a bit hard to read from the image. Digging further if I read the part number from the relay correctly, SRD-05VDC-SL-C, this is a 5 volt mechanical relay.

So you have two problems. One is that the relay coil is rated for 5 volts which means it should have 3.75 volts on the coil to work properly. The rPi I/Os won't survive with this much voltage across them when the output is not driven low.

The other is the rated current, 71.4 mA. This is far higher than what the rPi I/Os can drive.

To solve both problems you simply need to add a transistor to your circuit. In reality your relay module should have the transistor on it, but it doesn't look like it does.

There seem to be tons of available references on how to interface this type of relay to your rPi. If you provide better info on what you have I can give you more info on what you can do to use it.

--

Rick C
Reply to
rickman

There appears to be a three terminal blob to the right of the green LED which could be a switching transistor. I have a two relay board that looks remarkably similar. I haven't tried it on 3.3 volts, but it works fine with an Arduino at 5v.

Reply to
Dennis

I tried it with my RPi with I/O pin 24 and and it works fine. In looking at the picture in the link again they have the pins numbered wrong. Pin

22 is actually to the left of the blue wire termination - the one they show connected is actually pin 23.

The green LED is the power indicator, the red one is the relay state. The center relay terminal is common which is switched to the one on either side based on the input. The red LED in on with a low input.

Here is my test code - modify as needed for the I/O pin you use.

import RPi.GPIO as GPIO import time

#set up GPIO using BCM numbering GPIO.setmode(GPIO.BCM)

#setup GPIO using Board numbering #GPIO.setmode(GPIO.BOARD)

outpin = 24 # pin to control relay

GPIO.setup(outpin, GPIO.OUT) # make it an output pin

try: while True: GPIO.output(outpin,GPIO.LOW) time.sleep(5) GPIO.output(outpin,GPIO.HIGH) time.sleep(5) except KeyboardInterrupt: # If CTRL+C is pressed, exit cleanly: GPIO.cleanup() # cleanup all GPIO

Reply to
Dennis

I see the transistor blob you refer to. Do you know the circuit on your module? Is the transistor switched by 3.3 volts rather than 5?

If the relay is activated by a low the circuit would need to be a PNP transistor or P channel FET on the high side switched by pulling the base/gate to ground. This means the high would need to be 5 volts which is not entirely compatible with the rPi I//Os.

Does your relay activate on a high input or a low?

--

Rick C
Reply to
rickman

Several things.

Firstly you've bought a cheap relay board which may have said "Raspberry Pi" on it website, but it really is not Pi compatible. This happens a lot, especially with sellers in far-off land you have no way to get in-touch with. They lie and use the Raspberry Pi name to lure unsuspecting buyers.

So the relay board - the real issue is that it's designed for 5v operation. It's really for an Arduino or similar running at 5v. The Pi's

3.3v combined with the resistor into the opto coupler does not provide enough "oomph" to light the internal LED and activate the relay. Thats the case with most of these cheap boards...

... however - another issue is that sometimes these relay boards (and I've seen dozens of different ones )-: effectively connect the LED + resistor to the 5v line - expecting the driving computer to short it to 0v.

Try this:

Leave it wired as it is, but then remove the wire from the Pi'2 GPIO pin 22 and connect it to 0v/Gnd.

If the relay fires then you have one of these.

And by reading the above, this is most likely.

So do this:

Re-connect it to the Pi on that pin (bcm_gpio 22)

and run these commands:

gpio -g mode 22 in

Realy ought to be off and stay off.

gpio -g write 22 0

Nothing should happen at this point.

gpio -g mode 22 out

Relay should click ON. The pin is in output mode and is sinking the current from the 5v, via the resistor + LED to 0v.

gpio -g mode 22 in

Relay should click off.

I've seen this make similar relay boards work. It works, BUT ... when in the off state (ie. gpio pin is in input mode), you are presenting 5v to the GPIO pin. fortunately via a resistor and diode and the input protection diodes on the GPIO pin will limit the damage, but it's very not recommended. It'll work, but for how long...

Really, you need to throw this relay board away and get something designed for the Pi. Use something like a PiFace board.

Gordon (Author of wiringPi)

Reply to
Gordon Henderson

Thanks rickman, for your replies

Hmm, yes, but I did try feeding it 5 V as well. no go

That might be the problem. Still, if I grounded the signal pin, the relay came to life, so the coil was fed enough to click/switch the relay

Yes, I realize that now. Either I do that or try it with an arduino. I could communicate with the arduino via the serial port

Yes and I read a good part. Some info indicates it is ok to use directly with the pi, while other indicates that a transistor is needed. I just thought it was very strange that I got movement when I grounded the middle pin. As you can tell, electronics is not my strongest subject.

indicates 3 min into the movie that no transistor i needed.

I try the arduino , and it it works, I'll use that instead

I got a big server in the garage that gathers data at night. I don't want to run it during the day, so I'm thinking of automatically ssh in to it and issue a sudo halt using expect from the pi. This still leaves the fans on, so I'd like to kill the power to it via the relay. and when power is restored, it boots automatically.

--
--
Reply to
Björn Lundin

OK I traced out the circuit. It is a PNP transistor. Input through 1K to the base, 5V on the emitter, relay (with diode across the coil) and red LED (through 1K) on the collector to ground. The relay is energized by a low input (it has a NC and NO contact so the definition of on depends on which contact you are using).

With 5V Vcc the input pin is about 4.6V with no connection. This may be a problem for long term use on a RPi. I'm not sure how the RPi handles a voltage above 3.3V on an output pin. A level shifter may be a good idea.

If I change the Vcc to 3.3V the relay still switches, but I'm not sure how reliable that would be long term.

Reply to
Dennis

As Dennis pointed out there is a fuzzy blob on the photo that looks like it is three terminal and likely a transistor. So you would need to trace out the circuit to see what they have designed. The circuit on the module Dennis has turned out to be a high side switch which means it is handling the higher current for you, but is referenced to 5 volts and so will over stress your rPi I/O pin. If this is what is on your module it is not a good idea to drive this relay directly from your rPi I/O. It will need another transistor to handle the voltage translation. Just one transistor and two resistors. I would use a common base arrangement with the base biased to about 1 volt. Or you can use a common emitter arrangement which will invert the sense of the signal which you can compensate for in the software.

Some Arduinos are 5 volt devices which will drive this I/O directly. But first you need to trace out the circuit to see just what you have.

--

Rick C
Reply to
rickman

I am pretty sure mine has the same circuit as his but with 2 relays. It looks similar and has "2 Relay Module" and "HL-52" on the board. I think they make 1, 2, 4 and 8 relay versions that are just multiple copies of the same circuit.

The question is why high switching (PNP) rather than low side (NPN) which would work fine with either 3.3 or 5 volts. My guess is for safety. The processors I know of come up with the I/O in input mode which would look like a high signal (tri-state or pull-up) which would leave the relay unenergized. You can then do your set up before changing to output mode and selecting the relay. Using an NPN could energize the relay immediately on power up which you may not want.

Reply to
Dennis

I have seen Chinese boards made to do the same job but by different companies. They can vary widely while appearing very similar. So I don't share your confidence of the two circuits being the same, but I certainly can't rule it out.

It would not be an issue if the output comes up high impedance (which is not a pull-up). Either a high side or low side switch could default to off. A pull up or a pull down as appropriate should be on the relay module. I agree that there seems to be little difference between the high side and the low side switch. So I can't say why they would use the high side switch.

--

Rick C
Reply to
rickman

The relay boards I use are the same as ebay item 291563108767

That one happens to be 2 channel, I also use an 8 channel one. The clue is the jumper between two pins marked "VCC" and "JD-VCC" The idea is that if you remove that link you can power the board with its own local 5v PSU thus taking full advantage of the on-board opto-couplers. However, I can report that the board works fine with the jumper left in place, and connected to the pi gpio in exactly the way you are doing.

Interestingly all the single channel ones I can see don't appear to have the opto-coupler or the jumper, so I can't vouch for them.

--

Graham. 

%Profound_observation%
Reply to
Graham.

Mine does not have any jumpers at all - singel channel

--
--
Reply to
Björn Lundin

Well, what can I say. One born any minute ?

correct, it does.

and it does

this one too. toggling the port bewteen in and out toggles the relay

I'll hook it up via my arduiono, if I can bring that to life again.

And thanks a lot for the walk-trough for dummies, and for wiringPi.

--
--
Reply to
Björn Lundin

Info on this module is scant. Is there a reason why you don't trace out the circuit so you know for sure what the circuit is? It shouldn't be that hard to do.

If you only have one transistor, the circuit you have is most likely the one Gordon is thinking of. But it is always possible not. Trace it out and we'll know for sure.

It can be used with the rPi if you use something to limit the voltage on the I/O pin. A blue, white or UV LED would be perfect for this. As long as the circuit on the relay board has a resistor in the path from 5 volts to the input pin, the LED can safely clamp the voltage at that pin to about 3.3 volts protecting the rPi I/O. Use an IR LED in series between the rPi and the relay board and it will drop the excess voltage so the visible LED won't cause the relay circuit to activate.

| | Relay _____ | | Module / | | IR LED | |_ --- rPi | | /| | |/ Gnd Header -->>----+-----|< |------ Gordon

--

Rick C
Reply to
rickman

Assuming you can afford the space for a dual channel, I suggest you get one like ebay 291563108767.

You can defiantly afford the price!

--

Graham. 

%Profound_observation%
Reply to
Graham.

The two-relay board I mentioned works perfectly with a pi, and is

1/20th the price of a piface.

Common emitter buffers, opto-isolators, and channel LEDs

I am using it on my RASPBX with 16 shell scripts called from extensions_custom.conf with appropriate voice confirmation.

All great fun, but I can't think of any uses for it at the moment.

--

Graham. 

%Profound_observation%
Reply to
Graham.

Yes, I just ordered some. Thanks

--
--
Reply to
Björn Lundin

Yes, there is a reason. I'm a software guy, and although I do understand Ohms law, when it comes to transistors, I'm lost.

So I would not know where to start.

And this drawing is beautiful. However it is over my head. But I really appreciate the effort

Reply to
Björn Lundin

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.