Controlling a relay from a transistor.

Hi, this question has probably been asked many times, but even after 4 days I cannot find an answer that works for me.

I am trying to control a relay with the arduino UNO board with a transistor. With a wiring similar to http://playground.arduino.cc/uploads/Learning/solenoid_driver.pdf except solenoid is a relay in my setup.

When I physically connect the relay coil to the 5v pin on the arduino board it works since there is sufficient voltage and current as the 5V pin draws power from my battery pack/USB. The relay has a nominal coil current of 89.3mA and pickup/dropout voltage of 3.5/0.25V. One end of the coil goes to the 5V pin With the transistor in the setup connected to GND and a digital out pin controlling the transistor (2N222A/2N4401, I have both at disposal), I may hear a very faint clicking sound in the relay with the diode removed, but diode or none, the relay is not triggered by the transistor.

Relay cannot be driven directly from digital out pin due to P=IV. digital pins only have 40mA max which is not enough for the relay coil

The transistor's passed voltage is measured to be around 4.9V and there is significantly less current (my amp meter is not working at the moment, but testing with an LED gives a much dimmer light). All of the relay/transistor tutorials and instructions around the internet show a transistor helping drive the relay. Is there something wrong about my setup and is the transistor supposed to reduce current? If so, how is this setup supposed to work?

Thanks a bunch!

Are you sure you have E-B-C identified & connected properly on the 2N222A / 2N4401 Transistors?

Do not operate the circuit without the diode properly connected across the coil ! The diode protects the Transistor from the large inverse spike.

Try to get the Resistor, Transistor, Diode and Relay working before you attach to Arduino. Connect Resistor to +5v should turn on the Transistor and Relay. Connect Resistor to Ground should turns off the Transistor and Relay.

Both transistors should provide enough current. What size resistor do you have between the output pin and the transistor base?

mrsummitville: Are you sure you have E-B-C identified & connected properly on the TIP102 Transistor?

Yes, I have the order as C-B-E on the 2N222A transistor. I don't think I mentioned TIP102, but I have that as well and know that it is B-C-E. TIP102 data sheet: http://www.fairchildsemi.com/ds/TI/TIP102.pdf

ralphd: What size resistor do you have between the output pin and the transistor base?

I tried a 330om resistor and no resistor at all and got the same results. What is the resistor supposed to do between output and base? I thought a digitalWrite of HIGH would just let the power through the transistor.

Sorry, saw TIP 102 in the schematic that you posted.

Build the Circuit and make it work BEFORE connecting to Arduino. And do [u]not[/u] operate the circuit without the diode connected.

I do not understand ... "... The transistor's passed voltage is measured to be around 4.9V ..."

When Resistor is connected to +5v ... 1) B-to-E should measure about 0.7 Volts 2) C-to-E should measure about 0.8 Volts 3) Voltage across the relay will only measure 4.2 Volts. So will 4.2 Volts energize the relay ?

After you get the circuit working then connect to Arduino.

The resistor limits the current to the proper milliamps.

By passed voltage, I was referring to C-to-E.

The purpose of the transistor is to pass the 5V pin power to the relay coil when we energize the base transistor pin, correct?
How does the base voltage affect the transistor if (voltage) == on and (!voltage) == off?

With 2N222A transistor:

  1. B-to-E is measuring at 1.60 V.
  2. C-to-E is measuring at 2.26 V.
  3. Voltage across relay is 5.00 V.

Please post URL LINK to 2n222a transistor datasheet

"... How does the base voltage affect the transistor if (voltage) == on and (!voltage) == off? ..." Try to get the Resistor, Transistor, Diode and Relay working before you attach to Arduino. Connect Resistor to +5v should turn on the Transistor (B-to-E = 0.7v) and turn on Relay. Connect Resistor to Ground should turn off the Transistor (B-to-E = 0v) and turn off Relay.

Your B-to-E of 1.60v sounds like a Darlington configuration not a simple Transistor = 0.7v ??? Something is wrong here. Your Emitter (E) is connected to ground? I need the data sheet.

Do [u]not[/u] apply +5 volts directly to BASE without the current limiting resistor! Otherwise the Transistor can be damaged.

You state ... "... Voltage across relay is 5.00 V ..." Well then your relay must be ON ??? I mean measure voltage across the relay's coil when the Transistor is ON. The voltage across the Relay cannot be 5 volts, as you stated.

Make sure the Diode is pointed in the correct direction. There is a right way and a wrong way - polarity sensitve.

