Transistors, Relays, and Controlling High-Current Loads

Originally written on August 22, 2014 by Tom Igoe
Last modified on August 27, 2016 by Tom Igoe

Introduction

Related video: High Current Loads

When you’re using microcontrollers, you frequently need to control devices that need more electrical current than a microcontroller can supply. For this, you need electrical relays or transistors. These notes explain relays and transistors as they’re used for this purpose. In order to get the most out of these notes, you should know something about how electricity works, and you should know the basics of how a microcontroller works as well.

Relays

Related video: Relays

Digital output from a microcontroller is typically a low-amperage signal. For example, when you set a pin HIGH, the voltage coming on that pin is typically +3.3V or +5V, and the amperage that it can source is around 10 milliamps. This is fine if you’re controlling an LED, whose required amperage is tiny. However, most devices you’d want to control need more current than that to operate. You need a component in between your microcontroller and the device that can be controlled with this small voltage and amperage. Relays and transistors are most often used for this purpose. A relay is a switch that’s controlled by a small electric current. Relays take advantage of  the fact that when you pass an electric current through a wire, a magnetic field is generated surrounding the wire as well.  This is called induction. When you place two pieces of ferrous metal near a coil of wire and pass current through the wire, the magnetic field can move the two pieces of metal towards each other. Those pieces of metal can form a switch, which can be turned on and off by putting current through the coil.

relay_detail

Two relays, one whole and the other with the switch removed. The blue and white tube is the coil, and the glass vial contains the switch

Relay_switch_closeup

Detail of the switch inside the relay, magnified 20x

relay_connections   In the illustration above, you can see that there’s no electrical connection between the microcontroller circuit that’s controlling the coil of the relay and the lamp circuit. This is another advantage that relays offer. A schematic, of the relay circuit looks similar to the illustration above: relay_schem The current needed to move the shaft in the coil is very low (less than 10 milliamps) so the coil can be energized by an output pin of your microcontroller. The current that can flow through the switch, however, is much higher. The lamp circuit is separate from the microcontroller. It uses a separate power source, with the amperage and voltage needed to turn on the lamp. The power source, the lamp, and the switch side of the relay are all placed in series. When the coil is energized, the leaves of the switch are physically moved by the magnetic force created, the lamp circuit is completed, and the lamp turns on. Relays are also useful when you want to replace a switch in an existing electronic device. If you replace the switch with the coil of a relay, the microcontroller can control the device just as if the button were pressed.

Transistors

Related videos: Transistor Schematics, NPN Transistors, PNP Transistors, Darlingtons and MOSFETs

Because a relay is a mechanical switch, it can be somewhat slow. Relays take a few milliseconds to close, so they aren’t very effective when you want to turn them on and off rapidly. Sometimes you need to switch a high current circuit rapidly. In this case you would use a switching transistor. A transistor is an electronic device that can work as a switch. It allows control of a large current by a smaller current as does a relay. Unlike a relay, however, a transistor is not mechanical, and can operate much faster than a relay. There are several types of transistors and they come in two major classes: bipolar transistors, and field-effect transistors, or FETs. All transistors have some similar properties though. They all have three connections, referred to as the base, the collector, and the emitter (on FET transistors, the three connections are the gate, the drain and the source).When you apply a small voltage and current between the base of a transistor and the emitter (or the gate and the drain on a FET), you allow a larger current to flow from the collector to the emitter (or the drain and the source). One of the main differences between MOSFETS and bipolar transistors is that MOSFETS require negligible current on the base in order to activate. Among bipolar transistors, there are two types: NPN transistors, and PNP transistors. When you apply positive voltage to the base of an NPN transistor,  it turns on the collector-emitter connection  and allows current to flow from collector to emitter.  The equivalent MOSFET is called an N-channel MOSFET. When you apply voltage to the base of a PNP transistor, by contrast, the collector-emitter connection turns off, and  no current can flow from collector to emitter. The MOSFET equivalent is a P-channel MOSFET.    transistors We’ll use NPN transistors in the examples here, specifically a very robust transistor called a darlington transistor. This type of transistor is designed to switch high-current loads. The particular model used in these examples is a TIP120 Darlington transistor. You could use an N-Channel MOSFET with a protection diode (more on that below) in place of the darlington transistor. The IRF520 MOSFET would be the appropriate equivalent. The basic circuit for using a transistor to control a high-current load is simple. You connect a DC power source to one terminal of the load, then connect the second terminal of the load to the collector (or drain, for a MOSFET) of the transistor. The emitter (or source) is then connected to ground, and the base (or gate) is connected to the output of your microcontroller. When you take the output pin of the microcontroller high, the voltage difference between the base (or gate) and the emitter  (or source) allows current to flow through the load, through the collector  (or drain) to the emitter (or source) and to ground.   transistor_motor_schemNote how similar this schematic is to the relay schematic. The transistor here is serving the same function as the relay. However, it can switch much faster than the relay. In addition, because there are no mechanical parts, it will reliably function for more switching operations than the relay. However, current can only flow in one direction through a transistor. If the voltage on the collector (or drain) is lower than that on the emitter (or source), you can damage the transistor. The same is not true with a relay. There are three differences between this transistor circuit and the relay circuit above. The first is that you’re using a motor as the load, rather than an incandescent light bulb. Because motors are inductive loads (they work because of induction; for more, see the DC motor notes), they can create a reverse voltage when spinning down after you turn them off. Because of this, the second difference is the protection diode in parallel with the transistor. The protection diode routes any reverse voltage around the transistor, thereby protecting it. The third difference is that the microcontroller attached to the base (or gate) and the transistor’s emitter (or source) must have a common ground. If not, then the circuit will not work. You don’t need the diode if you’re controlling a resistive load like an incandescent lamp, but it generally doesn’t hurt to include it. Generally, if you are switching DC motors, solenoids, or other high-current DC devices which create motion, it’s better to use a switching transistor than a relay. The ideal way to control a motor is with an H-bridge, which is an array of transistors that lets you control not only speed but also direction. There’s more on that in the motor control notes.