Raspberry Pi - Stepper Motor

This tutorial shows how a Raspberry Pi can control a stepper motor through a DRV8825 driver. In detail, we will learn:

The tutorial is applicable to all types of bipolar stepper motors with four wires. It will use a NEMA 17 stepper motor as an example.

Raspberry Pi stepper motor

If you want control another type of stepper motor, please check out this Raspberry Pi - 28BYJ-48 Stepper Motor tutorial.

Hardware Preparation

1×Raspberry Pi 5
1×Stepper Motor Nema 17
1×DRV8825 Stepper Motor Driver
1×Expansion Board for DRV8825
1×12V Power Adapter
1×DC Power Jack
1×Jumper Wires
1×Recommended: Screw Terminal Block Shield for Raspberry Pi
1×Recommended: Raspberry Pi Prototyping Base Plate & Breadboard Kit

Or you can buy the following kits:

1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables .

Overview of Stepper Motor

Two common types of stepper motors exist:

  • bipolar: this motor has four wires
  • unipolar: this motor has either five or six wires

For a 6-wire unipolar stepper motor, we can utilize four of its six wires and control it as if it were a bipolar stepper motor.

For a 5-wire unipolar stepper motor, refer to Raspberry Pi - control 28BYJ-48 stepper motor using ULN2003 driver.

This tutorial concentrates solely on the bipolar stepper motor.

Bipolar Stepper Motor pinout

The bipolar Stepper Motor has four pins and the names of these pins vary depending on the manufacturer. The following table displays some of the more common names for the pins:

PIN NO Naming 1 Naming 2 Naming 3
1 A+ A A
2 A- A C
3 B+ B B
4 B- B D
Bipolar Stepper Motor pinout

The arrangement of pins, the labelling of wires, and their colours may differ between manufacturers. To determine the connection between the wire colour and its name, you must consult the datasheet or manual. The image above also displays the details of two different motors with different wire labelling and colours.

Steps per Revolution

The specification of the motor states the DEG_PER_STEP. Depending on the control technique, STEP_PER_REVOLUTION can be determined using the following table:

Control method Steps per Revolution Real degree per step
Full-step STEP_PER_REVOLUTION = 360 / DEG_PER_STEP DEG_PER_STEP
Half-step STEP_PER_REVOLUTION = (360 / DEG_PER_STEP) * 2 DEG_PER_STEP / 2
Micro-step (1/n) STEP_PER_REVOLUTION = (360 / DEG_PER_STEP) * n DEG_PER_STEP / n

For instance, if the motor's data sheet states a 1.8 degree/step:

Control method Steps per Revolution Real degree per step
Full-step 200 steps/revolution 1.8°
Half-step 400 steps/revolution 0.9°
Micro-step (1/n) (200 * n) steps/revolution (1.8 / n)°

How to control a stepper motor using Raspberry Pi

Raspberry Pi can generate signals to control the stepper motor, however, these signals are not of sufficient voltage and/or current to meet the stepper motor's requirements. Consequently, a hardware driver is necessary between Raspberry Pi and the stepper motor. This driver serves two purposes:

  • To amplify the control signals from Raspberry Pi in terms of current and voltage
  • To protect Raspberry Pi from the high current and voltage used to power the stepper motor.

There are numerous hardware drivers that can be utilized to manage stepper motors. The one we pick here is the DRV8825 driver, plugged into its expansion board.

Overview of DRV8825 Driver

Each small DRV8825 board drives a single bipolar stepper motor (a NEMA 17, for example). The Raspberry Pi tells it only two things: "move one step" and "which way". The driver does all the coil switching by itself. It is a much better fit for a stepper than an older dual H-bridge board, because it keeps the coil current at a safe level, and the motor and the driver stay cooler.

DRV8825 Driver Pinout

DRV8825 Driver pinout

