messinwu
Posts: 17
Joined: Wed Aug 08, 2012 1:33 pm

Relay connected to GPIO pin

Mon Aug 13, 2012 2:01 pm

Hey everyone

I'm very new to electronics, and very interested in learning. I found this article:

http://www.susa.net/wordpress/2012/06/r ... sing-gpio/

... and decided to get the stuff from Radio Shack and yesterday after a failed attempt due to misunderstanding of the relay posts, I did get it working as intended. Yay!

Now I just want to add an LED to the circuit, to come on when the GPIO is "on". I've read that LED's kind of act like a resistor, so could I just replace the resistor in the above design with the LED? Not sure how much resistance they provide. I've ordered a couple strips of "light green" LED's, if that tells you anything.

Or maybe I just put the LED in line between the transistor and the relay?

Any help would be greatly appreciated.... thanks in advance!!

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

Re: Relay connected to GPIO pin

Mon Aug 13, 2012 2:14 pm

All the circuits I've seen show a resistor with the LED to limit the current flow. Without the resistor the LED would act like a fuse. A short, bright, life followed by death.

Add the LED in series before or after the resistor. It may be dim though with a 1K resistor.

You might need to lower the resistor to 500 or so to make the LED glow. Whether that would screw up your transistor/relay operation I'm not competent to judge.

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 13370
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: Relay connected to GPIO pin

Mon Aug 13, 2012 2:16 pm

No the LED does not "act like a resistor", LED's are diodes, and drop a more or less constant voltage of 2V (for a red LED) over them when a reasonable current (1mA) runs through them.
One simple solution would be to tie the LED in series with the base resistor for the transistor, this would work fine if the GPIO would put out say 6 Volt, but because the GPIO only puts out 3,3 Volt, and LED drops 2V its not possible to use this simple solution (the base drops another 0.7 Volt, so together they are 2.7 Volt, and 3.3Volt is too little voltage, as it would mean only 3.3 - 2.7 = 0.6 Volt would fall over the base resistor.

So in this case its best to connect LED + series resistor over the relay coil, which will have 5 Volt over it when the relay is activated. Put a 1K resistor in series with the kathode of the LED, and connect the anode of the LED to the 5V line, connect the other end of the 1K resistor to where the collector of the transistor and the relay coil are connected together.
The LED will burn when the relay coil gets its 5V.

User avatar
MattHawkinsUK
Posts: 538
Joined: Tue Jan 10, 2012 8:48 pm
Location: UK
Contact: Website

Re: Relay connected to GPIO pin

Mon Aug 13, 2012 2:21 pm

That circuit is really for switching on a mains powered device, in this case BL1. If all you want to do is turn on an LED then that circuit isn't really suitable.

However what you need to do is replace BL1 with your LED. Put a 560 ohm(approx) resistor in line with it. And replace the 240V supply with 5V. This means when the relay switches it will connect the resistor and the LED to 5V.

The other option is bin BL1, REL1, AC1 and D1. Connect the 5V point to a 560 ohm resistor, then an LED and then the collector of the transistor. This is close to the circuit I use for switching LEDs from GPIO pins. This is much cheaper as it doesn't require any diodes or a relay.

Image
My Raspberry Pi blog and home of the BerryClip Add-on board : http://www.raspberrypi-spy.co.uk/
Follow me on Google+, Facebook, Pinterest and Twitter (@RPiSpy)

messinwu
Posts: 17
Joined: Wed Aug 08, 2012 1:33 pm

Re: Relay connected to GPIO pin

Tue Aug 14, 2012 11:03 am

Thanks guys!

Actually, I'm not just trying to switch an LED on and off, I'm switching something else on and off using the relay, but I'd just like an LED to indicate activity or not. So, the suggestion by mahjongg to put it over the coil connections sounds like the best solution.

However, I then have to wonder, since the LED is pulling 2v, is that going to cause the coil to not activate the relay, since it requires 5v to operate? Or, is that going to effectively try to pull 7v from a 5v source? Or, is this taken care of with the resistor inline?

User avatar
Burngate
Posts: 6371
Joined: Thu Sep 29, 2011 4:34 pm
Location: Berkshire UK Tralfamadore
Contact: Website

Re: Relay connected to GPIO pin

Tue Aug 14, 2012 11:31 am

You'll need to put a 560 ohm resistor in series with the LED, and those two across the relay coil.

If you were to put a LED in series with the coil, as you say the LED would have 2v across it, leaving only 3v for the relay which might not be enough. And the resistance of the coil might be so low that more current will flow than the LED can handle.

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 13370
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: Relay connected to GPIO pin

Tue Aug 14, 2012 10:02 pm

Not to mention that the LED might get damaged by the reverse voltage that will occur over the coil when you turn it off (that is why there is a diode over the coil, so that this very high reverse voltage doesn't damage the transistor, but is safely disposed of. In case you wonder, a relay coil is an inductor, and thus as inductors do will generate a voltage if you try to stop running current through them, think of a inductor in this sense as a flywheel that tries on going (turning) even when you do not drive it anymore. The diode is the "energy dump" that safely disposed the energy stored in the "flywheel" (coil), but if there is a diode (LED) in the way that tries to block this energy flow it will cause so much voltage over the LED that it will become defective. In other words, a LED in series with the coil is a very bad idea indeed.

Obviously I meant putting the LED (and its indispensable series resistor) over (in parallel with) the coil. And obviously I understood that the LED should light only when the relay is activated.
And in fact It will do this, connected this way.

messinwu
Posts: 17
Joined: Wed Aug 08, 2012 1:33 pm

Re: Relay connected to GPIO pin

Wed Aug 15, 2012 1:00 am

Awesome advice! Thanks everyone!! I'll post some pics after I get it done. Unfortunately, work is keeping me too busy at the moment, so it'll be a few days, this weekend at latest.

Thanks again!!

Return to “Other projects”