Trying to control a relay - Page 1
Forum section: electronics / beginners

author: tobia
author_rank: Contributor
timestamp: May 23, 2020, 03:21:53 pm
content: HelloI'm a newbie, putting together a simple circuit for home automation, but I'm having trouble controlling a relay.You can see the schematic in the attachment, where the 1k resistor on the left takes a 5V digital out from an ATTiny pin, and the screw connectors on the right are supposed to control a valve.If you look at the Rigol screenshot in the second attachment, the blue signal is pin 2 (base) of the transistor, and the red signal is pin 3 (collector). Division is 1V, and GND for both signals is at -3 div, where the blue pointer is on the left.I've done similar circuits many times before, so this time I put it together without a second thought, but now I find that it's not working. In particular, the collector is not dropping to ground, therefore not opening the relay.What incredibly silly mistake am I making?PS. Third picture is the relay. I'm starting to think that it has something to do with inductance of the coil, because of the shape of the oscillation in the red signal. But it's a 5VDC controlled relay, so it should be fine?

author: DrG
author_rank: Super Contributor
timestamp: May 23, 2020, 03:54:37 pm
content: my guess...the 222 connections are not what you think they are...https://electronics.stackexchange.com/questions/109391/2n2222a-mismatch-between-emitter-and-collector

author: KMoffett
author_rank: Regular Contributor
timestamp: May 23, 2020, 03:58:30 pm
content: You have a base current of 4.4.mA. The coil current is 71mA. Rule of thumb Ib=Ic/10. So try decreasing the resistor to ~560Ω.

author: rstofer
author_rank: Super Contributor
timestamp: May 23, 2020, 03:59:49 pm
content: What is the coil resistance and, for the applied voltage, what will be the current? If I had to guess, I would think that the transistor is getting hot.You picked 1k Ohms for the resistor. Do you know why you picked that value?Once you know the relay coil resistance and the expected current, divide this Collector Current by 10 and use this for the Base Current. Calculate the base resistor as R=(5V - 0.7V) / (Base Current). The 0.7V is the expected Base Emitter voltage when the transistor is fully on. The 5V is probably wrong! Measure the actual pin output voltage when the new resistor is installed and perhaps refine the value. Sometimes pins won't pull all the way up when loaded.You want to guarantee the transistor gets into saturation. Measure the collector to emitter voltage when the relay is on and it should be 0.2V or so. Certainly nowhere near 1V. The power dissipation will be (Collector Current) * (Collector-Emitter Voltage).

author: tobia
author_rank: Contributor
timestamp: May 23, 2020, 05:30:07 pm
content: Quote from: DrG on May 23, 2020, 03:54:37 pmWow, this came out of left field. I had no idea, and yes you are right, E and C were swapped!Quote from: rstofer on May 23, 2020, 03:59:49 pmThank you for your detailed reply. The datasheet reports coil resistance at 70Ω and nominal current at 71.4mA, which is consistent with 5V applied voltage.The transistor was not getting hot, not that I could feel by hand.If I divide 71.4mA by 10 it means I want a base current of at least 7.1mA, which puts the base resistor at most at 605Ω, so yeah 1k was wrong.I put in a 560Ω one (after fixing the transistor polarity, see above.)Now I get a different waveform, but it's still not energizing the relay coil. Base-emitter (blue signal) is around 0.7V, a bit higher than before.The 5V rail is stable at 5V, not a dip.Collector-emitter (red signal) now gets down to 0.2V or so, but only for about 0.7ms, after which it rises to 3.4VThe mystery deepens!

author: tobia
author_rank: Contributor
timestamp: May 23, 2020, 08:27:06 pm
content: Thank you to everybody who replied.After fiddling around forever, I threw the transistor out and replaced it with a N-channel, enhancement mode, logic-level gated MOSFET (TN0606) which worked like a charm.I connected the Gate directly to the microcontroller pin; the Source to ground ("source" of what? electrons? worst pin name ever); and the Drain to the coil + flyback diode. I did need to add a pull-down resistors on the gate, otherwise it would accumulate charge and stay on. I tried a couple values and found out that 100K worked well (10K was too low, for whatever reason.)I will never understand BJTs.

author: Electro Fan
author_rank: Super Contributor
timestamp: May 23, 2020, 08:47:29 pm
content: Quote from: tobia on May 23, 2020, 08:27:06 pmLOLGood luck with thatNice work getting it going.