Physical power switches are easy. Except that good physical power switches that can handle real power aren’t so easy. They tend to be big and clunky and expensive. If you appreciate good design, I guess you could say they are a real turn-off.
If you are already setting up transistors to turn LEDs on and off, why not expand this to turning everything on, such that all you need is the cheapest possible button or switch that can handle a few volts at a few milliamps? Or how about powering down parts of a project to save energy?
I’m going to cover a few practical cases where you are switching power on and off and, things being as they are with electronics, we’re going to mostly be talking about MOSFETs, almost like I’ve got a FET-ish for them.
Relays and contactors
Relays and contactors are mechanical devices for switching power on and off. Relays tend to be lower-power devices that can switch and route signals, where contactors are just about turning lots of power on and off.
Either way, there’s a physical metal contact and an electromagnet. Run power through the magnet and the metal contact gets bent towards or away from a second contact, roughly in the same way a switch work.
Like switches, they are mechanical devices that can only move so fast and wear out over time and make a “click” or “thunk” noise when operated.
Before transistors, relays were all we had. Old electronic switchboards for the telephone system and early electro-mechanical computing devices used relays.
You can get a device called a “Solid State Relay” that’s actually a blob of transistors and stuff that looks like some of the circuits I’m describing such that it looks like a relay except without all of the drawbacks. My 3D printer uses one of these SSR’s to control 110v AC for the printer’s bed.
Driving a MOSFET
There are a few different topologies for driving a MOSFET. In this schematic, I’ve got the simplest N-channel MOSFET circuit shown:
The simplest control circuit is a resistor or two, where neither resistor requires you to do any complicated calculations for a simple circuit. You need a pull-up (or pull-down) resistor to keep the MOSFET in a known state, maybe a 10k or 100k resistor, Rdown in this schematic. And, while you can get away with leaving it out, it’s handy to put a small resistor, maybe 100 ohms or so, between the gate and the MOSFET to protect the microcontroller against voltage spikes while switching, Rg in this schematic, where Rl represents whatever load you are driving.
In this case, you need to select a MOSFET that can be driven by your microcontroller’s voltage. Pretty much any logic-level MOSFET is going to work with a 5V microcontroller but MOSFETs that are happy with 3.3V are much harder to find, which I’ll get to later.
If you select for a logic-level MOSFET and keep the frequency below at least 1 kHz and aren’t driving particularly high voltages, this works just fine. This covers an astonishingly large number of cases, actually. As your performance requirements increase, you might need to actually calculate the resistors more carefully… but you might want to use a more sophisticated circuit to drive it instead.
You can construct driver circuits with more transistors to drive a MOSFET, but it turns out that there’s an easier way, so I’m skip most of that discussion and talk about “MOSFET Gate Drivers”, which put a bunch of hard stuff into a simple IC that’s frequently able to be found in a smaller package than the equivalent driver circuitry. These do two useful things:
First, they are able to switch a lot faster by channeling a lot more power than a microcontroller pin. A microcontroller pin’s ability to swing is measured in mA. The ability to swing for a gate driver is measured in full amps. This will fill or drain the capacitor in the gate much faster and therefore it switches faster, which means that you can PWM at a much higher frequency with the same heat generation or you can generate a lot less heat at the same PWM frequency.
Second, they generally have their threshold for signal levels set to be compatible with at least 3.3V logic, sometimes lower. However, they allow you to power the device with at least 5V. Sometimes they can handle voltages more like 12-24V, which can be quite handy because, if you look at the datasheet, even logic-level MOSFETS can sometimes have even lower resistance when driven at a higher voltage.
In this case, I’m showing a TC4427 gate driver, which can operate with a supply voltage up to 18V. In this example, I’m powering it with 12V, and the MOSFET at 48V, where maybe the microcontroller is only a 3.3v microcontroller.
Other devices have a built-in charge pump to generate the required voltage, although a lot of those devices aren’t designed to hold the required gate voltage with a charge pump indefinitely. Apparently unless it specifically states that it can, assume it can’t.
You can also get gate drivers that are designed to drive MOSFETs via transformers or opticouplers, which gives you isolation from the electricity you are switching, although it’s frequently cheaper and not that much more complicated to just use a cheap opticoupler to drive the gate driver.
High-side versus low-side driving
Relays and switches don’t really care which side they are on so the same device can work as a high-side or a low-side driver. What’s the difference? Well, look how Rl, the resistor representing whatever load you are driving, is positioned in the left circuit vs the right circuit.
A low-side driver sits between the device and ground. A single NPN BJT transistor or N-channel MOSFET works as a low-side driver.
A high-side driver sits between the device and power. A single PNP BJT transistor or P-channel MOSFET works as a high-side driver.
For driving non-intelligent LEDs, most of the time, you are using a low-side driver. There are two big reasons for this:
First, a low-side driver doesn’t care about the total voltage, just about the voltage it’s seeing at it’s terminal. You could have a 200 V power supply driving a strip of LEDs where the voltage drop is 180 V and the MOSFET at the end only needs to care about the 20V. Conversely, a high-side driver would need to be specced to handle the full 200V.
Second, an N-channel MOSFET is fundamentally more efficient than a P-channel MOSFET, so much so that there are devices that do electrical chircanery to make a N-channel MOSFET be able to be used as a high-side driver almost as if it’s a P-channel MOSFET… although the modern P-channel MOSFET is still really quite efficient.
Conversely, if you are actually turning something on and off like a microcontroller, you really want a high-side driver. The big problem with low-side drivers is that it means that the driven load is not at ground-potential, so if you used a low-side driver to turn a microcontroller board on and off, both rails of the board will be at 5 volts with respect to ground and shorting it against anything grounded is going to put negative voltage on the part… whereas if you use a high-side driver, it’s all just at ground potential.
H bridges
High and low side driving can work in tandem to route power.
For example, you can create an “H-Bridge”, so called because it kinda looks like one in a circuit diagram, with two pairs of high-side drivers and two pairs of low-side drivers. If you are driving a DC motor, this will allow you to reverse the flow of current through it. This is mostly the domain of driving motors, but there are some two-color christmas lights that use an H-Bridge.
Simple H-Bridges are fairly easy to make, however the simple H-Bridge has hazard conditions that will cause the device to self-destruct, so pretty much everybody instead purchases H-bridge driver chips that prevent the hazard conditions from occurring. There are chips that have everything you need as well as chips that are designed to drive external transistors.
Practical high-side drivers with P-Channel MOSFETs and inverters
A single P-Channel MOSFET
You can connect a SPST switch between the gate and ground. When the gate is grounded, power flows. When the gate is not grounded and the pull-up resistor causes the gate to equal Vcc, power stops flowing.
Coming from a digital background, you might realize that you’ve got a device where the drain pin is high when the gate pin is low and vice versa and comment that it looks like an inverter. If you replace the actual load with just a little resistor and use the drain as an output, what you have is a pMOS inverter, so it doesn’t just look like it’s an inverter, it really is one. Except that a CMOS inverter can handle a few mA but you have an inverter where each pin can drive many potentially many amps.
In some circumstances, you can stop here, turning power on and off for a high-powered device with a dinky little switch and a beefy P-Channel MOSFET and a pull-up resistor. There are problems, however.
Most importantly, it only works when the voltage you are switching is a few volts away from the voltage you are controlling the gate with. The Vgs(th) of a MOSFET is generally a few volts. If you are switching 12 volts and the control line is a 5v GPIO output, A 5v high means that Vgs is a little over -7 volts and a low means that Vgs is -12 volts, so it’ll never switch.
Furthermore, you can only switch where Vcc is within the allowable Vgss range, not the more generous Vdss range so you might have a MOSFET that can switch 60 volts but it’s only going to switch 20 volts.
An N-P MOSFET pair
The next step up is to use a second device to raise the voltage of the gate higher. An N-channel MOSFET is great for this.
There’s two ways you can see this. First, you could look all of the way up at a low-side switch and realize it’s a low-side switch switching a high-side switch. Alternatively, from the digital side, you could just see it as a second inverter, making it a buffer, except a buffer that’s both voltage-level-adapting and able to handle a lot more current.
Now, the P-Channel MOSFET’s Vgs is going to swing all of the way up to Vcc and then all of the way down to ground, controlled by a much lower voltage on the N-Channel MOSFET.
You can see the N-Channel MOSFET being a very simple easy gate driver. There are much more complicated gate drivers.
Doing everything with N-Channel MOSFETs
Generally speaking, a P channel MOSFET is going to be less efficient than an N channel MOSFET, although the actual difference has varied over time.
There’s nothing preventing power from flowing through an N-Channel MOSFET instead in much the same way.
However, the Vgs for an N-Channel MOSFET is positive instead of negative. Thus, if the gate voltage is the same as the source pin, the MOSFET is off. If you want to turn an N-Channel MOSFET on the voltage needs to be higher than the source instead of lower. Some sort of voltage multiplier, probably a tiny charge pump, is necessary to make this happen.
There are chips that are high-side drivers for an N-Channel MOSFET and will raise the voltage high enough, but apparently at least some of them are only designed for something that will be continuously switching on and off so if you want to use one as a fancy low-resistance power switch, you probably want to check to make sure the data sheet specifically tells you that it’ll stay on indefinately.
Practical reverse-voltage-protection using P-Channel MOSFETs
It’s fairly easy to stuff a battery the wrong way into the slot or otherwise screw up power. This will fry things. Wouldn’t it be nice if we made things such that this doesn’t happen?
Let’s start with the switch-less case: A single Schottky diode.
There are plenty of beefy Schottky diodes out there that can handle a few amps of power. All of them are going to reduce the voltage and eat up energy.
What if there was a better way?
Well, a P-Channel MOSFET is going to turn on if the gate is at around the same voltage as the source and then it turns off if the gate goes well below the source voltage. We can use this effect to shut off voltage if it goes negative:
The resistor and zener diode are there to protect the gate against too much voltage. Also note the orientation of the body diode in the MOSFET. If the drain and source were switched, the body diode would still allow voltage through.
Let’s go a little bit further.
Let’s start with a simple P-Channel MOSFET on/off switch, to the left, and combine it with the power switch, which gives us the circuit to the right.
Pololu sells this as a discrete module but I usually end up including it on the main circuit board. You can see the same parts are all still there.
Load switches, starting from a MOSFET.
Check out the progression from left to right.
First, you’ve got a regular old MOSFET.
Second, you’ve added a set of diodes to keep the gate within the allowable Vgss range. This makes the device more resistant to shocks from ESD as well as more durable otherwise.
Now, let’s add a second set of diodes, and maybe some control electronics. It’s going to eat up power from the gate, but you can use it to power some small logic to shut the gate down if it gets too hot or there’s too much current running through it. It still looks like a MOSFET, still drives like one, you might be able to take a design that had regular MOSFETs and blindly replace them with these self-protected MOSFETs and everything will just work.
Finally, you’ve added even more features. The gate drive is designed for being directly driven from a TTL or CMOS voltage level and it’s referenced against ground. This makes a huge set of use-cases simpler to design around because now it’s a magical device that just turns power on and off. However, this also means that you might not be able to use it everywhere a MOSFET gets used.
Sometimes, it’s easier to just package the driver electronics separately from the MOSFET, so you are building a load switch with a MOSFET or two driven by a MOSFET controller.
Wirehead’s first rule of power-electronics states that “If you want to do something with power, somebody’s probably packaged it as a load switch or a MOSFET controller.”
There’s a lot of devices that are packaged as a load switch or as a gate driver and that mostly depends on if you can fit the right size of MOSFET and the control logic into the desired package size. If you can fit everything into one package, it’s a load switch, otherwise it’s a MOSFET controller.
So, returning to the pair of MOSFETs above, this is an example of the MAX4866 chip. One of the two MOSFETs is now N-channel (fed by a charge pump) and it also contains extra features like cutting off the power if it’s too high.
There’s a bunch of different devices like this!
Fully-featured load switches can really take a load off your design effort
A brief list of the features available in various load switches:
- Over-voltage cutoff, to protect a circut if you’ve accidentally connected 12v to the 5v line
- Under-voltage cutoff, so a circuit won’t get flaky at low voltage
- Reverse-battery protection, to prevent backwards power wiring from damaging the circuit
- Backflow prevention, to prevent power from going backwards through the device. Note that this is not the same as reverse-battery protection!
- Current limit, to shut off or otherwise limit the amount of power a device can use, like a very intelligent circut breaker
- Circuit discharge on power-down, so there’s not power sitting in the capacitors
- Soft-start, to ease the power on and help sequence the load
- Easier switching, including sometimes a full on/off interface that you can drive with a GPIO pin
- A diagnostic interface, so you can generate intelligent error codes to describe exactly what’s broken.
You can fit a pretty full-featured load switch for 5v or less and maybe an amp or three of power into a SOT-23 package, including the MOSFETs required.
There are beefy load switches that can handle 20+ amps of power at 24+ volts in substantial packages, say a D2PAK.
You can also find relatively tiny SOT-23 or SOIC parts that are designed to drive external MOSFETs, which enables you to size the MOSFET separately from the driver electronics.
You’ll find devices labeled “Ideal diode” or “Ideal diode controller” that use a MSOFET plus some logic to prevent reverse batteries and power backflow. You can even find some that are fast and beefy enough so that they can work as if they are rectifier diodes.
Not everything that’s basically a MOSFET controller or gate-driver or load-switch is going to be labeled as such, you’ll just look on the datasheet and it’s a bunch of control logic wrapped around one or two MSOFETs.
Wrapping up
Going back to my example, it’s important to realize just how amazingly large of a lever semiconductors can be. A GPIO pin for a 3.3v device might be able to output 10-20 mA of current where you’ll get about 2.6v of power, and you can easily switch hundreds of watts of power at higher voltages.