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:
- What the DRV8825 driver does, and how the Raspberry Pi uses it to turn a bipolar stepper motor
- How to program Raspberry Pi to control the position of the stepper motor
- How to program Raspberry Pi to control the speed of the stepper motor
- How to program Raspberry Pi to control the direction of the stepper motor
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.

If you want control another type of stepper motor, please check out this Raspberry Pi - 28BYJ-48 Stepper Motor tutorial.
Hardware Preparation
Or you can buy the following kits:
| 1 | × | DIYables Sensor Kit (18 sensors/displays) |
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 |

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

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

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:

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:
- Create a Python script file drv8825_stepper_motor.py and add the following code:
- 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:
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.

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:

| 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.