The pins that matter in this tutorial:

  • STEP pin: each pulse (LOW to HIGH) on this pin moves the motor by one step.
  • DIR pin: sets the direction. HIGH turns one way, LOW turns the other way.
  • EN pin: LOW or free = driver on. We leave it free.
  • M0, M1, M2 pins: choose the step size, from full step to 1/32 step.
  • 1A, 1B, 2A, 2B pins: the two coils of the stepper motor.
  • VMOT and GND pins: the motor power, from 8.2V to 45V.
  • Current-limit knob: a tiny screw on the module. It sets the coil current, about 1.5A per coil at most (2.5A peak).

Other good facts:

  • The logic inputs work with 3.3V signals, so the GPIO pins of the Raspberry Pi drive them directly.
  • The chip protects itself against over-current, over-heating and a too-low supply voltage.
  • Over about 1.5A, the small driver board gets hot to the touch. Keep the knob near its low end at first, and turn it up a little each time until the motor runs evenly.

The DRV8825 Expansion Board

The bare module has small pins that are hard to wire. So we plug it into the two sockets of its expansion board. The board gives:

  • VIN / GND screw terminal: the 12V supply goes here. A 100uF capacitor on the board keeps the voltage steady. The board is marked 12-24V.
  • 4-pin XH plug: the stepper motor plugs in here. The pins are marked 2B, 2A, 1A, 1B.
  • 3-way DIP switch: sets MS1, MS2 and MS3 (the M0, M1 and M2 pins). All three OFF = full step = 200 steps per revolution for a 1.8 degree motor.
  • G V S header: one row each for EN, STEP and DIR. S is the signal pin, G is GND. The V pin is only needed for an A4988, so we leave it free.
  • RST and SLP come pre-joined on the expansion board, which means the driver stays on.

Wiring Diagram

The wiring diagram between Raspberry Pi and Stepper Motor DRV8825 Driver

This image is created using Fritzing. Click to enlarge image

※ NOTE THAT:

  • Full-step mode needs the 3 DIP switches on the expansion board in the OFF position.
  • The pins on stepper motors may differ between manufacturers. Refer to the table below for the correct wiring.

Wiring table between Raspberry Pi and DRV8825 expansion board

Raspberry Pi pins DRV8825 expansion board pins
GPIO23 (pin 16) STEP (S pin)
GPIO24 (pin 18) DIR (S pin)
GND G pin

The EN row and the V pins stay free. On the expansion board, the VIN and GND screw terminal is where the 12V power comes in, from the adapter via a DC power jack.

Wiring table between DRV8825 expansion board and Stepper motor

Important!: Do not pay attention to the order of the wires in the stepper motor on the wiring diagram shown above. It is just an illustration. The pin arrangement on stepper motors can be different depending on the manufacturer. Make sure that your wiring follows the table below.

DRV8825 expansion board pins Stepper motor pins Or Or
1A A+ A A
1B A- A C
2A B+ B B
2B B- B D

Prior to purchasing a stepper motor, we suggest that you review the datasheet, specification, or manual for the stepper motor. Ensure that it includes the correlation between the pin's color and name. For instance, this stepper motor has the following mapping as shown in the image below:

Stepper Motor coil color mapping

In view of the mapping, the wiring table is as follows:

DRV8825 expansion board pins stepper motor pins wire color
1A A black wire
1B C green wire
2A B red wire
2B D blue wire

※ NOTE THAT:

One coil pair must go to 1A/1B and the other pair to 2A/2B. If you swap the two wires of one coil (for example 1A with 1B), the motor turns the other way (from clockwise to anticlockwise, and vice versa).

Raspberry Pi Code - Stepper Motor Code

The following code:

  • Makes the motor turn one revolution in a clockwise direction
  • Pauses the motor for 5 seconds
  • Causes the motor to rotate one revolution in an anticlockwise direction
  • Halts the motor for 5 seconds
  • Repeats the process endlessly

Detailed Instructions

  • Make sure you have Raspbian or any other Raspberry Pi compatible operating system installed on your Pi.
  • Make sure your Raspberry Pi is connected to the same local network as your PC.
  • Make sure your Raspberry Pi is connected to the internet if you need to install some libraries.
  • If this is the first time you use Raspberry Pi, See how to set up the Raspberry Pi
  • Connect your PC to the Raspberry Pi via SSH using the built-in SSH client on Linux and macOS or PuTTY on Windows. See to how connect your PC to Raspberry Pi via SSH.
  • Make sure you have the RPi.GPIO library installed. If not, install it using the following command:
