Controlling Stepper Motors

Introduction

Stepper motors are useful for when you need to rotate a full 360 degrees, but need to position your motor at a particular angle. What follows is a more detailed introduction to unipolar and bipolar stepper motors and how to control them from a microcontroller.  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. You should also understand how DC motors work.

As you learned in the introduction to motors,  stepper motor is a motor controlled by a series of electromagnetic coils. The center shaft has a series of magnets mounted on it, and the coils surrounding the shaft are alternately given current or not, creating magnetic fields which repulse or attract the magnets on the shaft, causing the motor to rotate.

Related Videos

There are two basic types of stepper motors, unipolar steppers and bipolar steppers.

Unipolar Stepper Motors

Unipolar steppers motor have five or six wires. The five-wire version has four coils which are all connected on one pole. Six-wire motors are actually bipolars,  two coils divided by center connections on each coil. The center connections of the coils are tied together and used as the power connection. They are called unipolar steppers because power always comes in on this one pole.

 

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.

Bipolar stepper motors

The bipolar stepper motor usually has four wires coming out of it. Unlike unipolar steppers, bipolar steppers have no common connection. They have two independent sets of coils instead. You can distinguish them from unipolar steppers by measuring the resistance between the wires. You should find two pairs of wires with equal resistance. If you’ve got the leads of your meter connected to two wires that are not connected (i.e. not attached to the same coil), you should see infinite resistance, or no continuity. Some bipolar steppers have a center connection on each coil. These center connections can be joined to turn a 6-wire bipolar stepper into a unipolar stepper.

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.

Like other motors, stepper motors require more power than a microcontroller can give them, 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 (hopefully 3V or so), apply 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 for small steppers, and frankly, above that level it’s best not to guess.

H-Bridge Control of Stepper Motors

To control the stepper, apply voltage to each of the coils in a specific sequence. Both types of stepper motor can be controlled with an H-bridge (related video).  The sequence would go like this:

Stepper motor wire stepping sequence

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

The circuits for controlling a unipolar stepper or a bipolar stepper from an H-bridge are very similar. In both cases, you have four ends of coils that go to the four outputs of the H-bridge. The difference is that for the unipolar, you also have a common center wire. That wire can be attached to the same motor voltage supply that feeds the H-bridge, or it can be left disconnected. If you do the latter, you’re treating the unipolar motor as if it had two separate coils — in other words, as if it were a bipolar stepper.

Here’s the wiring for a unipolar stepper to an H-bridge:

Schematic drawing of an h-bridge and unipolar stepper motor connected to an Arduino. The Arduino is connected to the inputs of an L293D H-bridge like so: Digital pin 8 is connected to the H-bridge's input 1 (physical pin 2). Digital pins 9 through 11 are connected to the H-bridge's 2 through 4, respectively (physical pins 7, 10, and 15). The H-bridge's enable pins (physical pins 1 and 9) are connected to +5 volts. The H-bridge's ground pins (physical pins 4, 5, 12, and 13) are connected to ground. The H-bridge's motor supply pin (pin 8) is attached to +12 volts. The H-bridge's logic supply pin (pin 16) is connected to +5 volts. The unipolar stepper motor's center wire is connected to +12 volts. The four ends of the coils, labeled A, B, C, and D, are connected to the H-bridge's output pins, physical pins 4, 6, 11, and 14, respectively.
Schematic diagram of a unipolar stepper motor wired to an H-bridge and an Arduino.
Breadboard view of an L293D h-bridge connected to an Arduino for driving a stepper motor. An Arduino Uno on the left connected to a solderless breadboard, right. The Uno's 5V output and ground are connected to the side rows of the breadboard on both sides to create voltage and ground bus rows on the sides of the breadboard. The H-bridge straddles the middle of the breadboard in rows 10 through 16. The H-bridge's pins 1, 9, and 16 are connected to the left or right voltage buses of the breadboard, whichever is closest to their side. An external DC power jack connects to the right side ground bus of the breadboard from its negative terminal, and its positive terminal connects to pin 8 of the H-bridge. The motor's pink wire (wire 4) is connected to the same row as pin 3 of the H-bridge. The motor's orange wire (wire 2) is connected to the same row as pin 6 of the H-bridge. The motor's yellow wire (wire 3) is connected to the same row as pin 11 of the H-bridge. The motor's blue wire (wire 5) is connected to the same row as pin 14 of the H-bridge. The motor's red wire (wire 1) is connected to the positive terminal of the DC power jack.
Breadboard view of a unipolar motor connected to an H-bridge and an Arduino.

Notice that the center pole is attached to the 12V power supply as well as the H-bridge. You can also run the unipolar stepper without connecting the center pole in this way. If you do, you’re basically operating it as a bipolar stepper.

To control a bipolar stepper motor, you give the coils current using to the same steps as for a unipolar stepper motor. However, instead of using four coils, you use the both poles of the two coils, and reverse the polarity of the current across the coils.  Below is the circuit for that.The circuits for a unipolar and for a bipolar motor are the same, except for the center wire of the unipolar motor:

