Arduino Nano ESP32 - Stepper Motor
In this tutorial, we are going to learn how to use Arduino Nano ESP32 to control the stepper motor:
- How to wire a DRV8825 driver and use it to move a 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 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
There are two common types of stepper motors:
- Bipolar: These motors come with 4 wires.
- Unipolar: Unipolar motors can have either 5 or 6 wires. In the case of a 6-wire unipolar stepper motor, you can effectively use four out of the six wires to control it as if it were a bipolar stepper motor.
If you're specifically interested in a 5-wire unipolar stepper motor, you can refer to the tutorial titled Arduino Nano ESP32 - Control 28BYJ-48 Stepper Motor Using ULN2003 Driver.
However, this tutorial is exclusively dedicated to bipolar stepper motors.
Bipolar Stepper Motor pinout
A bipolar stepper motor has four pins, and these pins can be named differently depending on the manufacturer. Below, you'll find a table with some common pin names:
| 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 names of wires, and the colors of wires can differ from one manufacturer to another. To understand how the wire colors correspond to the pin names, it's essential to consult the datasheet or user manual provided by the manufacturer. The image above illustrates the specifications of two distinct motors, each featuring its unique wire naming and wire coloring conventions.
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 Nano ESP32
The Arduino Nano ESP32 can produce signals to command the stepper motor, but these signals may lack the necessary voltage and current levels that the stepper motor demands. Hence, an intermediary hardware driver is needed to bridge the gap between the Arduino Nano ESP32 and the stepper motor. This driver serves two primary functions:
- Amplifying Control Signals: It boosts the current and voltage of the control signals originating from the ESP32, ensuring that they meet the requirements of the stepper motor.
- Protecting the ESP32: Simultaneously, it shields the Arduino Nano ESP32 from the higher current and voltage levels used to power the stepper motor, preventing any potential damage.
Various hardware drivers are available for controlling stepper motors. Our choice for this tutorial is the DRV8825 driver, mounted on its expansion board.
Overview of DRV8825 Driver and Its Expansion Board
Meet the DRV8825: a little driver chip that runs a single bipolar stepper motor, like a NEMA 17. The Arduino Nano ESP32 only sends two signals: STEP (one pulse = one step) and DIR (the direction). The DRV8825 does the rest: it powers the two coils of the motor in the right order.
We picked it over the old L298N for three reasons: it limits the coil current (so the motor and the driver do not overheat), it can do microsteps for a smooth and quiet motion, and it needs only 2 control pins instead of 4.
DRV8825 Driver Pinout

Main facts of the DRV8825 module:
- Motor power: 8.2V to 45V. The expansion board is marked 12-24V. We use a 12V adapter.
- Current: about 1.5A for each coil (2.5A peak). The current limit comes from a small knob on the module: keep it low at first, then turn it up in small steps until the motor runs smoothly. Push it past about 1.5A and the chip heats up fast.
- Microstepping: a full step can be cut into smaller steps, down to 1/32 of a step.
- Logic: works with 3.3V and 5V boards, so the Arduino Nano ESP32 pins can drive it directly.
- Protection: over-current, over-temperature and under-voltage.
The expansion board has two sockets, and the DRV8825 module plugs right into them. This makes the wiring easy:
- VIN / GND screw terminal: the 12V input for the motor. A 100uF capacitor next to it keeps the voltage steady.
- 4-pin XH motor plug: marked 2B, 2A, 1A, 1B. One coil of the motor goes to 1A / 1B, the other coil to 2A / 2B.
- 3-way DIP switch: its three small switches decide the level of MS1, MS2 and MS3. When all three are OFF, the driver runs in full step: 200 steps per turn for a 1.8 degree motor. This tutorial uses full step.
- G V S header: one row each for EN, STEP and DIR. The S pin is the signal, G is GND and V is power.
- RST and SLP: the expansion board links these two pins, which keeps the DRV8825 out of sleep mode.
The EN pin and the V pins stay unconnected. When EN is free, the driver is on. The V pins are only needed for the A4988 driver, not for the DRV8825.
Wiring Diagram

This image is created using Fritzing. Click to enlarge image
Wiring table between Arduino Nano ESP32 and DRV8825 expansion board
| Arduino Nano ESP32 pins | DRV8825 expansion board pins |
|---|---|
| D4 | S pin of STEP |
| D3 | S pin of DIR |
| GND | G pin |
- EN and V pins: not connected.
- VIN and GND screw terminal: + and - of the 12V adapter (through the DC power jack).
Loose jumper wires are a common reason why a stepper misses steps. You can lock each wire in place with a Screw Terminal Expansion Board for Arduino Nano: the Arduino Nano ESP32 plugs into it, and every pin gets a screw. The wiring with the screw terminal board looks like this:

This image is created using Fritzing. Click to enlarge image
※ NOTE THAT:
- Plug in the 12V adapter only after all the wiring is done. Never plug or unplug the motor while the 12V is on: this can damage the DRV8825.
- Order of the pins on stepper motors may vary between manufacturers. Please check the below table for correct wiring.
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 expansion board 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 check 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 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:
The two wires of one coil must go to the same pair: 1A and 1B, or 2A and 2B. If you swap the two wires inside one pair (for example 1A with 1B), the motor still works, but it turns the other way (clockwise to anticlockwise, and vice versa).
Wiring without the Expansion Board
Do you want a smaller build? Leave out the expansion board and use the DRV8825 module by itself. It takes a few more wires, and the pins are small, so check each one twice.

This image is created using Fritzing. Click to enlarge image
The same circuit on the screw terminal shield:

This image is created using Fritzing. Click to enlarge image
| DRV8825 module pin | Connect to |
|---|---|
| STEP | Arduino Nano ESP32 D4 |
| DIR | Arduino Nano ESP32 D3 |
| GND (next to FAULT) | Arduino Nano ESP32 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 motor supply needs a 100uF capacitor here (25V or more), from VMOT to the GND beside it. Mind the polarity: + goes to VMOT.
- No movement at all? Add a short wire from RST to SLP.
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 Nano ESP32 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.
- Driver boards that only need STEP and DIR pulses, such as the DRV8825 (this is called DRIVER mode).
- Multiple simultaneous steppers, with independent concurrent stepping on each stepper.
With a DRV8825, the library only sends the STEP and DIR signals. The microstep size is set by the DIP switch on the expansion board, not by the code. We create the stepper object like this:
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, if you swap 1A with 1B, or 2A with 2B, the increment of the position may be anticlockwise and the decrement of the position may be 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 Nano ESP32 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
Detailed Instructions
To get started with Arduino Nano ESP32, follow these steps:
- If you are new to Arduino Nano ESP32, refer to the tutorial on how to set up the environment for Arduino Nano ESP32 in the Arduino IDE.
- Wire the components according to the provided diagram. Then flip the expansion board's three DIP switches to OFF, which gives full step.
- Connect the Arduino Nano ESP32 board to your computer using a USB cable.
- Launch the Arduino IDE on your computer.
- Select the Arduino Nano ESP32 board and its corresponding COM port.
- Open the Library Manager by clicking on the Library Manager icon on the left navigation bar of 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 Nano ESP32
- Plug in the 12V adapter.
- 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
※ NOTE THAT:
Does the motor only hum or shake instead of turning? Then give the current-limit knob on the DRV8825 a small turn up. If the module gets too hot to touch, turn it down.
Code Explanation
Read the line-by-line explanation in comment lines of source code!