sudo apt-get update sudo apt-get install python3-rpi.gpio
  • Create a Python script file drv8825_stepper_motor.py and add the following code:
""" This Raspberry Pi code was developed by newbiely.com This Raspberry Pi code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pi/raspberry-pi-stepper-motor """ import RPi.GPIO as GPIO import time # DRV8825 on its expansion board (BCM numbering) STEP_PIN = 23 # GPIO23 (pin 16) -> STEP (S pin) DIR_PIN = 24 # GPIO24 (pin 18) -> DIR (S pin) # Full step (all DIP switches OFF) DEG_PER_STEP = 1.8 STEP_PER_REVOLUTION = int(360 / DEG_PER_STEP) # 200 steps # Set GPIO mode and configure pins GPIO.setmode(GPIO.BCM) GPIO.setup(STEP_PIN, GPIO.OUT, initial=GPIO.LOW) GPIO.setup(DIR_PIN, GPIO.OUT, initial=GPIO.LOW) # Function to move the stepper motor a number of steps def move(steps, clockwise, delay): # DIR sets the direction GPIO.output(DIR_PIN, GPIO.HIGH if clockwise else GPIO.LOW) time.sleep(0.001) # short wait after changing DIR # Each pulse on STEP = one step for _ in range(steps): GPIO.output(STEP_PIN, GPIO.HIGH) time.sleep(delay) GPIO.output(STEP_PIN, GPIO.LOW) time.sleep(delay) try: # Wait time between STEP edges (smaller = faster) delay = 0.002 while True: # Move the stepper motor one revolution in a clockwise direction move(STEP_PER_REVOLUTION, True, delay) # Pause for 5 seconds time.sleep(5) # Move the stepper motor one revolution in an anticlockwise direction move(STEP_PER_REVOLUTION, False, delay) # Halt for 5 seconds time.sleep(5) except KeyboardInterrupt: print("\nExiting the script.") finally: # Clean up GPIO settings GPIO.cleanup()
  • Plug the 12V power adapter into the DC power jack.
  • Save the file and run the Python script by executing the following command in the terminal:
python3 drv8825_stepper_motor.py

The script runs in an infinite loop continuously until you press Ctrl + C in the terminal.

By changing the value of delay variable in the code, you can change the speed of the stepper motor. A smaller delay means shorter STEP pulses, so the motor turns faster.

You will observe the following:

  • The stepper motor will rotate one revolution in a clockwise direction.
  • The stepper motor will pause for 5 seconds.
  • The stepper motor will then rotate one revolution in an anticlockwise direction.
  • The stepper motor will pause for another 5 seconds.
  • This process will be repeated.

If the motor only hums and does not turn, turn the current-limit knob on the DRV8825 up a little.

Code Explanation

Check out the line-by-line explanation contained in the comments of the source code!

Wiring without the Expansion Board

This part shows the same circuit with the DRV8825 module alone. The Raspberry Pi code stays exactly the same.

The wiring diagram between Raspberry Pi and Stepper Motor DRV8825 Module without Expansion Board

This image is created using Fritzing. Click to enlarge image

To simplify and organize your wiring setup, we recommend using a Screw Terminal Block Shield for Raspberry Pi. This shield ensures more secure and manageable connections, as shown below:

Raspberry Pi Screw Terminal Block Shield
DRV8825 module pin Connect to
STEP Raspberry Pi GPIO23
DIR Raspberry Pi GPIO24
GND (next to FAULT) Raspberry Pi GND
VMOT + of the 12V adapter
GND (next to VMOT) - of the 12V adapter
2B and 1B one coil of the stepper motor
1A and 2A the other coil of the stepper motor
EN, M0, M1, M2, RST, SLP, FAULT not connected

※ NOTE THAT:

  • Keep the 12V supply stable with a 100uF capacitor across VMOT and GND. A 25V or 35V part is a good choice.
  • A silent motor often means RST is not pulled high. A short wire from RST to SLP fixes it.

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!