DC Motors: The Basics

These notes are heavily indebted to Gordon McComb’s Robot Builder’s Bonanza, second edition, which includes some excellent chapters on motors and motor use.

Introduction

Related video: Meet the Motors

When trying to move things with microcontrollers, you’re likely to use one of three kinds of motors: DC motors, RC servomotors, and stepper motors. Following is a brief introduction to these three. In order to get the most out of these notes, you should know something about how electricity works, and you should know the basics of how a microcontroller works as well. You should also understand how transistors are used to control high-current loads.

Motors convert electrical energy into mechanical energy so that you can move things in the physical world.  They are based on the electrical principle of induction. When you put electric current through a wire, it generates a magnetic field around the wire. The direction of the magnetic field is related to the direction of the electrical current.

 

Drawing showing the relationship between an electrical current and the magnetic field generated by the current. A wire crosses the drawing from left to right, with an arrow indicating that the direction of current flow is left to right. The line is surrounded by concentric rings indicating a magnetic field. An arrow on the rings indicates that the magnetic field flow is perpendicular to the electrical current flow, moving in a clockwise direction.
How induction works.

 

If there’s a magnet nearby, its field will interact with the wire’s magnetic field. It will either be attracted to the wire’s field, or repelled, depending on the relative orientation of the fields. The higher the current, the greater the magnetic field, and therefore the greater the attraction or repulsion. If you mount magnets on a spinning shaft surrounded by the wire, you have a motor (in the diagram below, the wire is arranged in two coils).  As the magnets are alternately attracted to one coil and repulsed by the other, it spins from one to the other, and you get circular motion.

Drawing of the mechanism of a DC motor. At the center of the drawing are two semi-circular magnets, labeled North and South, arranged to make a circle around a spinning shaft. Two coils of wire stand to the left and right of the magnets. The coils are joined together by a wire. The free end of the left coil is labeled +V and the free end of the right coil is labeled with an electrical ground symbol.
The basic mechanism of a DC motor.

 

All inductive loads (like motors, electromagnets, and solenoids) work on this same principle: induce a magnetic field by putting current through a wire, use it to attract or repulse a magnetic body. However, the principle works in reverse as well. When you spin a wire in an existing magnetic field, the field induces a current in the wire. So if you’ve got a motor spinning, and you turn it off, the fact that the motor’s coil is spinning in a magnetic field will generate a current in the wire while it’s spinning. This current comes back in the reverse direction of the current flow you generated to run the motor. When the motor isn’t attached to another source of electricity, you’d call this a generator, because the motor is now generating voltage. When the motor is connected to another source, it’s called back voltage, and it can cause damage to your electronics. Usually it’s stopped by putting a diode in parallel with your motor to stop the back voltage.

Motor Characteristics

There are a few characteristics common to all motors that you should keep in mind when looking for motors:

Voltage

The rated voltage of a motor is the voltage at which it operates at peak efficiency. Most DC motors can be operated somewhat above or below their range, but it’s best to plan to operate them at their rated voltage. Dropping below rated voltage reduces the motor’s power, and operating above the rated voltage may burn the motor out. Plan on the motor’s top speed being at rated voltage, and slowest speed at no more than 50% less than the rated voltage.

Current

Motors draw current depending on the load they’re pulling. Usually more load means more current. Every motor has a stall current, which is the current it draws when it’s stopped by an opposing force. This stall current is much greater than the running current, or current that it draws under no load. Your power supply for a motor should be able to handle the stall current with extra amperage to spare. Motors may draw near the stall current for a brief period of time when starting up, to overcome their inertia.

Speed

Motor speed is given in revolutions per minute (RPMs).

Torque

Torque is the measure of a motor’s pulling force. It’s measured by the force a motor can pull when the opposing force is attached to a shaft attached to its center rod. If the shaft sticks out a foot from the motor’s center, and the motor can pull one pound on that shaft, the motor’s torque is one foot-pound. Motor manufacturers haven’t standardized this measurement, so sometimes you will see it as ft.-lb., lb-ft., oz.-in, in.-oz., g-cm (gram-centimeter), and any other weight to length variation you can think of. Related video: Torque and Gearboxes

Drawing of the principle of torque. A motor is shown with its main axis drawn horizontally. There is a rod mounted on the motor's shaft, perpendicular to the main axis. THis rod will rotate when the motor is energized. The distance from the center of the shaft to the end of the rod is labeled as 1cm. A cube, labeled 1 gram, hangs off the end of the rod. An arrow indicates that the weight will be lifted when the motor rotates.
Torque illustrated. This motor can lift a 1 gram weight at a distance of 1 centimeter out from the center of rotation. Therefore, it can supply 1g*cm of torque.

