How to Control a Stepper Motor with a Raspberry Pi

D

Daniel Zhang

Last updated on Aug 17, 2026

  1. Introduction

The Raspberry Pi is a popular single-board computer widely used in hobbyist and industrial projects. It is essentially a small computer about the size of a credit card. It is widely used in various projects, including controlling stepper motors — a common task in motion control applications. In this article, you will learn how to use the Python programming language and the RPi.GPIO library to control a stepper motor, along with the corresponding wiring instructions.

  1. Required Materials
  1. Raspberry Pi (Raspberry Pi 3B+ or newer recommended)
  2. Stepper motor (e.g., NEMA 17)
  3. Stepper motor driver module (e.g., DRV8825 or A4988)
  4. Jumper wires (DuPont connectors)
  5. 12V DC power adapter
  6. Breadboard (optional)
  7. Decoupling capacitor (e.g., 100 µF electrolytic)
  1. Wiring Instructions

Safety Note: Always connect the motor and driver wiring before applying power. Double-check all connections to avoid damaging the Raspberry Pi or the driver module.

  1. Connect the four wires of the stepper motor to the A1, A2, B1, and B2 pins on the motor driver module.
  2. Connect the positive terminal of the 12V power supply to the VMOT pin on the driver module, and the negative terminal to the GND pin. Place a decoupling capacitor (e.g., 100 µF electrolytic) across VMOT and GND, as close to the driver module as possible, to suppress voltage spikes.
  3. Connect the 5V power pin (physical Pin 2 or Pin 4) of the Raspberry Pi to the VCC pin on the driver module, and connect a GND pin (e.g., physical Pin 6) to the GND pin on the driver module.
  4. Connect the following BCM GPIO pins on the Raspberry Pi to the corresponding pins on the driver module:
Raspberry Pi GPIO (BCM) Physical Pin Driver Module Pin
GPIO 17 Pin 11 DIR
GPIO 18 Pin 12 STEP
GPIO 27 Pin 13 MS1
GPIO 22 Pin 15 MS2
GPIO 23 Pin 16 ENABLE

Tip: The ENABLE pin on most A4988/DRV8825 modules is active-low. Pulling it LOW enables the driver output.

  1. Python Code Implementation

First, install the RPi.GPIO library. Enter the following commands in the terminal:

sudo apt-get update
sudo apt-get install python3-rpi.gpio

Next, create a Python file named stepper_motor.py and enter the following code:

import time
import RPi.GPIO as GPIO

# Define pins (BCM numbering)
DIR = 17
STEP = 18
MS1 = 27
MS2 = 22
ENABLE = 23

# Set GPIO mode
GPIO.setmode(GPIO.BCM)
GPIO.setup(DIR, GPIO.OUT)
GPIO.setup(STEP, GPIO.OUT)
GPIO.setup(MS1, GPIO.OUT)
GPIO.setup(MS2, GPIO.OUT)
GPIO.setup(ENABLE, GPIO.OUT)

# Enable driver and set direction
GPIO.output(ENABLE, GPIO.LOW)   # Enable driver (active-low)
GPIO.output(DIR, GPIO.HIGH)     # Clockwise direction

# Set stepping mode based on driver model (e.g., A4988)
GPIO.output(MS1, GPIO.HIGH)     # 1/4 step
GPIO.output(MS2, GPIO.LOW)

# Control motor rotation
steps = 200
delay = 0.001  # Shorter delay for faster speed

for _ in range(steps):
    GPIO.output(STEP, GPIO.HIGH)
    time.sleep(delay)
    GPIO.output(STEP, GPIO.LOW)
    time.sleep(delay)

# Clean up GPIO
GPIO.cleanup()

Finally, save the file and run it in the terminal:

python3 stepper_motor.py
  1. Summary

This article introduced how to use a Raspberry Pi to control a stepper motor, including wiring instructions and Python code using the RPi.GPIO library. You can easily achieve motor control by following these steps. Adjust the parameters in the code (such as step count, delay, and microstepping settings) according to your project requirements.