Digital output from a microcontroller is typically a low-amperage signal. For example, when you put a pin HIGH on the microcontroller (in Wiring/Arduino, it’sÂ
digitalWrite(somePin, HIGH); ), the voltage coming from that pin is typically +3.3V or +5V, and the amperage coming from it is typically 10 milliamps or less. This is fine if you’re controlling an LED, whose required amperage is tiny. Most devices you’d want to control require more current than that. In order to control them, you need something in between your microcontroller and the device that can be controlled with this small voltage and amperage. Relays and switching 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. When you place an iron shaft inside a coil of wire and pass current through the wire, the magnetic field moves the iron shaft. If that iron shaft is part of a switch, the switch can be turned on and off by putting current through the coil, which moves the shaft, closing the contact (see diagram below).
In a schematic, a relay looks like this:
Note the diode in parallel with the load here. This is used only when the load is a motor, solenoid, or some other inductive load, and when it is switched off, will generate a blowback voltage. The diode protects against this. See my motes on motors for more detail.
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 our microcontroller. The current that can flow through the switch, however, is much higher. The lamp circuit is mostly separate from the microcontroller. A separate power source, with the amperage and voltage needed to turn on the lamp, is used. The power source, the lamp, and the switch side of the relay are all placed in series, and the opposite side of the switch is grounded. When the coil is energized, the switch is physically moved by the magnetic force created, the lamp circuit is completed, and the lamp turns on. Notice that the coil and the lamp circuit are connected to the same ground, however. This applies only if we are switching a DC circuit (more on AC later).
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.
Because a relay is a mechanical switch, it can be somewhat slow (a few milliseconds to close). So relays 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 a device that can work as a switch or an amplifier. 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. For the moment, we’ll discuss using transistors as switches.
There are several types of transistors and they come in two major classes: bipolar transistors, and field-effect transistors, or FETs. For the moment, we’ll be talking only about bipolar transistors. All, however have some similar properties. 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 source and the drain). By putting a small voltage/current on the base of a transistor, you allow a larger current to flow from the collector to the emitter.
Among bipolar transistors, there are two types: NPN transistors, and PNP transistors. NPN transistors require a small positive voltage on the base relative to the voltage on the collector to turn on, whereas PNP transistors require a small negative voltage on the base. 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 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 of the transistor. The emitter is then connected to ground, and the base is connected to the output of your microcontroller through a resistor (the transistor only needs a small voltage, about 0.7V, to turn on, so the resistor limits the voltage to the base). When you take the output pin of the microcontroller high, the base gets a voltage, allowing current to flow through the load, through the transistor, and to ground.
Note 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. 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.