Resistance

Often you’ll see a motor rated in ohms. This just gives you the resistance that the motor’s coil offers. Using Ohm’s Law (voltage = current x resistance), you can calculate the motor’s current draw if you know the rated voltage and the coil resistance.

Types of Motors

DC Motor

The DC Motor is the simplest of the motors discussed here. It works on exactly the principle discussed above. There are two terminals, and when you apply direct current to one terminal and ground the other, the motor spins in one direction. When you apply current to the other terminal and ground the first terminal, the motor spins in the opposite direction. By switching the polarity of the terminals, you reverse the direction of the motor. By varying the current supplied to the motor, you vary the speed of the motor. Specific techniques for doing these tasks are discussed below. Related video: Power to a DC Motor

DC motors are usually very fast, often spinning at several thousand revolutions per minute (RPM).

DC toy motor, hobby size. This motor is a metal tube with flattened sides, approximately 2 in. (5cm) long. a thin shaft at one end spins when the motor is on. Two small metal tabs or wires protrude from the other end to connect the motor to your circuit.
Small DC motor, 130 size

For more on DC motor control, see this lab for single-direction control, or this lab for controlling a motor in two directions with an H-Bridge.

DC Motor Control

There are two easily controllable parameters of a DC motor, direction and speed. To control the direction, you reverse the direction of the voltage through the motor. To control the speed, you pulse width modulate it.

Direction

To control a DC motor from a microcontroller, you use switching arrangement known as an H-bridge, consisting of four switches with the motor in the center. Here’s the schematic for a typical H-bridge:

Schematic drawing of an H-bridge. At the top is a vertical line labeled +V. It branches horizontally to feed four switches, two in series with each other on each branch. A motor is connected to the junction where each pair of switches meets. The four switches form the vertical sides of the letter H, and the motor forms the crossbar. At the bottom of the diagram, the ends of the bottom switches are joined, and connected to ground. The switches are labeled, clockwise from top left, 1,3,4,2.
An H-bridge, at its simplest, is composed of four switches with a load at the center of them.

 

When switches 1 and 4 are closed and 2 and 3 are open, voltage flows from the supply to 1 to the motor to 4 to ground. When 2 and 3 are closed and 1 and 4 are open, polarity is reversed, and voltage flows from the supply to 3 to the motor to 2 to ground. Related video: H-Bridge

An H-bridge can be built from transistors, so that a microcontroller can switch the motor, like this:

Schematic drawing of an H-bridge made with transistors. The drawing is similar to the previous schematic, but the switches have been replaced with transistors. They are labeled, clockwise from top left, Q1, Q3,Q4, Q2. Q1 (top left) and Q3 (top right) are P-channel MOSFET transistors. Q2 and Q4 are N-channel MOSFET transistors. The source of Q1 and Q3 are connected to +V, and their drains are connected to the motor and to the sources of Q2 and Q4, respectively. The drains of Q2 and Q4 are connected to ground. The gates of transistors Q1 and Q2 are connected to each other, and the gates of transistors Q3 and Q4 are connected to each other.
An H-bridge made of transistors.

This schematic uses MOSFETs, which are good for controlling motors. If you’re familiar with bipolar transistors, the base, collector, and emitter of a bipolar transistor correspond to the gate, source, and drain of a MOSFET. The top two transistors above are P-channel, meaning that they allow current to pass when

and the bottom two are N-channel, so that the proper two transistors always switch together. When the left control pin is high, transistor 1 (labeled Q1) turns off because it’s a P-channel and Q2 turns on because it’s an N-channel.  The same happens with Q3 and Q4. If you were using this circuit, you’d want to make sure that the control pins are always reversed; when one is high, the other is low. Related video: MOSFET Transistor

Although you can make your own H-bridges, it’s usually easier to use a controller manufactured specifically for the job. A pre-manufactured H-bridge chip will include diodes to protect the transistors from back voltage, sometimes a current sensing pin to sense the current the motor is drawing, and much more. There are many motor drivers available from various electronics suppliers. Look around to find one that suits your needs and price range.

Speed

