Arduino - Stepper Motor
In this tutorial, we are going to learn:
- How to use DRV8825 driver to control bipolar stepper motor
- How to control the position of stepper motor
- How to control the speed of stepper motor
- How to control the direction of stepper motor
The tutorial can apply to any kind of bipolar stepper motor (4 wires). The tutorial will take NEMA 17 stepper motor as an example.

Hardware Required
Or you can buy the following kits:
| 1 | × | DIYables STEM V3 Starter Kit (Arduino included) | |
| 1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some links direct to products from our own brand, DIYables .
About Stepper Motor
There are two popular types of stepper motors:
- bipolar: this type of motor has 4 wires
- unipolar: this type of motor has 5 wires or 6 wires.
For a 6-wire unipolar stepper motor, we can use four of six wires and control it as a bipolar stepper motor.
For 5-wire unipolar stepper motor, see Arduino - control 28BYJ-48 stepper motor using ULN2003 driver
This tutorial focuses only on the bipolar stepper motor.
Bipolar Stepper Motor pinout
The bipolar Stepper Motor pinout has 4 pins. Depending on manufacturers, the motor's pins have several naming. The below table shows some wide-used pin-naming:
| 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 order of pins, wire naming, and wire coloring can vary between manufacturers. You need to read the datasheet or manual to see the mapping between wire color and pin name. The above image also shows the specification of two different motors with different wire naming and wire coloring.
Steps per Revolution
The motor's specification specifies the degree per step (let's call DEG_PER_STEP). Depending on the method of control, the steps per revolution (let's call STEP_PER_REVOLUTION) is calculated as 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 example, If the motor's datasheet specifies 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 Arduino
Arduino can generate signals to control the stepper motor. However, the signals from Arduino do not have enough voltage and/or current that the stepper motor requires. Therefore, we need a hardware driver in between Arduino and the stepper motor. The driver does two works:
- Amplify the control signals from Arduino (current and voltage)
- Protect Arduino from high current and voltage that is used to power the stepper motor.
There are many kinds of hardware drivers that can be used to control stepper motors. The DRV8825 Driver is a popular one. It is used in many 3D printers and CNC machines.
About DRV8825 Driver
A single DRV8825 Driver controls one bipolar stepper motor. In this tutorial, we plug the DRV8825 module into its expansion board. The expansion board gives us a screw terminal for the power, a plug for the motor, and a small header for the control pins.
Why this driver? It limits the current in the motor coils, so the motor and the driver do not get too hot. It supports microstepping, which makes the motor run smooth and quiet. And Arduino needs only two pins to control it: STEP and DIR.
DRV8825 Driver Pinout

The main facts of the DRV8825:
- Motor supply: 8.2V to 45V. The expansion board marks 12-24V. We use a 12V power adapter.
- Current: up to about 1.5A per coil, with short peaks to 2.5A. You choose the limit with the tiny knob (potentiometer) on the module.
- Microstepping: full step, 1/2, 1/4, 1/8, 1/16 and 1/32 step.
- Logic: works with 3.3V and 5V signals.
- Protection: over-current, over-temperature and under-voltage.
The expansion board has these parts:
- VIN and GND screw terminal: supplies power for the motor. A 100uF capacitor sits next to it to keep the voltage steady.
- 2B, 2A, 1A, 1B plug: a 4-pin XH plug for the stepper motor. 1A and 1B go to one coil, 2A and 2B go to the other coil.
- G V S header for EN, STEP and DIR: three rows of pins. In each row, G is ground, V is a supply pin and S is the signal. Arduino drives the S pins of STEP and DIR, and one G pin gives the common ground. Leave V free (only the A4988 uses it). Leave EN free too: then the driver is enabled.
- DIP switch (MS1, MS2, MS3): here you choose the microstep mode. When all three switches are OFF, the driver runs in full-step mode: 200 steps per revolution for a 1.8° motor.
On this board, RST and SLP are tied together, which keeps the chip out of sleep mode. The DRV8825 does not take 5V from Arduino either. It runs fully on the 12V from the VIN and GND terminal, and Arduino only shares its GND with it.
Wiring Diagram

This image is created using Fritzing. Click to enlarge image
※ NOTE THAT:
- Put the three DIP switches (MS1, MS2, MS3) in the OFF position: this is full-step mode.
- Order of the pins on stepper motors may vary between manufacturers. Please check the below table for correct wiring.
Wiring table between Arduino and DRV8825 Expansion Board
| Arduino pins | DRV8825 Expansion Board pins |
|---|---|
| 4 | STEP (S pin) |
| 3 | DIR (S pin) |
| GND | G pin |
Nothing goes to the EN pin or the V pin. For the motor power, the plug of the 12V adapter goes into the DC power jack, and the two wires of the jack go to the VIN and GND screw terminal.
Wires that touch a moving machine can come loose. To avoid this, you can use a Proto Screw Terminal Block Shield or a Screw Shield V2 on top of the Arduino Uno. The three wires then go into the screw terminals of pins 4, 3 and GND, as below.

This image is created using Fritzing. Click to enlarge image
Wiring table between DRV8825 Expansion Board and Stepper motor
Important!: Please do not care about the wire order of the stepper motor on the above wiring diagram image. It is just an example. The order of the pins on stepper motors may vary between manufacturers. Make sure that your wiring follows the below table.
| DRV8825 pins | Stepper motor pins | Or | Or |
|---|---|---|---|
| 1A | A+ | A | A |
| 1B | A- | A | C |
| 2A | B+ | B | B |
| 2B | B- | B | D |
Before buying a stepper motor, we would recommend you check the datasheet, specification, or manual of the stepper motor. Make sure that they provide the mapping between the pin's color and name. For example, This stepper motor provides the mapping as below image:

Based on that mapping, the wiring table becomes:
| DRV8825 pins | stepper motor pins | wire color |
|---|---|---|
| 1A | A | black wire |
| 1B | C | green wire |
| 2A | B | red wire |
| 2B | D | blue wire |
※ NOTE THAT:
In all the above wiring tables between the stepper motor and DRV8825 Driver, we can swap 1A with 1B, or 2A with 2B. Therefore, there are more ways to do the wiring. However, if we swap the two wires of one coil, the motor's rotation direction is changed (clockwise to anticlockwise, and vice versa).
Before the first run, turn the small current-limit knob on the DRV8825 module to a low setting. Then run the motor and turn the knob up slowly until the motor turns smoothly and does not miss steps. Do not go higher than you need: above about 1.5A, the module gets hot.
Wiring without the Expansion Board
No expansion board? You can wire the DRV8825 module straight to the Arduino. On the Arduino side nothing changes: the same pins as above.

This image is created using Fritzing. Click to enlarge image
With a screw terminal shield, the same wiring looks like this:

This image is created using Fritzing. Click to enlarge image
| DRV8825 module pin | Connect to |
|---|---|
| STEP | Arduino D4 |
| DIR | Arduino D3 |
| GND (next to FAULT) | Arduino 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:
- The expansion board has a 100uF capacitor on the motor supply; the bare module does not. Place one (25V or higher) across VMOT and its GND pin, near the module.
- When the motor stays silent, try a jumper between the RST and SLP pins.
How to control a Stepper motor using a DRV8825 driver.
Controlling a stepper motor is not a simple task, especially when we want to control it in a non-blocking manner. Fortunately, thanks to AccelStepper library, controlling the stepper motor becomes a piece of cake.
Arduino IDE also has a built-in Stepper library. However, We do not recommend you to use this library because:
- The library provides the blocking function. It means it blocks Arduino from doing other works while it controls the stepper motor.
- It does not have sufficient functions.
Instead, we recommend you use the AccelStepper library. This library supports:
- Acceleration
- Deceleration.
- The DRV8825 and other drivers with STEP and DIR inputs, through its DRIVER mode
- Multiple simultaneous steppers, with independent concurrent stepping on each stepper.
We create the stepper object in DRIVER mode, with the STEP pin first and then the DIR pin:
The library sends one pulse to the STEP pin for each step. The microstep mode is not set in the code. It is set by the DIP switch on the expansion board.
How To Control the Position of Stepper Motor via DRV8825 Driver
We can move the stepper motor to the desired position by using:
※ NOTE THAT:
The stepper.moveTo() function is non-blocking. This is a great point of the library. However, we have to pay attentions when using this function:
- Call 'stepper.run()' as frequently as possible. It should be called in the void loop() function.
- Do NOT use delay() function when motor is moving.
- Should NOT use Serial.print() and Serial.println() function when motor is moving. These functions make the stepper motor move slower.
How To Control the Speed of Stepper Motor via DRV8825 Driver
We can control not only the speed but also acceleration and deceleration by using some simple functions.
How To Control the Direction of Stepper Motor via DRV8825 Driver
If you do the wiring as above, the motor will rotate in:
- Clockwise direction: if we control the motor from a position to higher position (Position increment)
- Anticlockwise direction: if we control the motor from a position to lower position (Position decrement)
For example:
- If the current position is 100 and we control the motor to 200, the motor rotates in the clockwise direction
- If the current position is -200 and we control the motor to -100, the motor rotates in the clockwise direction
- If the current position is 200 and we control the motor to 100, the motor rotates in the anticlockwise direction
- If the current position is -100 and we control the motor to -200, the motor rotates in the anticlockwise direction
※ NOTE THAT:
As mentioned before, swapping the two wires of one coil (1A with 1B, or 2A with 2B) flips the direction. Then a higher position may turn the motor anticlockwise, and a lower position clockwise.
How To Stop Stepper Motor
- The stepper motor will automatically stop after reaching the desired position.
- The stepper motor can be forced to stop immediately anytime by using stepper.stop() function.
Arduino Code - Stepper Motor Code
The below code does:
- Rotate motor one revolution in the clockwise direction
- Stop motor 5 seconds
- Rotate motor back one revolution in the anticlockwise direction
- Stop motor 5 seconds
- That process is repeated again and again
Quick Steps
- Navigate to the Libraries icon on the left bar of the Arduino IDE.
- Search “AccelStepper”, then find the AccelStepper library by Mike McCauley
- Click Install button to install AccelStepper library.

- Copy the above code and open with Arduino IDE
- Click Upload button on Arduino IDE to upload code to Arduino
- Plug the 12V power adapter into the DC power jack
- You will see:
- Stepper motor rotates one revolution in the clockwise direction
- Stepper motor stops 5 seconds
- Stepper motor rotates back one revolution in the anticlockwise direction
- Stepper motor stops 5 seconds
- The above process is run repeatedly.
- See the result on Serial Monitor
Code Explanation
Read the line-by-line explanation in comment lines of source code!
Video Tutorial
We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.