Motors and Microcontrollers 101

Uploaded by nerdkits on 26.06.2009

Electric motors are everywhere: from a fuel pump at a gas station, to a remote-controlled
car, to the fan inside your computer. If you want to control a motor from a microcontroller,
there are a few things you need to keep in mind. Let's go over the basics of DC motors
including back EMF, resistive, and inductive effects, and see how those affect the design
of a motor control circuit. If we have a motor, and put a voltage across
it, the motor will spin. But if we put a switch in series with the motor, we'll only be applying
power when the switch is closed. So things get interesting when we have the microcontroller
control the switch. Let's think about what the technical requirements are for the switch.
In order to get there, let's take a quick look at the motor, which is just a generic
brushed DC motor we bought. Here's a quick look at another motor disassembled. And you
can see the contacts, the brushes, the rotor, and the coils, and you can see the permanent
magnets inside the casing. Here are a few experiments and observations
that you should really try for yourself.
Observation number 1: when I spin it by hand, I get a voltage. Guess what? This is how a
generator works. This voltage is called the back EMF and it is proportional to the speed.
If you remember back to your physics class, this voltage comes about because the permanent
magnets in the motor are moving with respect to the motor coils.
Number 2: we can even attach an LED here and spin it, and see that we can light it up.
Number 3: now, you can see when I spin it, it keeps spinning for a second or two even
after my finger disconnects from the gear just because of inertia.
Number 4: but now if I put a wire between the two motor terminals, shorting it out,
and now I try spinning it, it's actually harder to turn and also doesn't spin freely anymore.
This might be hard to see in the video, but if you've never tried it before, give it a
shot. You'll be surprised how much difference a little wire makes.
Number 5: we can measure the resistance of the coils. We'll get about 9 ohms. A little
trick we can do is to just measure the resistance of the multimeter leads and subtract that
from our earlier measurement.
Number 6: we can take our full-size multi-meter, put it on the 10 amp max current measuring
mode and wire it in series with the motor. You might think that we take the 9 volts of
the power supply and divide it by 9 ohms of the motor resistance we get about an amp.
But when we actually measure it, we get a much smaller current – only about .06 amps
when the motor is running with no load. That's because of the back EMF we talked about earlier,
which means that as the motor spins faster, there's actually a voltage drop due to the
electromagnetic and mechanical work being done. So there's less current drawn from the
power supply.
Number 7: when I first connect it, the current is higher. It's hard to see because it blinks
on the multimeter are pretty quick, but it's at least .13 amps for one reading.
Number 8: the 1 amp number does actually represent the stall current, which is the current of
the motor when it's not spinning. If I actually prevent it from spinning with my hand, and
connect the power supply, you'll see I do get up to about .8 amps. This is still less
than the 1 amp, but that's because the power supply is trying to limit the current now.
Only try this for a few seconds because you don't want the motor to overheat. Also, only
do it with a small motor like this where you aren't going to hurt yourself and where it's
mechanically not hooked up to anything else.
And finally, Number 9: when I touch the wires together, I get a small spark. It might be
tough to see on the video, but it's there. Even a small spark means big voltages --
hundreds or thousands of volts.
So with those 9 observations, we can now try to build a simple electrical model of the
motor. First, we've got a voltage source. This represents the back EMF and is proportionatl
to the speed. Then we have a resistor. This represents the resistance of the wires inside
the motor. And finally we have an inductor. This represents the energy stored in the magnetic
field, and that's the reason for the spark. This model actually explains all the nine
observations, and now that we have this model, we can take it and try to figure out how to
switch this motor on and off.
Basically, we have two major things to worry about. The first is the maximum current through
the switch. Even though we only measured a no load current of .06 amps, if the motor
is working under a load, it's going to have a higher current. And when it's first starting
up, it's only going to have the stall current of .8 amps or 2 amps. If we only worried about
the .06 amps with no load current like we measured, we're going to have all kinds of
problems, where either the motor won't start or the motor or switch will overheat. So it's
important to take into account the full maximum current that you're motor can experience when
you're designing your circuit.
The second thing to worry about is that there's an inductor here. Basically, when the motor's
working as normal, there's no problem; the current's flowing here. But when the switch
is opened, the inductor doesn't want its current to change instantaneously, so it'll make a
huge voltage here until you get a spark that crosses the gap. That's what we saw earlier
during our experiments. But that spark and these high voltages can damage any other components
in the circuit. We can actually fix this problem by adding one diode in parallel with the motor,
but in reverse. A diode only lets current flow in one direction, so when the motor's
on as normal, it's not doing anything. But when the switch is opened and this voltage
starts to rise, this diode will start to turn on and will allow current to flow in this
loop. This prevents the voltage from getting dangerously high and protects your components
down here. This diode is called a flyback diode in this configuration, and this is an
important part in driving any inductive load.
Like we talked about in the NerdKits Guide, the transistor can act as a voltage-controlled
switch. And for now we're going these 2N7000 MOSFETS that are included in the USB NerdKit.
Relays can also be used to control motors, but they have some advantages and disadvantages
of their own. And we'll have to cover those in a later video. When we put 5 volts on the
gate of the 2N7000, we're basically choosing to operate along this curve. So we get a maximum
current of about three-fourths of an amp. With this and the flyback diode, we've got
a shot at controlling the motor by using the MOSFET in place of the switch we were drawing
One more thing: we're going to add a resistor between the gate and ground. This is just
for those occasional times when the microcontroller isn't driving this pin, like when we're in
programming mode and we want the motor to stay off. If we make this a 100k resistor,
that's gentle enough to pull the gate to ground without interfering with normal operation.
So this is our whole switching circuit. We've got the motor, the MOSFET, the fly back diode,
and a pulldown resistor.
So here's the first demo. A few lines of code that read from a push button, debounce it,
and toggle the motor on and off each time I press.
And here's another quick demo with speed control using pulse with modulation (PWM). We can
let the microcontroller turn the MOSFET on and off very quickly as a way to adjust the
speed. We've got the temperature sensor hooked up as the control signal, so as it gets hotter,
the motor spins faster. As you can imagine, this might be useful for a cooling fan. We
talk more about pulse with modulation in a few of our other videos so please take a look
for a better understanding.
I hope this video gives you an idea of how to get started controlling motors with the
microcontroller. This is just the start, and the topic goes a lot deeper. If you're still
curious and want more things to think about, consider the power dissipation of the MOSFET,
the diode reverse recovery time, gate capacitance, turn on/turn off times – all other issues
like that. And that's before we even get to H-bridges and switching the direction of the
motor. So it might sound like there's a lot to think about -- and there is – but it
all starts with the tools you've learned in this video, just turning a motor on and off.
As always, you can find the source code and more information at our website at
And please send us some feedback. Let us know how you're working motors into your projects,
and let me know if you've learned anything from this video.