I am currently trying to control a NEMA 17 stepper motor rated for 1.5A through a DRV8825 driver using a Raspberry Pi 4B for a school project. I was trying to use RPi.GPIO to control the motor but it does not work. I started to search and read that as of kernel 6.6 onwards, RPi.GPIO does not work anymore. I am new to the Raspberry Pi platform and have come across libraries such as libgpio and gpiozero, and despite reading the documentation, I am still lost.
How can I control the GPIO pins specifically to drive a stepper motor?
OS:
- Bookworm OS with kernel 6.12.34-rpt-rpi-v8
Hardware:
- NEMA 17 0.28N 1.5A
- 12V 2.5A Power supply
- DRV8825 stepper motor driver (Vref is set to around 0.74v)
Wiring diagram:
Code:
from time import sleep
import RPi.GPIO as GPIO
DIR = 20
STEP = 21
CW = 1 # Clockwise Rotation
CCW = 0 # Counterclockwise Rotation
SPR = 200 # Steps per revolution which is 1.8 per step for NEMA 17
GPIO.setmode(GPIO.BCM)
GPIO.setup(DIR, GPIO.OUT)
GPIO.setup(STEP, GPIO.OUT)
GPIO.output(DIR, CW)
step_count = SPR
delay = 0.005
for i in range(step_count):
GPIO.output(STEP, GPIO.HIGH)
sleep(delay)
GPIO.output(STEP, GPIO.LOW)
sleep(delay)
sleep(0.5)
GPIO.output(DIR, CCW)
for i in range(step_count):
GPIO.output(STEP, GPIO.HIGH)
sleep(delay)
GPIO.output(STEP, GPIO.LOW)
sleep(delay)
GPIO.cleanup()
