Sub topic 12
A first look at stepper motors
This chapter assumes you have read the earlier chapters on powering motors separately with a shared ground, on PWM, and on H-bridges. You should be comfortable with digitalWrite, delayMicroseconds, and wiring a driver board on a breadboard. Steppers are covered as an overview. You will learn what they are, how to wire and drive one with a step/direction board, and where they fail.
What a stepper motor is
A stepper motor turns in fixed angular increments called steps. A common motor has 200 steps per revolution, so each step is 1.8 degrees. You do not command a speed or an angle directly. You send a number of pulses, and the motor moves one step per pulse.
Inside, the motor has several coils around a toothed rotor. Energizing the coils in a repeating sequence pulls the rotor from one tooth position to the next. Reverse the sequence and the motor turns the other way. Hold one coil pattern and the rotor stays put and resists being turned. That holding torque is a major reason to choose a stepper.
Steppers sit between the two motor types you have already used:
| Property | Hobby servo | Small DC motor | Stepper |
|---|---|---|---|
| Position control | Built in, limited range | None without a sensor | By counting steps, no sensor needed |
| Continuous rotation | No (except modified servos) | Yes | Yes |
| Holds position when stopped | Yes, while powered | No | Yes, while coils are energized |
| Speed control | Slow sweeps only | PWM | Pulse rate |
| Needs a driver board | No | MOSFET or H-bridge | Yes |
Counting steps is open-loop control. The Arduino assumes every pulse produced a step. If the motor is overloaded or accelerated too hard, it skips steps and the Arduino never finds out.
Two common motor and driver families
Most hobby projects use one of two combinations.
- Bipolar steppers (such as the NEMA 17) with an A4988 or DRV8825 driver. The motor has four wires in two coils. The driver is a pair of H-bridges with current regulation. You control it with two signals, STEP and DIR. This is the usual choice for 3D printers and small CNC machines.
- The 28BYJ-48 geared unipolar motor with a ULN2003 driver board. It is cheap and runs from 5 V. It is slow and weak but good for learning. The Arduino drives four inputs, one per coil.
The ULN2003 board makes the Arduino do the sequencing. The A4988 does the sequencing itself, so the Arduino only sends pulses. That is why most of this chapter uses the A4988.
Wiring an A4988 driver
The same rules from the earlier chapters apply: motor power comes from an external supply, and the grounds are joined. The connections are:
- Connect VMOT and GND (next to VMOT) to the motor supply, usually 12 V. Put a capacitor of at least 47 µF, ideally 100 µF, across them as close to the board as possible.
- Connect VDD to the Arduino 5 V pin and the other GND to Arduino GND. This GND must be common with the motor supply ground.
- Connect the motor coils to 1A, 1B, 2A, 2B. Each coil pair is one coil. If your motor has four wires, find the pairs by checking resistance with a multimeter: the two wires of one coil read a few ohms, and wires from different coils read open.
- Connect STEP and DIR to Arduino digital pins.
- Tie RESET and SLEEP together so the driver is awake.
- Optionally connect ENABLE to a pin. It is active low: LOW enables the outputs and HIGH turns them off.
Set the current limit first
The motor's rated current is what it can carry without overheating. A bare A4988 does not limit current to your motor's rating unless you set it. You set the limit with a small trimmer potentiometer, which adjusts a reference voltage called Vref that you measure on the potentiometer's metal screw.
For Pololu boards with 0.068 ohm sense resistors, Vref = I_limit × 8 × 0.068, or about 0.544 V per amp. Clone boards often use 0.1 ohm sense resistors, which gives Vref = I_limit × 8 × 0.1, or 0.8 V per amp. Read the resistor marking on your board (R100 means 0.1 ohm, R050 means 0.05 ohm) instead of assuming. Set the limit to roughly 70 percent of the motor's rated current for a first test.
Driving it with STEP and DIR
Each rising edge on STEP moves the motor one step. The level on DIR at that moment sets the direction. The A4988 needs STEP pulses at least 1 µs wide, so the limit on speed is how fast your sketch and the motor can keep up.
const int STEP_PIN = 3;
const int DIR_PIN = 4;
const int EN_PIN = 5; // active low
const int STEPS_PER_REV = 200; // 1.8 degree motor, full steps
const unsigned int HALF_PERIOD_US = 2500; // 5 ms per step = 200 steps/s = 60 rpm
void stepMotor(long steps, bool forward) {
digitalWrite(DIR_PIN, forward ? HIGH : LOW);
delayMicroseconds(5); // let DIR settle before the first pulse
for (long i = 0; i < steps; i++) {
digitalWrite(STEP_PIN, HIGH);
delayMicroseconds(HALF_PERIOD_US);
digitalWrite(STEP_PIN, LOW);
delayMicroseconds(HALF_PERIOD_US);
}
}
void setup() {
pinMode(STEP_PIN, OUTPUT);
pinMode(DIR_PIN, OUTPUT);
pinMode(EN_PIN, OUTPUT);
digitalWrite(EN_PIN, LOW); // enable the driver
}
void loop() {
stepMotor(STEPS_PER_REV, true); // one revolution forward
delay(500);
stepMotor(STEPS_PER_REV, false); // one revolution back
delay(500);
}The speed math is simple. Steps per second is 1,000,000 / (2 × HALF_PERIOD_US). Revolutions per second is that number divided by steps per revolution. Here that is 200 steps per second, or one revolution per second.
This sketch blocks inside stepMotor, just like a delay()-based servo sweep. To move a stepper while doing other work, use the millis() pattern from the servo chapter, or use the AccelStepper library, which tracks target positions and adds acceleration.
Microstepping
The A4988 can divide each full step into smaller moves by blending coil currents. Three pins, MS1, MS2, and MS3, select the mode:
| MS1 | MS2 | MS3 | Resolution | Steps per rev (200-step motor) |
|---|---|---|---|---|
| LOW | LOW | LOW | Full | 200 |
| HIGH | LOW | LOW | Half | 400 |
| LOW | HIGH | LOW | Quarter | 800 |
| HIGH | HIGH | LOW | Eighth | 1600 |
| HIGH | HIGH | HIGH | Sixteenth | 3200 |
Microstepping gives smoother, quieter motion and reduces resonance. It does not give a proportional gain in accuracy. At fine settings, the extra positions between full steps have much less holding force, so a load can easily push the rotor off an intermediate position. Treat microsteps as a smoothing feature more than a precision feature.
Higher resolution also means higher pulse rates for the same speed. At sixteenth stepping, one revolution per second needs 3200 pulses per second. The digitalWrite calls in the loop above take about 4 to 6 µs each on a 16 MHz Uno, so very high rates will need a timer-based library.
Speed, torque, and acceleration
A stepper's torque falls as speed rises. The coils are inductive, so current takes time to build up in each step, and at high step rates it never reaches the set level. If you start a loaded motor at a high pulse rate, it cannot accelerate the load that fast. It stalls or hums without turning.
The fix is to ramp. Start with long delays between pulses and shorten them gradually until you reach the target speed, then reverse the ramp to stop. AccelStepper does this for you with setMaxSpeed and setAcceleration. Higher supply voltage helps, because it lets current rise faster. This is why 3D printers run 12 to 24 V motor supplies on motors rated for only a few volts: the driver's current regulation keeps the average current safe.
Driving a 28BYJ-48 with the Stepper library
For a 5 V geared motor and a ULN2003 board, the standard Arduino Stepper library is enough. This motor needs about 2048 steps for one output-shaft revolution because of its internal gearbox. Pass the coil pins in the order IN1, IN3, IN2, IN4. The motor will only buzz if the order is wrong.
#include <Stepper.h>
const int STEPS_PER_REV = 2048;
Stepper myStepper(STEPS_PER_REV, 8, 10, 9, 11);
void setup() {
myStepper.setSpeed(10); // rpm; this motor is unreliable above about 15
}
void loop() {
myStepper.step(STEPS_PER_REV); // one turn one way
delay(500);
myStepper.step(-STEPS_PER_REV); // one turn back
delay(500);
}Power the ULN2003 board from an external 5 V supply when possible. The motor can draw around 100 to 250 mA, which is more than you should take from an Arduino pin's regulator for long, and the coil current also adds noise to the board's supply.
Common problems
- Motor buzzes and vibrates without turning. The coil pairs are wired wrongly, the pulse rate is too fast to start, or (for a ULN2003) the pin order is wrong. Check coil pairs with a multimeter, lower the speed, and add a ramp.
- Motor turns but skips steps. The load is too heavy, the current limit is too low, or acceleration is too aggressive. Raise Vref carefully, increase supply voltage within the driver's rating, or slow down.
- Driver gets very hot and shuts down. Vref is too high for the board's cooling. Add a heat sink and airflow, or lower the current.
- Arduino resets when the motor starts. This is the brownout problem from the noise chapter. Check the shared ground, the bulk capacitor, and that the motor supply does not feed the Arduino's 5 V line.
- Motor does nothing. Check that RESET and SLEEP are connected, ENABLE is LOW, and VDD has 5 V.
Exercise
A NEMA 17 motor has 200 full steps per revolution. Your A4988 is set to sixteenth microstepping (MS1, MS2, MS3 all HIGH). You want the shaft to turn at 120 rpm. How many STEP pulses per second do you need, and what is the time between rising edges?
Show answer
One revolution is 200 × 16 = 3200 pulses. At 120 rpm the motor turns 2 revolutions per second, so you need 6400 pulses per second. The time between rising edges is 1 / 6400 s, about 156 µs, so each half period (HIGH or LOW) is about 78 µs. A plain digitalWrite loop can roughly manage this on an Uno, but there is little margin for other work, which is why a timer-based library such as AccelStepper is a better choice at this rate.
Exercise
You wire an A4988 board whose sense resistors are marked R100 (0.1 ohm). Your motor is rated for 1.5 A per phase, and you want to run at about 70 percent of that rating. What Vref should you set?
Show answer
The current limit is 1.5 A × 0.7 = 1.05 A. With a 0.1 ohm sense resistor, Vref = I_limit × 8 × 0.1 = 1.05 × 0.8 = 0.84 V. Measure Vref on the potentiometer screw against GND while the logic supply is on.