Thanks to visit codestin.com
Credit goes to github.com

Skip to content

juanmitaboada/microstepper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Stepper Motor Control with ESP32 and MicroPython

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.

📦 Features

  • 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.

🔧 Hardware Requirements

  • 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

📋 Wiring

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.

⚙️ Installation

  1. Flash a recent MicroPython firmware (v1.13+) onto the ESP32.
  2. Copy stepper.py to the ESP32 filesystem (e.g., via Thonny or mpremote).
  3. Edit the pin definitions in your main script if needed.

📝 Usage Example

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)

💡 API Reference

Stepper(turn_rps)

  • turn(rps: float) — Rotate continuously at rps (revolutions per second). Positive is clockwise, negative counterclockwise. 0 stops the motor with a smooth ramp.

Positioning

  • 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.

Configuration

  • 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.

📈 Debugging

Enable debug logging by observing console output; the library prints [DEBUG] statements during ramping, RMT start/stop, and position updates.

📝 License

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!

About

MicroPython Stepper Example

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published