Rotate the motor and sleep
What this example shows
One full rotation, one full rotation back, then the driver goes to sleep. Sleeping cuts the driver's current draw when the motor does not need to hold position, which is the reason ENABLE, RESET and SLEEP are wired to GPIO rather than tied high.
The example uses DRV8825.CLOCK_WISE for the first leg and
DRV8825.COUNTER_CLOCK_WISE for the second. Those names describe the DIR level
the module sets, not a guaranteed physical direction: which way the shaft turns
depends on how you wired the coils. Swap one coil's pair to reverse it.
The example's own header names this product: "WORKS WITH: Stepper motor driver DRV8825 board: www.solde.red/333000".
The example code
Pin numbers are the NULA DeepSleep ESP32-S3 assignment from Quick Start. The recorded example uses GPIO 5, 18, 19, 21 and 22; GPIO18 is not broken out on that board and the ESP32-S3 has no GPIO22. Nothing else is changed.
from drv8825 import DRV8825
from time import sleep
# Define pin numbers (adjust to match your board setup)
DIR_PIN = 4 # GPIO4
STEP_PIN = 5 # GPIO5
EN_PIN = 6 # GPIO6 (optional)
RST_PIN = 7 # GPIO7 (optional)
SLP_PIN = 10 # GPIO10 (optional)
# The motor should be connected via the B1,B2,A1,A2 pins
# The motor power supply should be connected to the VIN and GND pins and have a
# parallel connected capacitor, reffer to your motors datasheet for more information
# Initialize the driver
motor = DRV8825()
motor.begin(DIR=DIR_PIN, STEP=STEP_PIN, EN=EN_PIN, RST=RST_PIN, SLP=SLP_PIN)
# Configure motor behavior
motor.setStepsPerRotation(200) # Typical for 1.8° stepper motors
motor.setStepPulseLength(1000) # Microseconds per step pulse
motor.setDirection(DRV8825.CLOCK_WISE)
motor.enable()
# Step the motor forward 200 steps (one full rotation)
print("Stepping forward...")
for _ in range(200):
motor.step()
sleep(0.005) # 5ms delay between steps
# Change direction and step back
motor.setDirection(DRV8825.COUNTER_CLOCK_WISE)
print("Stepping backward...")
for _ in range(200):
motor.step()
sleep(0.005)
# Put the motor to sleep
motor.sleep()
print("Motor is now sleeping.")
Expected result:
Stepping forward...
Stepping backward...
Motor is now sleeping.
Those three lines are the example's own literal print() calls, so the text is
exact.
The motor turns 200 steps, reverses, and ends where it started. On a 1.8° motor
in full step, 200 steps is one full rotation. Change the microstep setting with
M0, M1 or M2 and the same 200 pulses cover proportionally less: at 1/8 microstep
they are one eighth of a turn. setStepsPerRotation() does not know about the
microstep pins, so it is on you to keep the two in agreement.
After the final line the driver is asleep, holding nothing, and the shaft turns freely by hand.
Functions used
DRV8825()returns DRV8825Creates the driver object. It configures nothing on its own; every pin is set up by begin().
ReturnsA new driver instance with no pins assigned.
Parameters
This constructor takes no parameters.
begin(DIR, STEP, EN=None, RST=None, SLP=None)returns boolAssigns the control pins and configures them as outputs. DIR and STEP are required. EN, RST and SLP are optional; pass them only if those pins go to GPIO rather than being tied to a fixed level. RST and SLP are initialised high, which is the level the driver needs to run.
ReturnsTrue once the pins are configured.
Parameters
| Type | Name | Description |
|---|---|---|
int | DIR | GPIO number for the direction pin. |
int | STEP | GPIO number for the step pin. |
int | EN | GPIO number for the enable pin. Optional, defaults to None. |
int | RST | GPIO number for the reset pin. Optional, defaults to None. Driven high at startup. |
int | SLP | GPIO number for the sleep pin. Optional, defaults to None. Driven high at startup. |
setStepsPerRotation(steps)returns NoneTells the module how many steps make one full rotation, so it can track position within a rotation. A 1.8 degree motor at full step takes 200. It does not change how the motor is driven.
ReturnsNothing.
Parameters
| Type | Name | Description |
|---|---|---|
int | steps | Steps per full rotation. |
setStepPulseLength(length)returns NoneSets how long the STEP pin is held high, and then low, inside each step() call, in microseconds. Set to 0 and step() toggles the pin with no added delay.
ReturnsNothing.
Parameters
| Type | Name | Description |
|---|---|---|
int | length | Pulse length in microseconds. |
setDirection(direction)returns boolSets the direction pin. Use the class constants DRV8825.CLOCK_WISE and DRV8825.COUNTER_CLOCK_WISE rather than raw numbers. Any other value is rejected and the pin is left alone. The constant names describe the DIR level, not a guaranteed shaft direction, which follows your coil wiring.
ReturnsTrue if the direction was valid, False otherwise.
Parameters
| Type | Name | Description |
|---|---|---|
int | direction | DRV8825.CLOCK_WISE (1) or DRV8825.COUNTER_CLOCK_WISE (0). |
enable()returns boolEnables the driver outputs by pulling the ENABLE pin low. Returns False if no enable pin was passed to begin().
ReturnsTrue if the pin was driven, False if no enable pin is configured.
Parameters
This function takes no parameters.
step()returns NoneAdvances the motor one step: pulses STEP high then low, waiting the configured pulse length at each level. It also increments the step counter and moves the tracked position one place in the current direction.
ReturnsNothing.
Parameters
This function takes no parameters.
sleep()returns boolPuts the driver into low-power sleep by pulling the SLEEP pin low. Call wakeup() before stepping again. Returns False if no sleep pin was passed to begin().
ReturnsTrue if the pin was driven, False if no sleep pin is configured.
Parameters
This function takes no parameters.
Putting it together
The shape is always the same: begin() with your pins, describe the motor with
setStepsPerRotation() and setStepPulseLength(), enable(), then step.
Timing is yours to control. setStepPulseLength(1000) holds each STEP level for
1 ms, and the sleep(0.005) in the loop adds 5 ms between steps, so the motor
runs at roughly 140 steps per second. Shorten either to go faster.
The driver stays powered while idle unless you tell it otherwise, and the saving
from sleep() is not small. The datasheet puts the DRV8825's own supply current
at typically 5 mA running against typically 10 µA asleep, both measured at 24 V.
wakeup() brings it back and isSleeping() reports the current state. On a
battery-powered controller it is worth sleeping between every move.
Two things sleep does not switch off. The motor stops being held, so anything with a load on it will back-drive: sleep at the end of a move only if losing position is acceptable, or if the mechanism holds itself. And the board's own PWR LED keeps drawing whatever the driver is doing, unless you cut JP1.