ESP8266 - Stepper Motor
In this tutorial, an ESP8266 and a DRV8825 driver work together to control a stepper motor. In detail, we will learn:
- How the ESP8266 sends STEP and DIR signals to a DRV8825 driver to control a bipolar stepper motor
- How to program ESP8266 to control the position of the stepper motor
- How to program ESP8266 to control the speed of the stepper motor
- How to program ESP8266 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 ESP8266 - 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 4 wires
- unipolar: this motor has either 5 or 6 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 5-wire unipolar stepper motors, refer to ESP8266 - control 28BYJ-48 stepper motor using ULN2003 driver.
The emphasis of this tutorial is solely on the bipolar stepper motor.
Bipolar Stepper Motor pinout
The pinout of the bipolar Stepper Motor has 4 pins. Different manufacturers use various names for the motor's pins. The following table provides some of the more commonly 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 sequence of pins, the labeling of wires, and the colors of the wires may differ between manufacturers. It is necessary to read the datasheet or manual to understand the correlation between the wire color and the pin name. The image above also displays the details of two distinct motors with dissimilar wire labeling and wire coloring.
Steps per Revolution
The specification of the motor indicates the DEG_PER_STEP. Depending on the type of control, STEP_PER_REVOLUTION can be determined using the table below:
| 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 datasheet states that it has 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 ESP8266
ESP8266 can produce signals to operate the stepper motor. However, the signals from ESP8266 lack the necessary voltage and/or current that the stepper motor needs. Consequently, a hardware driver is required between ESP8266 and the stepper motor. This driver has two functions:
- Increase the control signals from ESP8266 (current and voltage)
- Shield ESP8266 from the high current and voltage used to power the stepper motor.
There are various types of hardware drivers that can be used to manage stepper motors. From these, we pick the DRV8825 and place it on its expansion board.
Overview of DRV8825 Driver
Each bipolar stepper motor gets its own DRV8825 driver. The ESP8266 sends it a pulse on STEP for every step and a level on DIR for the direction. That is all: two pins. The driver keeps the coil current at the value you set, so the motor does not overheat. The older L298N on this page had no such limit, and it took four pins.
DRV8825 Driver Pinout

The DRV8825 module plugs into an expansion board, and we connect our wires to the expansion board:
- VIN and GND screw terminal: This supplies power for the motor. The expansion board is marked 12-24V (the DRV8825 itself accepts 8.2 to 45V); we use 12V. A 100uF capacitor on the board smooths this supply.
- STEP and DIR (S pins of the G V S header): These are connected to ESP8266's pins. Each pulse on STEP moves the motor one step, and DIR sets the direction.
- G pins: These are ground pins. One of them is connected to GND of the ESP8266.
- EN and V pins: These are not connected. With EN free, the driver is enabled. V is only used with an A4988 module.
- 2B, 2A, 1A, 1B plug: These are connected to the two coils of the stepper motor.
- 3-way DIP switch: This sets the microstep mode. With all switches OFF, the driver runs in full-step mode: 200 steps per revolution for a 1.8° motor.
- Current-limit knob (on the DRV8825): This sets the coil current, up to about 1.5A per coil without extra cooling.
The expansion board already connects RST to SLP, and this keeps the driver running. The DRV8825 accepts 3.3V logic, so the ESP8266 pins drive STEP and DIR directly.
Wiring Diagram
The NodeMCU connects to the G V S header of the expansion board with three jumper wires.

This image is created using Fritzing. Click to enlarge image
※ NOTE THAT:
- Turn the current-limit knob of the DRV8825 low before the first run. Then turn it up slowly until the motor runs smoothly. Heat becomes a problem when the coil current goes past roughly 1.5A.
- Plug in the 12V adapter only after all wires are in place, and never plug or unplug the motor while the power is on.
- The arrangement of pins on stepper motors may differ between manufacturers. Refer to the table below to ensure the correct wiring.
Wiring table between ESP8266 and DRV8825 Expansion Board
| ESP8266 pins | Expansion board pins |
|---|---|
| D1 | STEP (S pin) |
| D2 | DIR (S pin) |
| GND | G pin |
| Not connected | EN, V |
Power comes in through a DC power jack: the 12V adapter plugs into it, and its wires end at the VIN and GND screw terminal of the expansion board.
Wiring table between DRV8825 Expansion Board and Stepper motor
Important!: Pay no attention to the order of the wires in the stepper motor on the wiring diagram image. It is just an illustration. The arrangement of the pins on stepper motors may differ depending on the manufacturer. Make sure your wiring follows the table below.
| 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 of the stepper motor. Ensure that it includes the correlation between the pin's color and name. For instance, this stepper motor supplies the mapping as seen in the following image:

