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.

A 28BYJ-48 geared stepper motor connected to a ULN2003 driver board
A 28BYJ-48 geared stepper motor with its ULN2003 driver board.
Photo: Kushagra Keshari, CC BY-SA 4.0

Coils, rotor & driver signals

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

ComboSupplyESP32 pinsGood forNotes
28BYJ-48 + ULN2003 board5 V4 (IN1–IN4)First experiments, light loads, slow pointersLow cost, with a gearbox (~1/64). Slow (typically ≤ ~15 RPM) and weak. Unipolar.
NEMA 17 + A49888–35 V2 (STEP, DIR) + optional EN, MS1–3Printers, sliders, plottersUp to 1/16 microstep. Set the current limit with the Vref pot. Above ~1 A, it needs a heatsink.
NEMA 17 + DRV88258.2–45 V2 + optionalSame as A4988, with a little more currentUp to 1/32 microstep. It uses a different Vref formula.
NEMA 17 + TMC2208/2209~5–28 V (check board)2 + optional UARTQuiet machinesAlmost 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.

ESP32 GPIO 25 GPIO 26 GPIO 27 3V3 GND A4988 + heatsink ENABLE MS1 MS2 MS3 RESET SLEEP STEP DIR VMOT GND 2B 2A 1A 1B VDD GND MS1–3 open = full step jumper 3.3 V logic 12–24 V supply ≥ 2 A 100 µF common GND NEMA 17 coil 2 = green pair coil 1 = purple pair
Pololu-style A4988 carrier. Connect RESET to SLEEP (or pull it HIGH). If you do not, the driver stays disabled. ENABLE is active-LOW: LOW = motor powered.

Connections

A4988 pinConnect toWhy
STEPGPIO 26Each rising edge = one (micro)step
DIRGPIO 27HIGH/LOW selects direction
ENABLEGPIO 25 (or GND)LOW = on, HIGH = coils off (cool, but no holding torque)
RESET ↔ SLEEPeach otherThe two pins must be HIGH for the driver to operate
MS1–MS3open / 3V3 / GPIOMicrostep selection (table below)
VDD, GNDESP32 3V3, GNDLogic supply (3–5.5 V)
VMOT, GND12–24 V supply + 100 µFMotor power (8–35 V)
1A 1B / 2A 2Bmotor coil 1 / coil 2Each 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)

  1. 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.
  2. Connect the logic wires first (VDD, GND, STEP, DIR, RESET↔SLEEP, ENABLE). Keep the motor supply disconnected.
  3. 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.
  4. Connect the motor coils (1A/1B = one coil, 2A/2B = the other coil).
  5. 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.
  6. 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.
Do not connect or disconnect the motor while VMOT is energized. The inductance of the coil makes a voltage spike that destroys the driver.
Heat: Above ~1 A for each coil, A4988 chips become hot. Use the adhesive heatsink and some airflow. If you do not need holding torque, set ENABLE HIGH.
Power: Select a supply for the total current of all the motors. A NEMA 17 usually takes less current from the supply than its rated coil current, because the driver chops the current. A 12 V 2 A supply is sufficient for one or two motors. Refer to Power supplies.

Microstepping & calculators

A4988 microstep pins

MS1MS2MS3Resolution
LLLFull step
HLL1/2
LHL1/4
HHL1/8
HHH1/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

ESP32 GPIO 16 GPIO 17 GPIO 18 GPIO 19 GND ULN2003 driver board IN1IN2 IN3IN4 + 5–12V− 5 V supply 28BYJ-48 5 V unipolar 5-pin plug
The ULN2003 is a Darlington transistor array (7 low-side switches with built-in flyback diodes). Each IN pin connects one half-coil to ground. The plug connects the red common wire of the motor to +5 V.
  • 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 Stepper library needs IN1, 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