Relay not switching...


7 posts
by thogue » Sun Nov 04, 2012 3:39 pm
Well I went to ratshack and picked up a 5v relay to try to control my first relay via the rPi, here are my parts.

275-0240 - 5v - SPDT Relay (http://htyp.org/275-0240_Radio_Shack_5-volt_relay)

an NPN transistor pack, (2N222A , 2N3904, 2N4401).

1k ohm, 1/4 watt, 5 % tolerance.


GPIO -> 1kohm -> Base of Resister

Transistor E -> Ground

Transistor C -> - coil

+ coil -> 5v
+ coil -> diode -> 5v

Well anyways, the transistors work to control LEDs that require 2-3ma (I measured ma, via the multimeter). Also the relay clicks on if I simply "ground" the pin that should be going to the collector. If I attempt to "switch" the relay via gpio I hear VERY faint clicks, but the relay does not do its thing. When I measured current draw of the transistor with the relay hooked up to it, (and switched on via GPIO, I read about 40~ma, If I read the current draw of the relay connected directly to ground (causing it to switch on) the draw is 84~mah.

Maybe some of the more keen engineers can tell me where I have gone wrong? Tests seem to tell me that my transistors are not big enough, but that means I am missing some understanding of how these transistors are related. Also, I tested to make sure the transistors were getting fully saturated, and they are getting .71V and giving it 3.3v does not change the brightness of the LED.


Thanks anyone who read through that, and bigger thanks to anyone who can help set me straight. I would really like to understand exactly why this transistor is failing me.
Posts: 131
Joined: Wed Sep 19, 2012 2:16 am
by techpaul » Sun Nov 04, 2012 5:41 pm
A few thoughts is your circuit as shown below?

Which transistor from the pack are you using?

With relay trying to drive on what is the volatge on the transistor base and collector?
The collector voltage will enable a quick approximation of the coil current.

Have you tried putting a second 1k resistor in parallel with the existing resistor, giving 500R and more base current.

Looks like you have used wrong transistor, and cannot drive enough current to energise the relay, or a connection is not as good as you think it is.
Attachments
transistor-relay.gif
Is this your Relay circuit
transistor-relay.gif (5.82 KiB) Viewed 666 times
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/
Posts: 1482
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK
by johnbeetem » Sun Nov 04, 2012 7:00 pm
thogue wrote:Well anyways, the transistors work to control LEDs that require 2-3ma (I measured ma, via the multimeter). Also the relay clicks on if I simply "ground" the pin that should be going to the collector. If I attempt to "switch" the relay via gpio I hear VERY faint clicks, but the relay does not do its thing. When I measured current draw of the transistor with the relay hooked up to it, (and switched on via GPIO, I read about 40~mA, If I read the current draw of the relay connected directly to ground (causing it to switch on) the draw is 84~mA.

I think you're getting enough current for the relay to start to engage (hence the faint click) but not enough to flip all the way over. According to the relay spec you linked, you need approx 89 mA for the relay to switch on completely, which is approx what you get when you ground it. Your single transistor with 40 mA is only providing about half of that.

My suggestion: since you have extra NPN transistors, wire two of them up as a Darlington pair: http://en.wikipedia.org/wiki/Darlington_transistor

This way the current sourced by RasPi is low, but you get plenty of current amplification from the Darlington pair.
User avatar
Posts: 938
Joined: Mon Oct 17, 2011 11:18 pm
Location: The Coast
by thogue » Sun Nov 04, 2012 7:28 pm
Actually I tried all 3 transistors. I also have tried using two transistors as that seemed like a logical move when I saw I was seeing half the current draw then what was needed.... But it didn't do the trick and quickly made question my understanding.
Posts: 131
Joined: Wed Sep 19, 2012 2:16 am
by thogue » Sun Nov 04, 2012 9:35 pm
damn mobile devices, setting up a "darlington pair" right now :)
Posts: 131
Joined: Wed Sep 19, 2012 2:16 am
by johnbeetem » Sun Nov 04, 2012 9:45 pm
thogue wrote:Actually I tried all 3 transistors. I also have tried using two transistors as that seemed like a logical move when I saw I was seeing half the current draw then what was needed.... But it didn't do the trick and quickly made question my understanding.


[Edit: I wrote this before seeing preceding post.]

How did you hook up the two transistors? If you just put two of them in parallel sharing the single 1K resistor, that's not going to help with NPN transistors. It would work with NFETs.

Here's what your original circuit is doing: the base-to-emitter (BE) voltage drop is about 0.7V, so if RasPi is pulling up to 3.3V (probably more like 3.0v), you're getting 2.6V (or 2.3V) across the 1K resistor, which gives you 2.3-2.6 mA through the BE junction. The transistor amplifies the BE current by a "beta" factor of 10-20, i.e., the collector-to-emitter (CE) current is beta times the BE current. In your case, beta is about 16, which gives you the 40 mA you measured.

If you tie two NPNs in parallel, the BE current is split, with about half going through each BE junction. This means the CE current through each transistor is cut in half as well. So you'll have about 20 mA through each transistor, for a total of 40 mA. In other words, no change.

If you cut the resistor in half (500 Ohm), the current through the BE junction would double and thus the CE current would also double. However, this assumes that RasPi can source twice the current. Each BCM2835 GPIO can source 2-16 mA. I don't know what Linux sets it to by default. If it's only set to 2 mA, the GPIO won't drive enough current through the resistor. You can increase the drive current, but be careful how many high-current outputs you have on simultaneously. See http://elinux.org/RPi_Low-level_periphe ... .28GPIO.29

With the Darlington pair, the first transistor amplifies the current through the resistor by a factor of beta. The second transistor amplifies by a second factor of beta, so you end up with a total amplification of beta squared, typically 100-400.
User avatar
Posts: 938
Joined: Mon Oct 17, 2011 11:18 pm
Location: The Coast
by thogue » Sun Nov 04, 2012 10:13 pm
About to read your lengthy post ::Edit:: just read it, thank you so much for the lesson! , I would just like to say thanks everyone for your replies. Honestly I had never looked how a transistor worked until yesterday and I made a general understanding but the math certainly had not clicked yet, I think im much closer now.

Darlington pair does the trick! Also helped me understand transistors better, found lots of info somehow I had not stumbled into yet.
Posts: 131
Joined: Wed Sep 19, 2012 2:16 am