Using the mapping, the wiring table is:
| 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:
In the wiring tables presented above, 1A and 1B, as well as 2A and 2B, can be interchanged. This increases the number of possible wiring configurations. However, swapping the two wires of one coil makes the motor change its direction of rotation (clockwise to anticlockwise, or vice versa).
Wiring without the Expansion Board
Some kits come with the DRV8825 module but no expansion board. The ESP8266 does not notice any difference.

This image is created using Fritzing. Click to enlarge image
See more in ESP8266's pinout and how to supply power to the ESP8266 and other components.
| DRV8825 module pin | Connect to |
|---|---|
| STEP | ESP8266 D1 |
| DIR | ESP8266 D2 |
| GND (next to FAULT) | ESP8266 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:
- A 100uF capacitor (25V+) on VMOT and GND, placed right at the module, keeps the motor supply clean.
- A motor that stays still may mean the driver is asleep. Then connect RST to SLP with a short jumper wire.
How to control a Stepper motor using a DRV8825 driver.
Controlling a stepper motor can be a challenging endeavor, particularly when we need to do so without interruption. Fortunately, the AccelStepper library makes it a breeze.
The Arduino IDE includes a built-in Stepper library. However, we do not suggest utilizing this library as:
- It contains a blocking function, which prevents the ESP8266 from performing other tasks while controlling the stepper motor.
- It lacks sufficient features.
Instead, we suggest you utilize the AccelStepper library. This library provides:
- Acceleration
- Deceleration
- Easy use of a DRV8825 (or a similar STEP/DIR driver) in DRIVER mode
- The ability to control multiple steppers at once, with each stepper able to move independently
- Note: The microstep mode is chosen on the driver (DIP switch), not in the library
With the DRV8825, the stepper object is created in DRIVER mode:
How To Control the Position of Stepper Motor via DRV8825 Driver
We can move the stepper motor to the desired position (counted in steps from the reference point) with:
※ NOTE THAT:
The stepper.moveTo() function is non-blocking, which is a great advantage of the library. However, when using it, we must be careful:
- Ensure that 'stepper.run()' is called as often as possible, preferably within the void loop() function.
- Avoid using the delay() function while the motor is in motion.
- Do not use Serial.print() or Serial.println() functions while the motor is running, as this will slow down its movement.
How To Control the Speed of Stepper Motor via DRV8825 Driver
We can regulate both speed and acceleration/deceleration through the utilization of a few straightforward functions.
How To Control the Direction of Stepper Motor via DRV8825 Driver
Your code does not touch the DIR pin; the library changes it when needed. If you wire the motor as indicated, it will rotate in a:
- Clockwise direction: when the motor is moved from a lower position to a higher position (Position increment)
- Anticlockwise direction: when the motor is shifted from a higher position to a lower position (Position decrement)
- If the current position is 100, and we command the motor to 200, it will rotate in a clockwise direction.
- When the current position is -200 and the motor is instructed to -100, it will rotate in a clockwise direction.
- If the current position is 200 and the motor is commanded to 100, it will rotate in an anticlockwise direction.
- If the current position is -100 and the motor is instructed to -200, it will rotate in an anticlockwise direction.
※ NOTE THAT:
As previously noted, if you exchange 1A with 1B, or 2A with 2B, the increase in the position may be counterclockwise and the decrease in the position may be clockwise.
How To Stop Stepper Motor
- The stepper motor will come to a halt when it reaches the desired position.
- If an immediate stop is desired, the stepper.stop() function can be used.
ESP8266 Code - Stepper Motor Code
The code below:
- Makes the motor rotate 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 this process continually
Detailed Instructions
To get started with ESP8266 on Arduino IDE, follow these steps:
- Check out the how to setup environment for ESP8266 on Arduino IDE tutorial if this is your first time using ESP8266.
- Make sure the expansion board's three DIP switches are all OFF. This is the full-step setting.
- Wire the components as shown in the diagram. Keep the 12V adapter unplugged for now.
- Connect the ESP8266 board to your computer using a USB cable.
- Open Arduino IDE on your computer.
- Choose the correct ESP8266 board, such as (e.g. NodeMCU 1.0 (ESP-12E Module)), and its respective COM port.
- Click to the Libraries icon on the left bar of the Arduino IDE.
- Search for “AccelStepper”, then locate the AccelStepper library created by Mike McCauley.
- Click the Install button to install the AccelStepper library.

Copy the code and open it with the Arduino IDE.
Click the Upload button on the Arduino IDE to upload the code to the ESP8266.
Plug in the 12V adapter.
You will observe that the stepper motor rotates one revolution in the clockwise direction, pauses for 5 seconds, then rotates back one revolution in the anticlockwise direction and pauses again for 5 seconds. This process is repeated continuously.
Check the results on the Serial Monitor.
Code Explanation
Check out the line-by-line explanation contained in the comments of the source code!