Stepper Motor Control with ESP8266 NodeMCU

This post shows how to control unipolar stepper motor using ESP8266 NodeMCU board (ESP-12E) and rotary encoder module.
The stepper motor used in this example is 28BYJ-48 (5V unipolar stepper motor) which usually comes with its driver board (equipped with ULN2003 integrated circuit).

In this project I used the rotary encoder shown below:

rotary encoder

The rotary encoder module has 5 pins: GND, + (+5V or 3.3V), SW (push button), DT (pin B) and CLK (pin A).
As an addition to the rotary encoder there is a push button and three pull up resistors for pins SW, DT and CLK of 10k ohm. With the three pull-up resistors, the normal state of each terminal is logic high.
The rotary encoder generates (when rotating) two square waves on pins A (CLK) and B (DT) with 90° out of phase as shown in the figure below:

rotary encoder output waveform

Since the normal state of pin A (CLK) and pin B (DT) are logic high we’ve to detect falling (transition from high to low) of one of them, here pin A is used to detect the movement of the rotary encoder in both directions (falling of pin A signal). Direction of rotation can be detected by knowing the status of pin B, if pin B is logic high this means the direction of rotation is clockwise (CW), and if pin B is logic low this means the direction of rotation is counter clockwise (CCW).

Hardware Required:

  • ESP8266 NodeMCU board
  • 28BYJ-48 stepper motor (with ULN2003A driver board)
  • Rotary encoder module
  • 5V power source
  • Micro USB cable (for programming and powering the NodeMCU)
  • Bread board
  • Jumper wires

Stepper motor control with NodeMCU circuit:
Project circuit schematic diagram is shown below.

ESP8266 NodeMCU stepper motor control circuit rotary encoder

The stepper motor is connected to the ULN2003A board which is supplied with external power source of 5V. The control lines (IN1, IN2, IN3 and IN4) of this board are connected to the NodeMCU as:
IN1 to NodeMCU pin D1,
IN2 to NodeMCU pin D2,
IN3 to NodeMCU pin D3,
IN4 to NodeMCU pin D4.

The rotary encoder module has 5 pins: GND, + , SW, DT (pin B or data pin) and CLK (pin A or clock pin) where:
GND is connected to NodeMCU GND pin,
+ is connected to NodeMCU 3V3 pin,
SW is push button pin, not used in this example,
DT is connected to NodeMCU pin D5,
CLK is connected to NodeMCU pin D6.

Stepper motor control with NodeMCU code:
In this example I used Arduino stepper motor library (built-in) which simplifies the code, it’s included in the code using the following line:

The stepper motor which I used in this project is 28BYJ-48, this motor is equipped with speed reducer of 1/64. The internal motor has 32 steps per one revolution which means the external shaft has 2048 steps per one revolution (64 x 32). Number of steps is defined in the code as shown below:

and the connection of the control lines of the stepper motor with the NodeMCU board are defined as:

The rotary encoder pin A (CLK) and pin B (DT) are connected to NodeMCU pins D6 and D5 respectively. They are defined as:

Both pins can be used to interrupt the NodeMCU microcontroller (ESP8266EX) whenever there is a change in the state of at least one pin. The following lines are used to enable interrupt-on-change for pins D6 and D5:

When the rotary encoder is rotated, it interrupts the NodeMCU microcontroller which directly will execute the function enc_read():

Full Arduino code:

The following video shows my simple hardware circuit test:

2 thoughts on “Stepper Motor Control with ESP8266 NodeMCU”

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top