Controlling Stepper Motors

Originally written on July 16, 2014 by Tom Igoe
Last modified on August 28, 2015 by Tom Igoe

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.

  unipolar_stepper_wiring

Bipolar stepper motors

The bipolar stepper motor usually has four wires coming out of it. Unlike unipolar steppers, bipolar steppers have no common center 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.

bipolar_stepper_wiring

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:

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

 

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:

UnipolarStepperLab_schem UnipolarStepperLab_bb
Schematic unipolar stepper wired to an H-bridge

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.  Here’s the circuit for that:

BipolarStepperLab_schem BipolarStepperLab_bb
schematic bipolar stepper motor wired to an H-bridge

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:

Unipolar stepper two-wire circuit:

UnipolarStepperLabTwoWire_schem UnipolarStepperLabTwoWire_bb
Schematic Unipolar wired to an H-bridge and controlled with two wires

Biolar stepper two-wire circuit:

BipolarStepperLabTwoWire_schem BipolarStepperLabTwoWire_bb
Schematic Bipolar wired to an H-bridge and controlled with two wires

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.