The purpose of the transistor is to pass the 5V pin power to the relay coil when we energize the base transistor pin, correct?

No the purpose of the transistor is to connect the ground of the relay coil to the ground of the circuit to allow current to flow. When the transistor is off, no current through the base, you will measure a voltage across the collector and emitter. When the transistor is on the collector is connected to the emitter, so no voltage is measured but cuter rent flows from 5V throught the relay, through the transistor to ground.

Is the relay driven by a seprate supply? Have you connected the two grounds together? Have you set the pin to be an output in the setup with a pin mode statement.

mrsummitville: Please post URL LINK to 2n222a transistor data sheet

...

Well then your relay must be ON ??? I mean measure voltage across the relay's coil when the Transistor is ON. The voltage across the Relay cannot be 5 volts, as you stated. Make sure the Diode is pointed in the correct direction. There is a right way and a wrong way - polarity sensitve.

Currently "Resistor to 5V" turns on the transistor. "Resistor to ground" turns off transistor. Datasheet: http://users.ece.utexas.edu/~valvano/Datasheets/2N2222.pdf Yes, my emitter is connected to ground. I checked my measurements again with a new 2N222 transistor. The last time, I accidentally measured with an LED in the circuit. Removed LED. With base energized, C-to-E is ~5 V.

The relay should be ON with 5V passing through it. However, it is not and I suspect that is due to insufficient current somehow. (see below) When I said that connecting relay coil from 5V to GND worked earlier, I just tried adding another wire in the coil circuit to measure voltage (got 5 V) and the relay did not turn on. I am using those thin short wires that fit into breadboards.

Grumpy_Mike: Is the relay driven by a seprate supply? Have you connected the two grounds together?

Yes, the relay switch is "driven?" by another supply. The coil is powered by the 5V power supply and the relay switches to complete a separate circuit that is about 7V.

Grumpy_Mike: Have you set the pin to be an output in the setup with a pin mode statement.

Oh my gosh, I had completely forgotten to add pinMode(X, OUTPUT); to my code. It worked after I added it in!

Still I don't understand how pinMode made a difference. Does calling digitalWrite without setting pinMode first cause a different current to be sent out?

Voltage across relay coil was 5 V in both cases? Did setting pinMode give it more sufficient current somehow? I don't understand how base affected the transistor besides just turning it on and off in this case.

If you do not set it as output, it is input by default.

When you set it HIGH, it is the internal pull up, at in effect you have a 20k resistor in line. That means very little current.

ansonl:

ralphd:
What size resistor do you have between the output pin and the transistor base?

I tried a 330om resistor and no resistor at all and got the same results. What is the resistor supposed to do between output and base? I thought a digitalWrite of HIGH would just let the power through the transistor.

You only want to output enough current to saturate the transistor. I disagree with those who would say you can damage the transistor with the ~40mA the arduino can push into the base, but more base current WILL increase Vbe and Vce (for exactly how much look at the transistor datasheet). That means less power to the device you’re trying to switch on and more power wasted as heat in your transistor.

I disagree with those who would say you can damage the transistor with the ~40mA the arduino can push into the base,

What makes you think the arduino can only supply 40mA? That current is the limit you must not exceed not the limit of what a pin can supply.

I disagree with those who would say you can damage the transistor with the ~40mA the arduino can push into the base,

One does not 'push current' into the base. Once the the base/emitter forward voltage drop value is approached or exceeded (typically around .7vdc) there is the equivalent of a short circuit being placed on the arduino output pin. This is not a condition you want to subject an output pin to, use a suitable series resistor sized to ensure the collector/emitter junction is saturated at the collector voltage and current value being used.

Lefty

retrolefty:

I disagree with those who would say you can damage the transistor with the ~40mA the arduino can push into the base,

One does not 'push current' into the base. Once the the base/emitter forward voltage drop value is approached or exceeded (typically around .7vdc) there is the equivalent of a short circuit being placed on the arduino output pin.

Since we're being pedantic, I'll point out that it's not a dead short. With a .7v drop and 40mA the resistance would be 17.5 Ohms.

ralphd:

retrolefty:

I disagree with those who would say you can damage the transistor with the ~40mA the arduino can push into the base,

One does not 'push current' into the base. Once the the base/emitter forward voltage drop value is approached or exceeded (typically around .7vdc) there is the equivalent of a short circuit being placed on the arduino output pin.

