Sign in

Not a member? | Forgot your Password?

Search blogs

Search tips

Free PDF Downloads

Advanced Linux Programming

What Every Programmer Should Know About Memory

Introduction to Embedded Systems

C++ Tutorial

Embedded Systems - Theory and Design Methodology

Recent Blogs on EmbeddedRelated

April is Oscilloscope Month: In Which We Discover Agilent Offers Us a Happy Deal and a Sad Name
posted by Jason Sachs

Little to no benefit from C based HLS
posted by Christopher Felton

Unit Tests for Embedded Code
posted by Stephen Friederichs

DSPRelated and EmbeddedRelated now on Facebook & I will be at EE Live!
posted by Stephane Boucher

Using a RTLSDR dongle to validate NRF905 configuration
posted by fabien le mentec

Introduction to Microcontrollers

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

See Also


Find us on Facebook

Embedded Systems Blogs > Jason Sachs > Byte and Switch (Part 1)

Jason Sachs (contact)
Jason has 17 years of experience in signal conditioning (both analog + digital) in motion control + medical applications. He likes making things spin.

Would you like to be notified by email when Jason Sachs publishes a new blog?


Pageviews: 4617

Byte and Switch (Part 1)

Posted by Jason Sachs on Apr 26 2011 under Tutorials | Interface Circuitry | Circuit Design   

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:

  • limited drain-source breakdown voltage
  • parasitic capacitance
  • parasitic inductance

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.

Rate this article:
Rating: 4.25 | Votes: 4
posted by Jason Sachs
Jason has 17 years of experience in signal conditioning (both analog + digital) in motion control + medical applications. He likes making things spin.

Next post by Jason Sachs: Byte and Switch (Part 2)
all articles by Jason Sachs

Comments / Replies

Very important stuff explained in simple language.
3 years ago
Sorry, you need javascript enabled to post any comments.
Excellent explanation. I have been implementing such circuits for many years by copying from existing designs. Now I actually understand the functionality of R1.
3 years ago
Sorry, you need javascript enabled to post any comments.
hmmmm...its very nice..
3 years ago
Sorry, you need javascript enabled to post any comments.
Jason, Excellent information. Could you carry this example a bit further and explain how you would spec each component? For example, you say "we pick a 40V MOSFET rated for 1A", obviously it needs a threshold gate voltage <3.3V, are there any other key criteria to selecting the mosfet (there a ton to choose from with a lot of specs)? From there what are the criteria to select the diode, R1, R2. For example again, to select R1 do we use the 3.3V and the max current that the Mosfet can take at the gate to size the resistor using R = V/I? Trying to apply this example to a real world project. Realize this is an old post, thanks for any response. Jeff
1 year ago
Sorry, you need javascript enabled to post any comments.
asad abbas
in most mosfet switches there is a built-in free-wheeling diode,so do we still need to use this other diode in parallel to the solenoid??
2 months ago
Sorry, you need javascript enabled to post any comments.
Sorry, you need javascript enabled to post any comments.