A DC motor’s speed is proportional to the supplied voltage. If the voltage drops too far, the motor won’t get enough power to turn, but within a certain range, usually 50% of the rated voltage, the motor will run at varying speeds. The most effective way to adjust the speed is by using pulsewidth modulation. This means that you pulse the motor on and off at varying rates, to simulate a voltage. Related video: Why use PWM on DC Motors?

Gearhead Motor

Gearhead motors are a subset of DC motors. They have a box on the top of the motors containing a series of gears that slow the rotational speed of the motor down and increase the torque. They are useful when you don’t need a lot of speed, but you do need power. They are controlled exactly the same as regular DC motors.

 

Photorealistic drawing of a gearhead motor. It is a normal DC motor, but with a gearbox attached over the shaft. The image shows a cutaway view of the gears, indicating how the sequence of gears steps down the speed of the motor to increase torque.
Gearhead motor with the gears shown. Image from designworldonline

RC Servomotor

For a nice short intro to servos, see the Seattle Robotics page.

Servo motors are a variation on the gearhead motor coupled with a potentiometer to give feedback on the motor’s position. The gears of the gearbox on a servo are attached to a potentiometer inside the case, and the pot is turned by the turning of the motor. The pot is connected to a capacitor in a resistor-capacitor circuit (R-C), and by pulsing this R-C circuit, you give the motor power to turn. When the motor turns, it changes the resistance of the R-C circuit, which in turn feeds the motor again. By pulsing the R-C circuit, you set the motor’s position in a range from 0 to 180 degrees. Related video: Meet the motors – servomotor

RC servomotor shown with different horns for attaching the motor to mechanisms
a small RC Servomotor

Servos have three wires to them, unlike most DC and gearhead motors, which have two. The first two in a servo are power and ground, and the third is a digital control line. This third line is used to set the position of a servo. Unlike other DC motors, you do not have to reverse the polarity of a servo’s power connections to reverse its direction.

Hobby servos, the kind most often used in small physical computing projects, usually take a pulse of between 1-2 ms every 18-20 ms. They rotate 0 to 180 degrees depending on the pulsewidth. A pulse of 1 ms will turn the motor to 0 degrees; 2 ms will turn it to 180 degrees. A servo needs to see a pulse every 18-20 ms even when it is not turning, to keep it in its current position, so once you’ve moved the motor to a new position, it’s essential to keep pulsing it with the same pulsewidth to keep it there.

For more on Servo motor control, see this lab: Servo Motor Control with an Arduino, and this video: Analog Output – Servo

Stepper Motor

Stepper motors are different than regular DC motors in that they don’t turn continuously, but move in a series of steps. A stepper motor is a motor that has multiple coils, not just one. By energizing each coil in sequence, you attract the shaft magnets to each coil in the sequence, and you can turn the motor in precise steps, rather than simply rotating continually.

Photo of three stepper motors. The center one is opened up to show the coils inside.
Stepper motor