Since we're being pedantic, I'll point out that it's not a dead short. With a .7v drop and 40mA the resistance would be 17.5 Ohms.

You don't seem to have come to terms that the 40ma rating isn't a automatic limit that cannot be exceeded no matter what the external load resistance is. The 40 ma specification is a maximum safety value that Atmel warns will damage the chip if exceeded. If pin output current could automatically limited to 40ma max then there would seldom be damaged output pins.

heres how you can use transistors to drive relays-one npn and one pnp driver example

retrolefty:

ralphd:

retrolefty:

I disagree with those who would say you can damage the transistor with the ~40mA the arduino can push into the base,

One does not ‘push current’ into the base. Once the the base/emitter forward voltage drop value is approached or exceeded (typically around .7vdc) there is the equivalent of a short circuit being placed on the arduino output pin.

Since we’re being pedantic, I’ll point out that it’s not a dead short. With a .7v drop and 40mA the resistance would be 17.5 Ohms.

You don’t seem to have come to terms that the 40ma rating isn’t a automatic limit that cannot be exceeded no matter what the external load resistance is. The 40 ma specification is a maximum safety value that Atmel warns will damage the chip if exceeded. If pin output current could automatically limited to 40ma max then there would seldom be damaged output pins.

Looks like you’re right about it being possible to damage the MCU:
Stresses beyond those listed under “Absolute Maximum Ratings” may cause permanent damage to the device.

However I’m still not convinced driving either of the two NPN transistors without a resistor would exceed the absolute maximums. The datasheet pin driver strength only goes up to 20mA, and extrapolating from that it looks like the internal resistance of the pin driver is ~25 Ohms. Add the resistance between the transistor base and emitter when saturated, and it might be hard to pass 40mA. With a dead short (< 1 Ohm), sure you can go over 40mA. But as I said, the base-emitter junction isn’t a dead short.

Why not use an optocoupler! In your circuit you can use it as an alternative of the transistor. You'll be able to trigger the relay coil without any physical contact with the arduino pins as well, sometimes it may be an added advantage. Instead of applying the base current to the transistor, it allows you to do the job by lighting an internal LED that will control the internal photo-transistor. Since the relay is used for implementing ON or OFF states, no need to worry about the amplification factor & other stuffs. Just need to take care of the C-E current rating & the input led current. Sharp817 optocoupler served my purposes. Mine was a JIHJIK SPDT 12V relay.

A quick look at a Sharp817 datasheet shows an absolute maximum output current rating of just 50ma. While there are some relays that will require less then that, it will limit the number of relays that will work. Those popular and cheap Asian E-bay relay modules utilize opto-isolators but rather then trying to drive the relay's coil directly with the optos output they use a switching transistor between the opto and the coil.

http://www.futurlec.com/LED/PC817.shtml

http://www.ebay.com/itm/New-1pcs-5V-10A-2-Channel-Relay-Module-Shield-Board-for-Arduino-ARM-PIC-AVR-DSP-/111246775165?pt=LH_DefaultDomain_0&hash=item19e6d3137d

ralphd:
Looks like you’re right about it being possible to damage the MCU:
Stresses beyond those listed under “Absolute Maximum Ratings” may cause permanent damage to the device.

However I’m still not convinced driving either of the two NPN transistors without a resistor would exceed the absolute maximums. The datasheet pin driver strength only goes up to 20mA, and extrapolating from that it looks like the internal resistance of the pin driver is ~25 Ohms. Add the resistance between the transistor base and emitter when saturated, and it might be hard to pass 40mA. With a dead short (< 1 Ohm), sure you can go over 40mA. But as I said, the base-emitter junction isn’t a dead short.

Do you know the I-V characteristic of a diode? In a nutshell, it is not linear like a resistor. When forward biased, as the voltage rises, the current increases exponentially. And I mean that in the strict mathematical sense, not the slang sense meaning “a lot”. Conversely, as the current through the diode increases, voltage increases logarithmically. Strictly speaking, yes, a B-E junction is technically not a dead short, but it’s one of the closest things you can get.

Taking your values at face value (25 ohms output resistance), a saturated BE junction will have no more than about 0.7V across it. That leaves 4.3V dropped through the pin driver. Basic Ohm’s law here: 4.3 / 25 = 172 mA.

172 mA not only exceeds the Absolute Maximum Rating of what a single pin can source (40 mA), it exceeds the AMR of what an entire port can safely source (100 mA), and comes close the the ABR the entire chip can draw (200 mA).

Are you convinced now that you need a resistor?