4
\$\begingroup\$

It appears most people drive relays through a transistor rather than directly from a digital output pin on an Arduino. I had wired the output pin directly to my relay before realizing this and it worked fine. What's the transistor for?

Is it to protect the output pin from exceeding the max current if the relay coil has too low a resistance?

Or, is it to conserve energy taking advantage of the common emitter amplification?

To produce less heat perhaps?

\$\endgroup\$
14
\$\begingroup\$

It appears most people drive relays through a transistor rather than directly from a digital output pin on an Arduino.

I had wired the output pin directly to my relay before realizing this and it worked fine.

So far, you have been lucky.

What's the transistor for?

The GPIO ports tend to have high output impedances. This roughly translates to "there are lots and lots of relatively low-power transistors inside the IC which can't put out much current either because they simple are designed for that or because the IC couldn't dissipate that much heat quickly enough." There's also very little need to put out much current - why do that (and open up Pandora's Box of "how far do I go? Do I put out 1mA? 100mA? 10A? 100A?") when you can just output a small signal to drive external amplifiers?

Is it to protect the output pin from exceeding the max current if the relay coil has too low a resistance?

This is on the right track. More on this later. And before, actually.

Or, is it to conserve energy taking advantage of the common emitter amplification?

If you simply/naively add a transistor at the output, using the same voltage rails, this will technically actually use more power since you'll be using the current output of the IC as well as the current of the load.

To produce less heat perhaps?

To produce less heat from the IC, more accurately.

Really, the GPIO outputs are meant nearly exclusively for signaling. In Arduino's case, they DO beef-up the outputs a bit so you can drive LEDs and the like, but this is very much not the case in most microcontrollers.

The transistor does several things:

(1) It provides protection from low-impedance loads which'll want to draw too much current. A BJT and resistor can be configured such that you always know the maximum current draw of the load, as seen by the IC. A MOSFET (my personal favorite) can be configured so the IC sees an extremely high-impedance load, which may be desirable in this case. (Note: MOSFETs tend to be slower, but the Arduino can't go fast enough for this to be particularly meaningful).

(2) It provides minimal back-current protection (because of the diodes inside the external transistor).

(3) It allows you to put the power-hungry things elsewhere. Transistors drop voltage inside of them, which means they consume power and heat up. If you're driving a 100mA load, a single-stage BJT will burn 70mW. That 70mW is fairly significant to the IC, but really insignificant to an external transistor (which can have a dedicated heat-sink if needed). In this case, the external transistor may draw 1mA from the IC (assuming a BJT beta of 100), so now the IC has to dissipate only 700µW while the external transistor takes care of the 70mW.

(4) It allows the other circuit to have different power rails. The IC might run at 5V, but you might want to control a 12V load (like a small motor). The two sides of the transistor don't have to have the same rails - so you can use this to link two sides of a system. This is not a particularly good idea when controlling, say, 120V with a 5V microcontroller, but 12V isn't a big deal.

(5) It allows you to convert the 5V output into a current-controlled source if you want to. This is particularly important in some environments.

Transistors are magical devices that do lots and LOTS of very, very nice things. They come in different sizes for a reason, and the ones inside microcontrollers are small and can only do small things.

\$\endgroup\$
  • \$\begingroup\$ A very well written answer! \$\endgroup\$ – Jasser Mar 5 '16 at 6:42
14
\$\begingroup\$

If the relay coil requires a small enough current, then you can drive it directly of the microcontroller (μC) pin. I have done that myself too.
Importantly, you still have to have a flyback diode.

If a coil requires larger current than an output pin can provide (sink or source), then a transistor (or MOSFET) is used. It can provide more current.

If the output pin can't push enough current for the relay, then you'd have an additional power source with more available current power the relay and the transistor switches it?

The μC and relay coil can share the same power supply in many cases. A typical μC pin can source or sink about 20mA. A common transistor like 2N3904 can switch 100mA, and larger transistor can switch more.

It's also possible that a relay coil is powered from a power supply with different voltage. For example, the μC is powered from +5V and the relay coil is powered from +12V. In this case, the transistor acts as a low side switch.

Related threads:
Why do I need a transistor to control a relay with an arduino, and which one should I get for this circuit?
What is the point of the transistor to drive the relay coil?

\$\endgroup\$
2
\$\begingroup\$

Yes, it could be said that it protects the output pin from supplying excessive current. Also some (most) relays require more throw current than an MCU GPIO pin can supply.

Did you include a freewheeling diode to protect the relay driver from the backlash when you release the relay? Failing to do so will kill your driver pretty quickly.

\$\endgroup\$
  • 2
    \$\begingroup\$ Have you actual hand-on experience in killing outputs this way? It seems unlikely (not that I suggest leaving it out, in fact, I strongly suggest using a Schottky diode to prevent substantial current from flowing through the chip in ways that are not characterized and could cause malfunction or entire chip latch-up). \$\endgroup\$ – Spehro Pefhany Mar 9 '15 at 1:38
  • \$\begingroup\$ @markt I did add a flyback diode. Thanks. :) So, is this correct then? Each GPIO pin on my Arduino has a 40mA limit. The 5V pin, however, should have about 500mA available when plugged into usb? Thus, if the relay needs more than 40mA, then the output can saturate the transistor which lets power from the 5V rail flow through the relay without letting magic smoke out of yet another Ardunio? \$\endgroup\$ – D. Patrick Mar 9 '15 at 1:43
  • \$\begingroup\$ @SpehroPefhany, I used a 1N4007 I had on hand. Why use a Schottky? I thought Schottky was typically used when high switch speeds were needed? Is there a benefit of a Schottky in a flyback configuration? \$\endgroup\$ – D. Patrick Mar 9 '15 at 1:44
  • 1
    \$\begingroup\$ @D.Patrick In this case, switch speed is called for. The back-EMF spike from the relay coil is fast. \$\endgroup\$ – Nick Alexeev Mar 9 '15 at 1:47
  • 1
    \$\begingroup\$ @D.Patrick The voltage drop is also (generally) lower. If you put the Schottky in parallel with a regular silicon junction, the Schottky will conduct most of the current. It doesn't matter if you have a discrete transistor or ULN2003, but for some chips it does matter because there are isolation junctions or ESD structures that should not conduct current under operating conditions. Reverse recovery time is not an issue but turn-on might conceivably be (depends on distributed capacitance of the coil). \$\endgroup\$ – Spehro Pefhany Mar 9 '15 at 1:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.