Schematic drawing of a bipolar stepper motor connected to an H-bridge and an Arduino. The drawing is identical to the unipolar schematic above, except there is no center wire for the stepper motor.
Schematic drawing of a bipolar stepper motor connected to an H-bridge and an Arduino.
Breadboard drawing of a bipolar stepper motor connected to an H-bridge and an Arduino. The drawing is identical to the unipolar breadboard drawing above, except there is no center wire for the stepper motor.
Breadboard drawing of a bipolar stepper motor connected to an H-bridge and an Arduino.

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 1.8-degree stepper, and it’s turned 200 steps, then it’s turned 1.8 x 200 degrees, or 360 degrees, or one full revolution.

Two-Wire Control

Thanks to Sebastian Gassner for ideas on how to do this.

In every step of the sequence, two wires are always set to opposite polarities. Because of this, it’s possible to control steppers with only two wires instead of four, with a slightly more complex circuit. By using an NPN transistor on each pair of wires, you can turn one off while the other goes on. The transistor’s base is connected to the first pin via a 1-kilohm resistor and to the output pin of the microcontroller.  The second pin is connected to +5V through a 10-kilohm pullup resistor, and the transistor’s collector is also attached to that pin. The transistor’s emitter goes to ground. When the transistor turns on, it provides a path of least resistance to ground for the current coming through the pullup resistor, and grounds the second H-bridge pin at the same time. So pulling the microcontroller’s output pin takes the first H-bridge pin high and the second pin low simultaneously. Duplicate this circuit for the third and fourth pins of the H-bridge, and you’ve got two-wire control of the bridge and the motor.

The circuits for two-wire stepping are as follows. The circuits for a unipolar and for a bipolar motor are the same, except for the center wire of the unipolar motor:

Unipolar stepper two-wire circuit:

Schematic drawing of a unipolar stepper motor connected to an H-bridge and an Arduino, 2-wire configuration. The drawing is similar to the unipolar schematic above; the motor wiring is the same, but the control from the microcontroller is different. Digital pins 8 and 9 are the only two pins controlling the H-bridge. Digital pin 8 is connected directly to the H-bridge's input 1 (physical pin 2 onthe H-bridge). Digital pin 8 is also connected to a 1-kilohm resistor, and the other side of the resistor is connected to the base of an NPN transistor. The collector of the transistor is connected to the H-bridge's digital input 2 (physical pin 6) and to a 10-kilohm resistor. The other end of that resistor is connected to +5 volts. The emitter of the transistor is connected to ground. Digital pin 9 is connected to the H-bridge's inputs 3 and 4 in the same way: specifically, digital pin 9 is connected directly to the H-bridge's input 4 (physical pin 15 on the H-bridge). Digital pin 9 is also connected to a 1-kilohm resistor, and the other side of the resistor is connected to the base of an NPN transistor. The collector of the transistor is connected to the H-bridge's digital input 3 (physical pin 10) and to a 10-kilohm resistor. The other end of that resistor is connected to +5 volts.
Schematic drawing of a unipolar stepper motor connected to an H-bridge and an Arduino, in a two-wire configuration.
Breadboard view of an L293D h-bridge connected to an Arduino for driving a stepper motor, 2-wire configuration. The drawing is similar to the unipolar breadboard drawing above; the motor wiring is the same, but the control from the microcontroller is different. The H-bridge is in the same position on the breadboard (rows 10-18) and the power, ground, and motor wiring is the same as the previous drawing. The control is different, specifically: Pins 7 and 10 of the H-bridge are connected to the voltage buses on their respective sides of the breadboard through 10-kilohm resistors. Two NPN transistors are mounted in rows 24 through 26 of the breadboard, one each in the center left and center right section. Their bases (in row 25) are connected to 2 1-kilohm resistors, both of which terminate in row 19 on the center left and right, respectively. The emitters of the transistors are connected to their nearest ground buses on either side. The collectors of the transistors (in row 26) are connected to pins 7 and 10 of the H-bridge, respectively, meeting up with the 10-kilohm resistors. Pin 2 of the H-bridge, in row 11, connects to the other side of the 1-kilohm resistor on the left center side of the breadboard, in row 21. Similarly, pin 15 of the H-bridge, in row 11 on the right side, connects to the other side of the 1-kilohm resistor on the right center side, in row 21. Row 21 on the right side also connects to digital pin 9 on the Arduino, and row 21 on the left side connects to digital pin 8 of the Arduino.
Breadboard view of an L293D h-bridge connected to an Arduino for driving a stepper motor, 2-wire configuration.

Bipolar stepper two-wire circuit:

Schematic drawing of a bipolar stepper motor connected to an H-bridge, 2-wire configuration. The drawing is identical to the unipolar 2-wire schematic above, except there is no center wire for the stepper motor.
Schematic drawing of a bipolar stepper motor connected to an H-bridge and an Arduino, two-wire version.
Breadboard drawing of a bipolar stepper motor connected to an H-bridge, 2-wire configuration. The drawing is identical to the unipolar 2-wire breadboard drawing above, except there is no center wire for the stepper motor.
Breadboard drawing of a bipolar stepper motor connected to an H-bridge and an Arduino, 2-wire configuration.

The stepping sequence is the same as it is for the two middle wires of the sequence above:

Step wire 1 wire 2
1 low high
2 high high
3 high low
4 low low

 

Programming the Microcontroller to Control a Stepper

Because both unipolar and bipolar stepper motors are controlled by the same stepping sequence, you can use similar code for either configuration. In the Arduino stepper library, you only need to change the initial pin configuration. For more on programming stepper control, see the Lab: Controlling a Stepper Motor With an H-Bridge.

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