Chapter 1: Beginnings
Chapter 2: Further Beginnings
Chapter 3: Hello World
Chapter 4: More On GPIO
Chapter 5: Interrupts
Chapter 6: More On Interrupts
Chapter 7: Timers
Chapter 8: Adding Some Real-World Hardware
Chapter 9: More Timers and Displays
Chapter 10: Buttons and Bouncing
Chapter 11: Button Matrix & Auto Repeating
Chapter 12: Driving WS2812 RGB LEDs
Would you like to be notified by email when Jason Sachs publishes a new blog?
Imagine for a minute you have an electromagnet, and a microcontroller, and you want to use the microcontroller to turn the electromagnet on and off. Sounds pretty typical, right?
We ask this question on our interviews of entry-level electrical engineers: what do you put between the microcontroller and the electromagnet?
We used to think this kind of question was too easy, but there are a surprising number of subtleties here (and maybe a surprising number of job candidates that were missing them), so read on.
Here's a sketch and some more details to get you thinking.
The microcontroller is powered off of 3.3V. The electromagnet is designed to run off a 24V supply, and we'll connect one side of it to the positive terminal 24V supply and the other side... well, we'd like to connect it to the negative terminal of the 24V supply when we want the electromagnet on, and disconnect it when we want the electromagnet off.
The usual answer is to use either a relay or an N-channel MOSFET, hooked directly as shown below for a MOSFET (Q1). A relay in this situation usually isn't practical, because a MOSFET is faster and cheaper, and the relay requires more current to energize the coil than a microcontroller can deliver. So we use a MOSFET. Great... except there are some problems.
Let's pick a MOSFET: let's say the electromagnet draws 200mA or so at 24V, for a power consumption of 4.8W. In order to have plenty of design margin, we pick a 40V MOSFET rated for 1A with proper heat sinking. Let's hook it up, and use the microcontroller to turn on Q1. Then we get voltage across the electromagnet coil, which looks kind of like an inductor with some series resistance. Because of the inductance, it takes a little while for the current to build up, usually in the 5-50 milliseconds range, until it reaches the steady-state current of 200mA.
Now we decide to turn off Q1... BANG! There's a pop, along with that pungent smell of burnt silicon and a wisp of smoke from the poor little MOSFET. Alas, we've let the magic smoke out, because there was still energy stored in the electromagnet's inductance, and it didn't have anywhere to go except through Q1. But the switch was turned off, you say! Well, sure, but remember, we bought a 40V part, and the inductance is going to generate as much electromotive force as necessary to keep the 200mA of current flowing until its stored magnetic energy dissipates, and 40V is nothing. If you measured the voltage between the MOSFET's source and drain with an oscilloscope, you'd probably see 50-60V for a few milliseconds during this "event": the breakdown voltage of the transistor has been reached, at which point it conducts whether you like it or not.
The fix for this is to add a freewheeling diode D1, as shown below. When Q1 turns off, the current flowing through the electromagnet flows around in a circle through D1, and with a diode drop across the coil, the current decays slowly.
There are also two resistors that should be in the circuit as well. R1 is a series resistor, usually in the 100-1Kohm range, between the microcontroller and the transistor, which serves a few purposes. The main one is to limit the current into the MOSFET gate during turn-on. MOSFETs don't need any gate current in steady-state, but they have parasitic capacitances between the gate and the other two terminals, and to turn that switch on, you need to charge them up. Most MOSFET manufacturers specify gate capacitance (Cgs and Cgd for gate-source and gate-drain) and gate charge (Qgs and Qgd for gate-source and gate-drain charge) -- we don't buy from manufacturers that don't specify these parameters. In any case, if you hook the MOSFET gate directly to the microcontroller output, the current that flows during the turn-on and turn-off transients can exceed the rating of the microcontroller. So you want to limit this current to a reasonable value, put a resistor there. It affects the turn-on and turn-off speed of the transistor, so it shouldn't be any larger than necessary.
The other reasons for adding R1 have to do with fault propagation and high-frequency oscillation. If Q1 fails, sometimes the gate and drain may be shorted together. Having R1 in the circuit can prevent damage to the microcontroller. The MOSFET also has parasitic inductance in its source lead, and this can cause some nasty switching transients; having at least 10-20 ohms will dampen any potential oscillation.
The resistor R2 is there so that when the microcontroller is powering up, or is held in reset, and its internal output is high-impedance, the MOSFET Q1 has a gate that is held at a defined voltage. If you don't hook up R2, Q1's gate is floating when the microcontroller is in reset. If you add R2, you ensure the gate is pulled low and the transistor Q1 is off.
Whew! That's a lot of thought for a simple switch.
To put it another way, think about the ways in which we've discussed that a MOSFET is not an ideal voltage-controlled switch:
Part 2 of this blog will cover some other limitations of MOSFETs and explain why there are better alternatives in some other "simple" switch applications.
Add a Comment