How to Control Stepper Motor with Raspberry Pi Easily
To control a stepper motor with a
Raspberry Pi, connect the motor driver to the Pi's GPIO pins and use a Python library like RPi.GPIO to send step signals. Write a script to energize coils in sequence, making the motor rotate step by step.Syntax
Use the RPi.GPIO library to control GPIO pins connected to the stepper motor driver. The main steps are:
- Set GPIO mode and pins as output.
- Define the sequence of signals to energize motor coils.
- Loop through the sequence to rotate the motor step by step.
- Use delays to control speed.
python
import RPi.GPIO as GPIO import time # Define GPIO pins connected to the motor driver coil_pins = [17, 18, 27, 22] # Define step sequence for 8-step control step_sequence = [ [1,0,0,1], [1,0,0,0], [1,1,0,0], [0,1,0,0], [0,1,1,0], [0,0,1,0], [0,0,1,1], [0,0,0,1] ] GPIO.setmode(GPIO.BCM) for pin in coil_pins: GPIO.setup(pin, GPIO.OUT) GPIO.output(pin, 0)
Example
This example rotates a 4-wire stepper motor one full revolution by cycling through the step sequence 512 times. It uses RPi.GPIO to control pins and time.sleep() to set speed.
python
import RPi.GPIO as GPIO import time coil_pins = [17, 18, 27, 22] step_sequence = [ [1,0,0,1], [1,0,0,0], [1,1,0,0], [0,1,0,0], [0,1,1,0], [0,0,1,0], [0,0,1,1], [0,0,0,1] ] GPIO.setmode(GPIO.BCM) for pin in coil_pins: GPIO.setup(pin, GPIO.OUT) GPIO.output(pin, 0) def step_motor(steps, delay=0.01): for _ in range(steps): for step in step_sequence: for pin, val in zip(coil_pins, step): GPIO.output(pin, val) time.sleep(delay) # Turn off coils after movement for pin in coil_pins: GPIO.output(pin, 0) try: step_motor(512) # One full revolution for 28BYJ-48 motor finally: GPIO.cleanup()
Output
Stepper motor rotates one full revolution smoothly.
Common Pitfalls
Common mistakes when controlling stepper motors with Raspberry Pi include:
- Not using a motor driver or transistor board, which can damage the Pi.
- Incorrect GPIO pin numbering mode (use
GPIO.BCMorGPIO.BOARDconsistently). - Skipping
GPIO.cleanup(), which can leave pins in an undefined state. - Using too short or too long delays, causing missed steps or slow movement.
python
import RPi.GPIO as GPIO import time # Wrong: Using BOARD mode but pins defined as BCM coil_pins = [17, 18, 27, 22] GPIO.setmode(GPIO.BOARD) # Mismatch with pin numbers for pin in coil_pins: GPIO.setup(pin, GPIO.OUT) # Correct way: # GPIO.setmode(GPIO.BCM) # coil_pins = [17, 18, 27, 22] # for pin in coil_pins: # GPIO.setup(pin, GPIO.OUT)
Quick Reference
Tips for controlling stepper motors with Raspberry Pi:
- Always use a motor driver board (e.g., ULN2003) between Pi and motor.
- Use
GPIO.BCMmode for pin numbering consistency. - Define the step sequence carefully for your motor type.
- Control speed by adjusting delay between steps.
- Call
GPIO.cleanup()to reset pins after use.
Key Takeaways
Use a motor driver board to safely connect the stepper motor to Raspberry Pi GPIO pins.
Control the motor by sending a sequence of signals to the coils using Python and RPi.GPIO.
Set GPIO mode to BCM and clean up pins after running your program.
Adjust delay between steps to control motor speed and smoothness.
Test connections and code carefully to avoid damaging hardware.