A MicroPython-based project to control a NEMA 17 stepper motor using an ESP32 and an A4988 (or compatible) driver. Leverages the ESP32's RMT peripheral for precise pulse generation, enabling smooth acceleration and deceleration, microstepping, and dynamic position/speed control via a simple API.
- Precise Timing: Utilizes the RMT peripheral for microsecond-accurate pulse generation.
- Microstepping: Supports 1, 2, 4, 8, and 16 microsteps per full step.
- Smooth Ramping: Software-controlled acceleration and deceleration (ramps) for start/stop and speed changes.
- Dynamic Speed Control: Change rotational speed (RPS) on the fly using the
turn(rps)method. - Position Control: Move to absolute (
pos_abs(angle)) or relative (pos_rel(delta)) angles in degrees. - Non-blocking API: Methods return control once the commanded move is complete.
- ESP32 development board
- A4988 (or DRV8825) stepper driver module
- NEMA 17 bipolar stepper motor
- External power supply (e.g., 12V–24V) for the motor
- Jumper wires and breadboard or custom PCB
| Driver Pin | ESP32 Pin | Function |
|---|---|---|
| EN | GPIO 5 | Enable (LOW = ON) |
| DIR | GPIO 16 | Direction |
| STEP | GPIO 17 | Step pulses |
| MS1 | GPIO 27 | Microstep bit 1 |
| MS2 | GPIO 26 | Microstep bit 2 |
| MS3 | GPIO 25 | Microstep bit 3 |
| VDD | 3.3V | Logic voltage |
| GND | GND | Ground |
| VMOT | 12V–24V | Motor power |
| GND | GND | Motor driver ground |
Note: Ensure MS1–MS3 are set to the correct levels for your desired microstepping mode.
- Flash a recent MicroPython firmware (v1.13+) onto the ESP32.
- Copy
stepper.pyto the ESP32 filesystem (e.g., via Thonny ormpremote). - Edit the pin definitions in your main script if needed.
from stepper import Stepper
from time import sleep_ms
# Initialize motor driver
motor = Stepper(
en_pin=5, dir_pin=16, step_pin=17,
ms1_pin=27, ms2_pin=26, ms3_pin=25,
microstep=8 # 1/8-step mode
)
# Continuous rotation at 2 RPS
motor.turn(2.0)
sleep_ms(5000)
# Move to absolute 45°
motor.pos_abs(45)
sleep_ms(1000)
# Move relative -90°
motor.pos_rel(-90)
sleep_ms(1000)
# Stop motor
motor.turn(0)turn(rps: float)— Rotate continuously atrps(revolutions per second). Positive is clockwise, negative counterclockwise.0stops the motor with a smooth ramp.
pos_abs(angle: float, speed_rps: float = 1.0)— Move to an absolute angle in degrees with smooth ramping.pos_rel(delta: float, speed_rps: float = 1.0)— Move by a relative angle (degrees) from the current position.
microstep— Microstepping value:1, 2, 4, 8, or 16.ramp_step— Incremental step (RPS) for each ramp iteration.ramp_delay_ms— Delay between ramp steps in milliseconds.
Enable debug logging by observing console output; the library prints [DEBUG] statements during ramping, RMT start/stop, and position updates.
This project is licensed under the Apache License, Version 2.0. You may not use this file except in compliance with the License. A copy of the License is provided in the LICENSE file.
Enjoy smooth, precise stepper control with the power of ESP32 and MicroPython!