Stepper motors: movement in exact steps
A stepper motor has two coils (A and B) and no position sensor. A driver switches the coils on and off in a fixed pattern. Each change in the pattern pulls the rotor exactly one step. On a typical motor, one step is 1.8° (200 steps per revolution). If you count the steps that you send, you know the position of the shaft. This is open-loop position control. 3D printers, CNC machines, camera sliders and plotters use it.

Photo: Kushagra Keshari, CC BY-SA 4.0
This is a simplified drawing. Here, one turn of the rotor is equal to 4 full steps. A real 1.8° motor has a rotor with 50 teeth, so it needs 50 of these electrical cycles (200 full steps) for each revolution. The pattern of coil currents is the same. Cyan shows current in one direction through a coil. Pink shows current in the opposite direction.
How a stepper works
Two coils, many teeth
Most hobby steppers are bipolar. They have two coils and 4 wires. The driver pushes current through each coil in two directions. This needs one H-bridge for each coil. Unipolar motors, for example the 28BYJ-48, have a center tap on each coil (5 or 6 wires). In each half-coil, the current flows in only one direction, so a simple transistor array can drive these motors.
Step modes
- Wave: one coil at a time. This mode gives the least torque and is the most simple.
- Full step: the two coils are always on. This mode gives full torque and 200 steps/rev.
- Half step: one coil, then two coils, in sequence. This mode gives 400 positions/rev.
- Microstepping: the driver sets the current in each coil to sine/cosine levels. This gives smooth, quiet motion and steps of 1/2 to 1/256 of a full step. The coil current is quantized into levels, as on the ADC page.
Torque, speed & missed steps
Holding torque is the force with which the energized motor resists rotation. Torque decreases when speed increases. The reason is that the coil inductance does not let the current increase quickly. If the load needs more torque than the motor can supply, the motor skips steps. Nothing tells you about this, so your position count becomes incorrect. To prevent this, use acceleration ramps. Also use a higher supply voltage with a current-limiting driver. Use a limit switch to find the “home” position at start-up.
Select a motor and driver
| Combo | Supply | ESP32 pins | Good for | Notes |
|---|---|---|---|---|
| 28BYJ-48 + ULN2003 board | 5 V | 4 (IN1–IN4) | First experiments, light loads, slow pointers | Low cost, with a gearbox (~1/64). Slow (typically ≤ ~15 RPM) and weak. Unipolar. |
| NEMA 17 + A4988 | 8–35 V | 2 (STEP, DIR) + optional EN, MS1–3 | Printers, sliders, plotters | Up to 1/16 microstep. Set the current limit with the Vref pot. Above ~1 A, it needs a heatsink. |
| NEMA 17 + DRV8825 | 8.2–45 V | 2 + optional | Same as A4988, with a little more current | Up to 1/32 microstep. It uses a different Vref formula. |
| NEMA 17 + TMC2208/2209 | ~5–28 V (check board) | 2 + optional UART | Quiet machines | Almost silent “StealthChop”. Set the current with Vref or UART (refer to the vendor documentation). |
Connect a NEMA 17 to an A4988
The ESP32 sends only logic signals. A pulse on STEP moves the motor one (micro)step. The level on DIR selects the direction. The motor power (VMOT) comes from a separate 12–24 V supply.
Connections
| A4988 pin | Connect to | Why |
|---|---|---|
| STEP | GPIO 26 | Each rising edge = one (micro)step |
| DIR | GPIO 27 | HIGH/LOW selects direction |
| ENABLE | GPIO 25 (or GND) | LOW = on, HIGH = coils off (cool, but no holding torque) |
| RESET ↔ SLEEP | each other | The two pins must be HIGH for the driver to operate |
| MS1–MS3 | open / 3V3 / GPIO | Microstep selection (table below) |
| VDD, GND | ESP32 3V3, GND | Logic supply (3–5.5 V) |
| VMOT, GND | 12–24 V supply + 100 µF | Motor power (8–35 V) |
| 1A 1B / 2A 2B | motor coil 1 / coil 2 | Each pair must be one coil |
Find the coil pairs
Wire colors are different for each manufacturer. Set a multimeter to resistance (Ω). If two wires have a few ohms between them, they are one coil. If two wires have an open circuit between them, they are from different coils. You can also do a quick test. Twist two wires together and turn the shaft. If the shaft becomes much harder to turn, those two wires are a coil pair.
Build procedure (A4988)
- Install the driver. Examine the orientation. The Vref pot position and the pin labels must agree with the diagram. If you install the driver backwards, it is immediately damaged.
- Connect the logic wires first (VDD, GND, STEP, DIR, RESET↔SLEEP, ENABLE). Keep the motor supply disconnected.
- Install the 100 µF electrolytic capacitor across VMOT and GND, near the driver. Make sure that the polarity is correct. The capacitor absorbs voltage spikes that can damage the driver.
- Connect the motor coils (1A/1B = one coil, 2A/2B = the other coil).
- Set the current limit before you operate the motor (calculator below). Energize the driver and keep the motor stationary. Measure Vref between the wiper of the pot and GND. Turn the pot with a ceramic or insulated screwdriver.
- Upload the test sketch and energize VMOT. If the motor only vibrates, one coil pair is incorrect or the steps are too fast. Decrease the speed and examine the wiring again.
Microstepping & calculators
A4988 microstep pins
| MS1 | MS2 | MS3 | Resolution |
|---|---|---|---|
| L | L | L | Full step |
| H | L | L | 1/2 |
| L | H | L | 1/4 |
| H | H | L | 1/8 |
| H | H | H | 1/16 |
The MS pins have internal pull-down resistors, so open = LOW. The DRV8825 uses M0–M2 with a different table (up to 1/32). Refer to its datasheet.
Step rate calculator
- Steps per rev
- 3200
- Step frequency
- 3200 Hz
- Angle / step
- 0.1125°
- Travel / step
- 2.5 µm
- Time between STEP pulses
- 312.5 µs
A T8 lead screw (common on 3D printers) moves 8 mm for each revolution. Small microsteps mainly make the motion smoother. With a load, the rotor stops only at about the position of each microstep. The 28BYJ-48 on a ULN2003 cannot microstep. With this motor, use 1 (or 2 for half step).
Current-limit (Vref) calculator
- Set Vref to
- 0.54 V
A4988: Imax = Vref / (8 × Rsense) → Vref = I × 8 × Rsense.
Read the very small resistors adjacent to the chip (for example, “R068” = 0.068 Ω, “R100” = 0.1 Ω). Start at or below the rated current of the motor. On the A4988, the coil current in full-step mode is limited to about 70% of this setting.
Beginner option: 28BYJ-48 + ULN2003
- Supply the + / − pins of the board from a 5 V supply. The 5V/VIN pin of the DevKit is sufficient for one motor on a good USB port. Do not use 3V3.
- The internal gearbox is about 1:64 (more exactly ~63.7:1). This gives ~2048 full steps or ~4096 half steps for each output revolution.
- This motor is slow. Start at about 10–15 RPM. At higher speeds, the motor usually only makes a buzz.
- The four LEDs on the board show which coils are on, so this board is the clearest way to see a step sequence.
- The pin sequence in the code is important. The Arduino
Stepperlibrary needsIN1, IN3, IN2, IN4.
Code
AccelStepper with an A4988 (acceleration ramps)
Library Manager → AccelStepper by Mike McCauley. If you increase the speed gradually, the motor does not miss steps.
accelstepper_a4988.ino
#include <AccelStepper.h>
const int STEP_PIN = 26;
const int DIR_PIN = 27;
const int EN_PIN = 25;
const int STEPS_PER_REV = 200 * 16; // 1.8° motor, MS1–MS3 HIGH = 1/16 microstep
AccelStepper stepper(AccelStepper::DRIVER, STEP_PIN, DIR_PIN);
void setup() {
pinMode(EN_PIN, OUTPUT);
digitalWrite(EN_PIN, LOW); // LOW = driver enabled
stepper.setMaxSpeed(4000); // steps per second
stepper.setAcceleration(2000); // steps per second²
stepper.moveTo(STEPS_PER_REV); // one full turn
}
void loop() {
if (stepper.distanceToGo() == 0) {
delay(500);
stepper.moveTo(-stepper.currentPosition()); // move back in the opposite direction
}
stepper.run(); // call as frequently as possible; do not use delay() while the motor moves
}
Raw STEP/DIR (what the library does)
step_dir_basic.ino
const int STEP_PIN = 26;
const int DIR_PIN = 27;
void setup() {
pinMode(STEP_PIN, OUTPUT);
pinMode(DIR_PIN, OUTPUT);
}
void turn(int steps, bool clockwise, int usBetweenSteps) {
digitalWrite(DIR_PIN, clockwise ? HIGH : LOW);
delayMicroseconds(5); // DIR setup time
for (int i = 0; i < steps; i++) {
digitalWrite(STEP_PIN, HIGH);
delayMicroseconds(3); // A4988 needs ≥ 1 µs high
digitalWrite(STEP_PIN, LOW);
delayMicroseconds(usBetweenSteps);
}
}
void loop() {
turn(200, true, 2000); // 1 rev at full step, 500 steps/s
delay(500);
turn(200, false, 2000);
delay(500);
}
28BYJ-48 with Arduino's Stepper library
byj48.ino
#include <Stepper.h>
const int STEPS_PER_REV = 2048; // full steps per output revolution
// Note the order: IN1, IN3, IN2, IN4
Stepper motor(STEPS_PER_REV, 16, 18, 17, 19);
void setup() {
motor.setSpeed(10); // RPM (keep it slow)
}
void loop() {
motor.step(STEPS_PER_REV); // one turn (blocking)
delay(500);
motor.step(-STEPS_PER_REV);
delay(500);
}
Troubleshooting
The motor vibrates but does not turn
Possible causes: a wire pair is connected across two different coils (find the pairs again). The step rate is too high for a start from rest (add acceleration). The current limit is too low.
The motor loses position or skips steps
The load or the speed is too high for the available torque. Decrease the maximum speed and add acceleration. Increase the supply voltage (in the limits of the driver rating). If the motor stays cool enough to touch, set the current a little higher.
The driver or motor is too hot
Steppers use full current also when they are stationary, so some heat is normal. Decrease Vref and add a heatsink. If the load cannot fall or slip, disable the driver (EN HIGH) when the motor is idle.
Nothing occurs
Make sure that RESET and SLEEP are connected to HIGH and that ENABLE is LOW. Make sure that VMOT has power and that the grounds are common. To see the pulses, examine the STEP pin with an LED or an oscilloscope.
The motor turns in the incorrect direction
Invert DIR in the code, or interchange the two wires of one coil (for example, 1A ↔ 1B). Do not interchange wires between coils.
Loud whine when the motor is stationary
The current chopping of the driver causes this noise. TMC2208/2209 drivers are much quieter. Microstepping also makes the motion smoother and quieter.
Resources
- Pololu A4988 carrierPinout, current-limit procedure and the minimal wiring diagram.
- Pololu DRV8825 carrierHigher-voltage driver with 1/32 microstepping.
- AccelStepper documentationAcceleration, many motors, non-blocking movement.
- Adafruit: All about stepper motorsMotor types, wiring and how to identify unknown motors.
- Power suppliesHow to select a 12–24 V supply and connect its ground to the ESP32 ground.
- DC motors · ServosCompare with the other methods to make things move.