Drivers 101 - learning how to control large loads with a small microcontroller
December 28, 2019 • tutorial
For most of our projects we have relied on switching LEDs ON or OFF. But what if we want to control something bigger than an LED, say, a lightbulb? Or many many LEDs that draw a substantial amount of current? In this tutorial we will learn how to use drivers such as transistors and relays to accomplish exactly that!
Why do we need drivers?
Typical microcontroller outputs can drive signals of up to 20mA, and often there is also a limit towards the total number of outputs that can sink 20mA at the same time. It is clear that whenever a microcontroller wants to drive more current than that we need to find an alternative: a driver!
Much like the tugboat in the image above, a driver allows a small circuit to switch large loads that by far exceed 20mA. In the rest of this tutorial we will go through some very useful examples that are applicable to a wide range of scenarios. Let's go!
What are typical loads?
In electronics, a load can be any device that draws current. It can be an LED, a light bulb, a resistor that just dissipated heat, but it can also be an inductance or a capacitor. Anything that draws current (and yes, that current can be direct current or alternating current) :)
In our case, however, and for the sake of this tutorial, let us focus on a few select loads that frequently occur in electronics, and let us understand the main parameters of these loads that make them very different to interact with an electronics circuit:
- Many LEDs or high-power LEDs are perhaps the simplest example of an electronic load. They require the same voltage as the original circuit (meaning around 3-12V) but a large current. In this case a simple transistor or perhaps a transistor array are perfectly fine.
- Electric DC motors are of course an entire topic on their own, and we won't go into a lot of details here. While the operating voltage and current may be in the typical range of the circuit, motors are inductive loads because they are based on magnetic fields inside a coil. This means that additional precautions have to be taken to protect the driver circuit from voltage spikes due to electromagnetic induction. You can use transistors for smaller motors, especially if you want to control their speed, but relays work fine.
- Line-voltage devices such as ceiling lights, heating systems, home entertainment systems, and so on, need to be treated carefully! After all, they work with line voltage which can be dangerous. Line voltage devices can be controlled via simple relays, and sometimes solid state relays are a good choice, too, which are based on the idea of thyristors and triacs.
The main focus of this tutorial is on how to use transistors, transistor arrays, and relays to control LEDs and small inductive loads. Make sure to be careful if you want to work with line voltages. Be safe :)
The main idea behind transistors is very simple: a small current can be used to allow a much larger current to flow. In this tutorial we won't go through all the details of how a transistor works, but instead focus on useful applications with microcontrollers.
Transistors have three terminals: the base, which can be thought of as the input of the transistor, as well as the collector and the emitter, between which the large current flows. There are also NPN as well as PNP transistors, which work roughly the same way, but are polarity-reversed. A very common package is the so-called TO-92 package, whose pinout you can see here:
Here is a simple sketch of how they can be used:
For NPN transistors a small current from the base to the emitter allows a large current to flow from the collector to the emitter. PNP transistors work the same way: a small current from the emitter to the base allows a larger current to flow from the emitter to the collector. Okay, but how does it work in practice? Here you can see a typical application:
This is just an example of course, and in a real-life application the LED would need a current-limiting resistor. Rather we should think of it as the large device we want to control with a small signal. But how does it work?
When the switch is the upper position, there is a small current flowing through the base of T1 to the emitter, and the LED1 turns on. When the switch is in the lower position, there is no current, and the LED is off.
In the right circuit the situation is reversed: in the upper position, there is no current flowing from the collector to the emitter, and LED2 is off. When the switch is in the lower position, however, there is flows current from the emitter to the base, and the LED turns on.
Now it is only a small step! We can replace the switch by an output of a microcontroller that can either be on or off. Here you can see an example:
For T1 I recommend the BC547C, and for T2 you can use the BC557C.
When the signal S1 is on, LED1 is on, and vice versa. When the signal S2 is on, LED2 is off, and vice versa. As you can see, a PNP transistor effectively inverts the signal.
There is one very important detail: in the NPN case, the LEDs can be switches against other voltages as well! What does that mean? In the above diagram, LED1 is connected to +5V. If you use the same circuit but adjust R2 to, say, 470Ω instead, then you might as well connect LED1 to +12V. In this case, the NPN transistor acts as a so-called level shifter. Not all voltages are permissible, though, and there is typically a maximum allowed voltage before the transistor fails, so make sure take a look at the datasheet or ask around :)
Last, I want to mention the very important snubber diode (or sometimes called flyback diode) that is needed whenever transistors are used to switch inductive loads. Here you can see a typical schematic:
When T1 is turned ON, the orange current is flowing through the load L1. When T1 turns OFF, however, the current wants to keep flowing (due to the electromagnetic law of induction). Where can that blue current go? If there is no snubber diode D1, that current will pile up against the collector of T1 and eventually cause it to short out and break. But if there is a snubber diode D1, that current can be redirected towards VDD. During regular operation the diode D1 does not interfere with anything else, because it is inserted in a non-conducting way.
We will see below that it is a good idea to include a simple snubber diode whenever we switch inductive loads such as relays with a microcontroller and a transistor.
If you need more than a few transistors, there are special integrated circuits that combine multiple transistors into one chip. These chips are called transistor arrays, and a very popular NPN version is the ULN2803A:
As you can see, it has eight inputs (pins 1-8) and eight collector outputs (pins 11-18). Pin 10 is ground, and pin 9 collects all the cathodes of the flyback diodes. Here you can see a typical application of the ULN2803A in combination with a CD4094 shift register which is very useful in microcontroller applications:
Again, the NPN transistor array works as a level shifter in this circuit since the operating voltage VDD can be different from the +5V that are used to control the shift register, and according to the ULN2803A datasheet the voltage VDD can be as high as 50V.
The UDN2891 is the PNP equivalent of a transistor bank, and is constructed in a very similar fashion, including the eight flyback diodes whose anodes (not the cathodes as in the NPN case, since PNP transistors switch towards ground) can be connected to ground (and not to VDD as in the NPN case).
Sometimes you can also find these drivers in combination with a shift register, which is quite a useful combination. In my experience, however, these ICs are always a bit more expensive and difficult to find, which is why I like to stick with a separate shift register and driver IC.
Relays belong to the class of electromechanical devices: they use a small electromagnet to physically open or close a switch. In that sense they are very simple devices, and they also galvanically isolate the control circuit (say, the microcontroller) from the load (say, a light bulb). Sometimes this galvanic isolation is very useful and required for safety reasons, and in those cases a relay is the obvious choice.
As we learned above, inductive loads such as relays require a snubber diode to protect the control circuitry from induced voltage spikes that occur whenever the coil is switched off. In some cases, relays even come with an integrated snubber diode, and in other cases it has to be added externally:
The electromagnet of a relay needs a certain voltage to trigger the electric contact, and typical relays require anything between 3V to 32V to do that. So make sure that when you purchase a relay for your 5V microcontroller circuit that it actually works with 5V.
Here you can see some typical relays that can be used with 5V microcontroller circuits:
They can typically switch up to 250V AC at currents ranging to a few Amperes. Make sure to check your relay's datasheet so that you don't exceed these numbers. Here are some more details:
- The relay on the left is a standard mechanical relay (type GR2L), and they come in many shapes and forms. They require 80mA at 5V to work, so a driving transistor is needed under any circumstances.
- The relays depicted on the right are so-called DIP reed relays: because they use a reed switch they are typically very small and only require 10mA at 5V to work, so they can be connected directly to a microcontroller. Moreover, this model comes in the dual-inline footprint also used with integrated circuits, so it can be nicely used in breadboard circuits (but remember that breadboards should not be used with line voltage). On the down side they are a bit more expensive and the total switching current is lower than for conventional relays. Check out their datasheet here.
Moreover you can find special relays that are bistable, meaning that a short pulse turns them on, and another short pulse turns them off. This is accomplished via a mechanical contraption inside the relay's body and can be quite handy at times.
So you might think: relays are great! But what is the downside of relays? I think there are three:
- Mechanical reliability. Because relays are mechanical devices, after some time the terminals show some wear and tear. For this reason, relays are not ideal for applications where they are sietched on and off very rapidly, since it dramatically reduces their lifespan. But of course there are also relays specifically built foe that purpose, so make sure you double-check in the datasheet!
- Switching speed is limited. Depending on the concrete size and built of your mechanical relay, the switching speed is limited to perhaps one or two operations per second. If you need to switch something on or off more frequently, a relay might nbot be your best choice.
- Only ON/OFF. Maybe this is completely obvious, but relays can only ever be completely ON or OFF. If you want to switch something only halfway on, then a relay is not your device of choice.
But if none of these are an issue to you, a relay is a great choice as a driver.
Thyristors, TRIACs, and switching alternating current
Is there a way to electronically switch an alternating current? So far it seems that relays are our only hope, but that is not true! Thyristors are electronic devices that can switch alternating currents. The name thyristor is an amalgamation of the words thyratron (a vacuum tube that was used to control currents a few decades ago, see also the video by CuriousMarc that the above image is taken from) and the word transistor. Sometimes they are also called silicon-controlled rectifier, or SCR for short. But how do they work?
A thyristor consists of a PNP and an NPN transistor connected as follows:
Let's break it down into smaller pieces and start at the beginning.
- Thyristors are switches, meaning that they can only be ON or OFF. This is the main difference to transistors, which are true analog devices that can be anything from totally ON to totally OFF.
- Thyristors are so-called latching switches. This means that after they are turned ON they stay ON, even if the signal that turned them ON in the first place disappears.
- The three terminals of a thyristor are called gate, anode, and cathode. The gate is used to switch the thyristor ON, and after the thyristor has been switched ON it conducts current flowing from the anode to the cathode. In the reverse direction it always acts as a diode.
- Let's look at T1 and T2 in the above diagram. When there is a signal on the gate G1, T2 becomes conducting and connects the base of T1 to ground. This in turn, see the section on transistors above, makes T1 conducting and connects the anode A1 to the cathode C1. Because T1 is now conducting, the signal at G1 can be removed, and the entire setup remains conducting.
- The symbol T3 is the standard symbol for a thyristor.
Okay, but how do you turn a thyristor OFF you might wonder? This is a problem when you use direct current, because there is no way to switch it off! For alternating current (also called AC), however, there is a way:
Because alternating current (AC) oscillates between its maximum amplitude and zero, there is always a moment when the current that flows through the thyristor crosses zero, an in this moment the thyristor turns off.
When you combine two thyristors in an antiparallel way you obtain a TRIAC:
TRIAC stands for triode for alternating current, and they are useful switches for AC loads. However, they also switch off whenever the load current crosses zero. This means that on their own, without additional circuitry, they cannot work as reliable switches.
There is a lot more to be said about thyristors, TRIACs, and various improvements, but for this article this is all I think we need. Why? Because we can use thyristors in solid state relays!
Solid state relays
The complicated turn-off behavior of thyristors and TRIACs is solved with additional circuitry, and one particular example are solid state relays (or SSR for short). These relays are literal black boxes that provide exactly the same functionality as their mechanical cousins, but they are electronic devices without moving parts. You may already have guessed it, they make use of thyristors with external circuitry that detects the zero-crossing of the load current and allows for precise control of the thyristors.
In that sense they are very easy to use, and you don't even need a snubber diode anymore. On the down side they are a bit more expensive than mechanical relays, but they last longer. One important detail is that they dissipate heat: Roughly 0.7V are lost across a solid state device, which cannot be avoided due to their use of semiconductors. This voltage drop is converted into heat when a certain current flows, and if that current becomes too much it actually demands the use of a heat sink. So make sure that you double check how much heat your solid state relay will have to dissipate when switched on.
The SSR depicted above is a heavy-duty relay of the type RM1A that has a built-in heat sink (the black metal). There are also versions with a smaller footprint, such as this one here:
As you can see (and you can read in the RP1A datasheet), this relay works with voltages from 4.25-32V and can switch voltages up to 60V.
One last comment: SSRs have the disadvantage that they are only available as single-throw switches, unlike mechanical arrays that are available as dual-throw versions as well.
A glimpse into power electronics...
There are a lot more interesting developments and devices out there used to drive heavy loads in electronics, but it is impossible to cover everything in detail in this article. Here are some of thee devices:
- GTOs (gate turn-off thyristors) can be switched OFF and are fully controllable switches, but the switch-off times are rather long (a few microseconds).
- IGCTs (integrated gate-commutated thyristors) have an improved switch-off time and can be used for applications with switching speeds of around 500Hz before the losses become too high.
- IGBTs (insulated-gate bipolar transistors) are built as thyristors but work as a transistor with very high switching speeds, voltages, and currents.
- Power MOSFETs (metal-oxide-semiconductor field-effect transistors) are the most common devices in power electronics, and are the most widely used power semiconductor device in the world. Unlike transistors and thyristors they require an electric voltage (rather than a current) to become conducting.
Especially field-effect transistors are very common and very useful devices, and if you are interested in a tutorial about those please let me know :)
Like in many other tutorials before you may ask: why do we need this? But I think that the topic of drivers is very important. In future projects we will see that this will come in handy in many different situations, because it allows us to connect low-power microcontroller circuits to the real world!
Thanks for reading!