Stepper Motor Control using ESP32 & A4988 (MicroPython)
Stepper Motor Control using ESP32 & A4988 (MicroPython) is a runnable microcontroller simulation example on SensBench — open the wired circuit, inspect the code, and compile real firmware in your browser, no install needed. Last updated Aug 14, 2026.
FREE SIMULATOR EXAMPLE · INTERMEDIATE
Stepper Motor Control using ESP32 & A4988 (MicroPython)
This example spins a stepper motor on the ESP32 through an A4988 driver in MicroPython — STEP and DIR pulses, the standard driver interface. The stepper’s speed is the pulse rate.
| Board | ESP32 DevKit (Xtensa LX6, 240 MHz) |
|---|---|
| Difficulty | intermediate |
| Category | robotics |
| Libraries | None — just the Arduino core |
| Runs in | Sensbench browser simulator — free, no account |

The exact circuit you get when you open this example — already wired, ready to run.
The sketch
# main.py
import time
import machine
# A4988 pins
dir_pin = machine.Pin(12, machine.Pin.OUT)
step_pin = machine.Pin(14, machine.Pin.OUT)
def move_stepper(direction, steps, delay_us):
print("Direction:", "CW" if direction else "CCW")
dir_pin.value(direction)
steps = abs(steps)
for i in range(steps):
step_pin.value(1)
time.sleep_us(delay_us)
step_pin.value(0)
time.sleep_us(delay_us)
print("Moved", steps, "steps")
def stop_stepper():
step_pin.value(0)
print("Stepper stopped")
print("Stepper motor program started")
while True:
move_stepper(1, 200, 2000) # Clockwise
time.sleep(2)
move_stepper(0, 200, 2000) # Anti-clockwise
time.sleep(2)
How it works
The firmware drives the A4988’s STEP and DIR pins: DIR sets the direction, and each STEP pulse advances the motor one step. The pulse loop’s delay sets the speed, and the example’s pattern shows the basic constant-speed drive.
The step pulse timing is the motor’s contract — the A4988 reads each rising edge, and the minimum pulse width must be respected. The MicroPython code’s delay values map directly to steps per second, making the speed arithmetic visible.
Before you build it for real
The motor supply is separate from the logic — the A4988’s VMOT rail (8-35 V) powers the motor, and the ESP32’s 3.3 V logic only drives STEP/DIR; sharing a rail crashes the driver or the board. Instant speed changes stall steppers — the example’s constant speed is safe, but real control ramps the pulse rate. And the driver’s current-limit potentiometer must be set — out of the box it is often too high for small motors, and the motor overheats while looking fine.
Get the parts
Related examples
FAQ
Why the A4988 instead of direct pins?
The A4988 is the standard stepper driver: it takes STEP and DIR signals and handles the coil sequencing and high-current switching internally. The ESP32 only generates the step pulses — two pins replace the four-coil direct drive.
How does speed work with steppers?
Speed is the step pulse rate: faster pulses, faster rotation. The firmware varies the delay between pulses to accelerate and decelerate — instant speed changes stall a stepper, which is why real control ramps the pulse rate.