33# BRIEF: An example of accelerating stepper motor.
44# LAST UPDATED: 2025-05-27
55
6- from drv8825 import DRV8825
7- from time import sleep
6+ from drv8825 import DRV8825 # Import the DRV8825 motor driver module
7+ from time import sleep # Import sleep function for timing delays
88
9- DIR_PIN = 4 #GPIO4
10- STEP_PIN = 5 #GPIO5
9+ # Define GPIO pins for motor control
10+ DIR_PIN = 4 # GPIO pin used for direction control
11+ STEP_PIN = 5 # GPIO pin used for step pulses
1112
12- motor = DRV8825 ()
13+ # Create a DRV8825 motor instance
14+ motor = DRV8825 ()
15+
16+ # Initialize the motor with the specified DIR and STEP pins
1317motor .begin (DIR = DIR_PIN , STEP = STEP_PIN )
18+
19+ # Set the number of steps the motor takes per full rotation (usually 200 for a 1.8° stepper motor)
1420motor .setStepsPerRotation (200 )
21+
22+ # Set the length of each step pulse in microseconds
1523motor .setStepPulseLength (1000 )
24+
25+ # Set motor rotation direction to clockwise
1626motor .setDirection (DRV8825 .CLOCK_WISE )
27+
28+ # Enable the motor driver (turns on output)
1729motor .enable ()
1830
1931print ("Accelerating" )
2032
33+ # Gradually increase the step frequency (i.e., speed up the motor)
34+ accelerationFactor = 1
35+ while accelerationFactor < 2000 :
36+ motor .step () # Send a single step pulse to the motor
37+ sleep (1 / accelerationFactor ) # Delay inversely proportional to the factor (higher = faster)
38+ accelerationFactor += 1 # Increase acceleration factor
39+ print (accelerationFactor ) # Print current acceleration factor
2140
22- accelerationFactor = 1
23- while accelerationFactor < 2000 :
24- motor .step ()
25- sleep (1 / accelerationFactor )
26- accelerationFactor += 1
27- print (accelerationFactor )
28-
29- while accelerationFactor > 1 :
30- motor .step ()
31- sleep (1 / accelerationFactor )
32- accelerationFactor -= 1
33- print (accelerationFactor )
41+ # Gradually decrease the step frequency (i.e., slow down the motor)
42+ while accelerationFactor > 1 :
43+ motor .step () # Send a single step pulse
44+ sleep (1 / accelerationFactor ) # Delay increases as acceleration factor decreases
45+ accelerationFactor -= 1 # Decrease acceleration factor
46+ print (accelerationFactor ) # Print current acceleration factor
0 commit comments