This design allows for very precise control of the motor: by proper pulsing, it can be turned in very accurate steps of set degree increments (for example, two-degree increments, half-degree increments, etc.). They are used in printers, disk drives, and other devices where precise positioning of the motor is necessary. Steppers usually move much slower than DC motors, since there is an upper limit to how fast you can step them (5-600 pulses per second, typically. However, unlike DC motors, steppers often provide more torque at lower speeds. They can be very useful for moving a precise distance. Furthermore, stepper motors have very high torque when stopped, since the motor windings are holding the motor in place like a brake.

To control a stepper, you use stepper driver that will energize the coils in the right order to make the motor move forward. There are plenty of libraries and driver modules and ICs that simplify the process. What follows is a low-level explanation of how steppers work.

Stepper Motor Control

There are two types of stepper motors, called unipolar and bipolar.  The difference is in their wiring. Unipolar steppers have all of their coils joined by a center wire. Bipolar steppers have two coils, which are not joined. Unipolar motors typically have five wires, while bipolars have four or six wires.   Their wiring works as shown below:

Drawing of the wiring for a unipolar stepper motor, showing two variations. In the drawing on the left side of the frame, labeled "5-wire unipolar stepper", four coils of wire radiate out from a central connection labeled "center wire. The other ends of the four coils are labeled "coil 1" through "coil 4". In the drawing on the right, labeled "6-wire unipolar stepper" there are two coils side by side, labeled "coil 1" and "coil 2". There is a connection in the center of each coil as well, and those center wires are joined together.
The wiring for unipolar stepper motors. The center wires for the two coils are tied together in a unipolar stepper.

The extra two wires in a 6-wire bipolar stepper allow you to use it as a 4-coil motor instead of a 2-coil, by using the center wire on each coil as a common supply or ground. In addition, you can turn a 6-wire bipolar into a 5-wire unipolar by joining the two center wires as shown below:

Drawing of the wiring for bipolar stepper motors, showing two variations. In the drawing on the left side of the frame, labeled "4-wire bipolar stepper", there are two coils next to each other, labeled "coil 1" and "coil 2". In the drawing on the right, labeled "6-wire bipolar stepper" there are two coils side by side, labeled "coil 1" and "coil 2". There is a connection in the center of each coil as well, but unlike the previous unipolar drawing, these two center connections are not connected to each other.
Wiring for bipolar stepper motors.

To determine which wire is which, consider the resistance of the coils. In a bipolar motor, the two coils will have the same resistance, and they are not connected to each other. So if you see infinite resistance, you have two wires on separate coils. When you find two pairs that have the same resistance, you’ve found the two coils of your bipolar stepper. In a six-wire bipolar motor, the resistance between the outside wires of a coil will be twice what it is between the center wire and either outer wire.  In a unipolar stepper, the resistance between the center wire and any of the other four will be the same, and the resistance between any two outer wires will be twice what it is from the center wire to any of the outer wires, as shown below:

Schematic drawing of a unipolar stepper motor. Four coils radiate out from a common center point. They are all joined at the common center, labeled 5. The free ends of the coils are labeled, clockwise from the top, coil 1, coil 2, coil 3, coil 4. If the resistance between 5 and any of the others is X ohms, then the resistance between any of the other pairs (e.g. 2 to 4, 3 to 4, etc.) is 2X ohms.
Schematic drawing of a unipolar stepper motor. If the resistance between 5 and any of the others is X ohms, then the resistance between any of the other pairs (e.g. 2 to 4, 3 to 4, etc.) is 2X ohms.

 

Schematic drawing for two bipolar stepper motors. The one on the left has two coils side by side, with four wires. The wires are labeled, clockwise from top left, 1, 3, 4, 2. The one on the right has two coils shown side by side, and each has a center connection in addition to the end connections, for a total of six wires. The wires are labeled, clockwise from top left, 1,3,6,3,2,5. In both cases, if the resistance between the ends of either coils is X ohms (for example, 1 to 2, 3 to 4), then the resistance between either end and the middle of a coil is 0.5X ohms (for example, 1 to 5, 2, to 5, 3 to 6, 4 to 6).
Schematic drawing for two bipolar stepper motors. In both cases, if the resistance between the ends of either coils is X ohms, then the resistance between either end and the middle of a coil is 0.5X ohms.

Like other motors, the stepper requires more power than a microcontroller can give it, so you’ll need a separate power supply for it. Ideally you’ll know the voltage from the manufacturer, but if not, get a variable DC power supply, apply the minimum voltage that the supply can generate voltage across two wires of a coil (e.g. 1 to 2 or 3 to 4) and slowly raise the voltage until the motor is difficult to turn. It is possible to damage a motor this way, so don’t go too far. Typical voltages for a stepper might be 5V, 9V, 12V, 24V. Higher than 24V is less common, and frankly, above that it’s best not to guess. Related video: Connect 12V Power Supply

To power each coil, you supply voltage one side of the coil while grounding the other side. Typically, you drive a stepper motor with an H-bridge or an array of power transistors or MOSFETS.

To move the stepper, you apply voltage to each of the coils in a specific sequence. Typical phasing could go like this:

Stepper motor wire stepping sequence

StepWire 1Wire 2Wire 3Wire 4
1highlowhighlow
2lowhighhighlow
3lowhigh lowhigh
4high low lowhigh

Once you have the motor stepping in one direction, stepping in the other direction is simply a matter of doing the steps in reverse order. Knowing the position is a matter of knowing how many degrees per step, and counting the steps and multiplying by that many degrees. So for examples, if you have a 2-degree stepper, and it’s turned 180 steps, then it’s turned 2 x 180 degrees, or 360 degrees, or one full revolution.

For more on stepper control, see the notes on stepper motor control and this Lab: Controlling a Stepper Motor With an H-Bridge.

For a more technical discussion of stepper motor control, see Control Of Stepping Motors, a tutorial, by Douglas W. Jones. Another good tutorial on steppers is at Ian Harries’ site.

Originally written on July 16, 2014 by Tom Igoe
Last modified on August 30, 2018 by Tom Igoe