In this getting started tutorial, we will learn about the 28BYJ-48 stepper motor and how to interface it with the ESP8266 NodeMCU development board using a UNL2003 driver. The 28BYJ-48 stepper motor is inexpensive and one of the most commonly used stepper motors out there. It comes with a UL2003 motor driver attached to it that is responsible for driving a stepper motor. The reason for using a driver is that ESP8266 GPIO pins can not provide enough driving current to a 28BYJ-48 stepper motor. The second important reason is that it protects ESP8266 GPIO pins from getting damaged due to the high current requirement of a stepper motor than a maximum current that ESP8266 GPIO pins can provide.
We have a similar guide with ESP32 and Arduino:
- ESP32 Interface with Stepper Motor (28BYJ-48 and ULN2003 Motor Driver)
- Control 28BYJ-48 Stepper Motor with ULN2003 Motor Driver and Arduino
Prerequisites
We will program our ESP8266 using the Arduino IDE. Make sure you are familiar with and have the latest version of Arduino IDE installed. Moreover, you will also require the ESP8266 add-on installed in Arduino IDE:
We will require the following components for this user guide:
Required Components
- ESP8266 NodeMCU development board
- One 28BYJ-48 Stepper Motor with ULN2003 motor driver
- External 5V power supply
- Connecting Wires
Stepper Motors Introduction
Stepper motors are DC brushless and synchronous motors. They rotate in discrete steps of predefined values and are able to rotate both clockwise and anticlockwise. Unlike other DC motors, they provide a precise position control according to the number of steps per revolution for which the motor is designed. That means a complete revolution of a stepper motor is divided into a discrete number of steps. They are commonly used in CNC machines, Robotics, 2D and 3D printers.
For this guide, we will use a 28BYJ-48 stepper motor and control it through ULN2003 motor driver.
28BYJ-48 Stepper Motor
This is the most commonly used stepper motor in low power industrial and most famously in hobbyist projects.
28BYJ-48 is a uni-polar 5V stepper motor that takes electrical signals as input and rotates by converting these input signals into mechanical rotation. It consists of 4 stationary coils rated at +5V. These coils are known as a stator and make a ring around the rotor. Because of 5 volts operating voltage, we can easily drive this motor from any microcontroller such as ESP32, ESP8266, Arduino, or TM4C123 Tiva Launchpad, etc. It has a 1/64 reduction gear set and therefore moves in precise 512 steps per revolution. These motors are silent in comparison to other DC motors and servo motors. You can achieve positional control easily without needing extra circuitry and components.
Stride Angle
This stepper motor has a stride angle of 5.625 degrees. That means 28BYJ-48 will complete one revolution in (360/5.625) 64 steps by taking one step at a time and in one step it covers a 5.625-degree distance. However, the stepper motor can also be used in full-step mode. In full-step mode, the angle of each step is 11.25 degrees. That means the motor completes its one revolution in 32 steps instead( 360/11.25).
Therefore, in order to move one step forward or backward, the coils of the motor energize with a particular sequence.
Steps per Revolution & Step Angle
The output shaft of this particular stepper motor is driven through a gear ratio of 64:1 that is also known as the speed variation ratio. This suggests that after the inside motor rotates 64 times then the shaft will complete one rotation.
Therefore we can conclude that:
- In order to complete one full rotation of the shaft a total of 2048 steps will be required. This is known as the steps per revolution calculated by multiplying 32 and 64 (32×64=2048).
- Moreover, this will enable the motor to have a step angle of 360º/2048 steps = 0.18º/step.
Specifications
- It is a unipolar 5 pin coil with a rated DC voltage of 5V.
- Has 4 phases with a stride angle of 5.625°/64.
- Speed variation ratio is 1/64
- The frequency of this stepper motor is 100Hz and insulated power is 600VAC/1mA/1s.
- The half-step method is recommended for driving this stepper motor.
- The value of pull in torque for a stepper motor is 300 gf-cm.
Pinout
The following figure shows the pinout diagram of the 28BYJ-48 stepper motor. It consists of 5 pins. Out of these 5 pins, four pins are used to provide sequence logic to the coils and one pin is +5 volts supply pin.
Pin Configuration Details
Pin Number | Coil Number | Colour |
1 | 4 | Blue |
2 | 2 | Pink |
3 | 3 | Yellow |
4 | 1 | Orange |
5 | Vcc | Red |
Coil 1-Coil 4: These are coils used to control the step sequence of the stepper motor. One end of each coil is connected with +5V and the other end will be connected with ULN2003 driver output.
Vcc: Used to apply +5 volt supply to the stepper motor. This voltage appears across the coils when a specific coil is ground through a control sequence.
ULN2003 Stepper Motor Driver Module
To use a 28BYJ-28 stepper motor with the ESP8266 board we will be required to attach it with the ULN2003 motor driver. This is necessary because the current consumption of 28BYJ-48 is around 240mA. That means the current required to drive coils by applying a sequence of control signals is also almost 200mA. GPIO pins of ESP development boards can not provide a current of this magnitude. Therefore, we need a ULN2003 driver which translates the low current output of ESP8266 GPIO pins into a higher current that meets the requirement of stepper motor control signals.
The ULN2003 breakout board has high current and voltage than a single transistor and therefore it can drive a stepper motor easily by enabling our ESP board.
Recommneded reading: ULN2003 introduction, pinout, example and features
Stepper Motor Driver Module Pinout
ULN2003 driver IC consists of 7 Darlington pair transistor outputs. Each output can drive 500mA and 50V load. The input to each 7 Darlington pair transistor will be a signal from our microcontroller. To drive a stepper motor, this driver board uses only four input pins (IN1, IN2, IN3, and IN4).
The following diagram shows the ULN2003 motor driver board:
Motor Connector Header is used to connect the stepper motor. It provides output from four Darlington pair transistors.
Pin | Description |
1N1 to IN4 | These are input pins used to provide control signals to the stepper motor such as control sequences. We will connect these pins with the GPIO pins of ESP8266. |
Vcc and GND | Vcc is a power supply pin and it is used to provide 5 volts power to the stepper motor from an external power source. |
ESP8266 does not have an onboard 5 volts signal. Therefore, we will need to provide external power supply of 5V to the motor driver in order for it to work.
Additionally, the 28BYJ-48 stepper motor requires 240mA current to operate and it also consumes power at an idle condition. Therefore, it is recommended not to power the 28BYJ-48 stepper motor directly from any microcontroller instead use an external 5 volts power supply.
Interfacing ESP8266 with 28BYJ-48 Stepper Motor using ULN2003 Driver
To connect the ESP8266 development board with the stepper motor and driver we will use the input pins IN1-IN4, the power supply pins, and the motor connection header. The 28BYJ-48 stepper motor already comes attached with the motor driver via the motor connector header. Now we will connect four GPIO pins of our ESP8266 board with the input pins (IN1-IN4) of the driver. We have used the following GPIO pins to connect with each input pin.
ESP8266 NodeMCU | Motor Driver |
---|---|
GPIO14 (D5) | IN1 |
GPIO12 (D6) | IN2 |
GPIO13 (D7) | IN3 |
GPIO15 (D8) | IN4 |
You can use any suitable ESP8266 GPIO pins as well.
Additionally, we will power the motor driver with 5V external power supply. Both the grounds of the power supply and the ESP8266 board will be in common.
The connection diagram is shown in the picture below.
Installing AccelStepper Library
We will require the AccelStepper library present in Arduino Library Manager to control the stepper motor using ESP8266. This library will provide us useful functions to set the maximum speed, acceleration and steps per revolution to rotate the motor in both directions.
To install the library, we will use the Arduino Library Manager. Open your Arduino IDE and go to Sketch > Include Libraries > Manage Libraries. Type ‘Accelstepper’ in the search bar and install the latest version.
ESP8266 Arduino Sketch Controlling 28BYJ-48 Stepper Motor
Open your Arduino IDE and go to File > New. A new file will open. Copy the code given below in that file and save it.
This code will help us control the stepper motor by setting the maximum speed, acceleration and steps per revolution. We will show you how to rotate the motor in both directions.
#include <AccelStepper.h>
const int steps_per_rev = 2048;
#define IN1 D5
#define IN2 D6
#define IN3 D7
#define IN4 D8
AccelStepper motor(AccelStepper::HALF4WIRE, IN1, IN3, IN2, IN4);
void setup() {
Serial.begin(115200);
motor.setMaxSpeed(700);
motor.setAcceleration(200);
motor.moveTo(steps_per_rev);
}
void loop() {
if (motor.distanceToGo() == 0){
motor.moveTo(-motor.currentPosition());
Serial.println("Rotating Motor in opposite direction...");
}
motor.run();
}
How the Code Works?
Firstly, we will include the AccelStepper.h library. This library provides useful functions that make it easy to control the stepper motor using ESP8266.
#include <AccelStepper.h>
The next step is to define the steps per revolution. This is the number of steps our motor requires to move one complete revolution. Previously, we showed the calculations of obtaining this value.
const int steps_per_rev = 2048;
Next, we will define the input pins of the motor connections with the ESP8266 board. As you can see we have used the GPIO pins 14,12,13 and 15 to connect with IN1, IN2, IN3 and IN4 respectively. However, you can use any other suitable ESP8266 GPIO pins as well.
#define IN1 D5
#define IN2 D6
#define IN3 D7
#define IN4 D8
Now we will create an instance of the AccelStepper library called motor() and pass HALF4WIRE to indicate that we want to control the first four wires and the individual motor input pins as arguments. Make sure you specify the input pins in their correct sequence. It is IN1, IN3, IN2, and IN4 for 28BYJ-48 stepper motor.
AccelStepper motor(AccelStepper::HALF4WIRE, IN1, IN3, IN2, IN4);
Inside the setup() function, Serial.begin() is used to establish the serial connection between the development board at a baud rate of 115200. We will use the setMaxSpeed() method on the motor instance and pass the speed of the motor in steps per second as an argument inside it. In our case we are setting the stepper motor speed to 700 steps per second. We will also set the acceleration of the motor using setAcceleration() and pass the acceleration in steps per second per second. The moveTo() method takes in the argument steps per revolution which we had defined as 2048. This will be used to mark our target position.
void setup() {
Serial.begin(115200);
motor.setMaxSpeed(700);
motor.setAcceleration(200);
motor.moveTo(steps_per_rev);
}
Inside the loop() function, we will rotate the motor in both directions. This will be achieved by keeping track of the target position. If the target position has been reached then another target position will be marked. This will be the same in value as before but with a negative sign. Thus, reversing the direction of the motor. Additionally, we will print when the motor changes direction in the serial monitor as well. The run() method will be responsible to rotate the motor every one step at a time.
void loop() {
if (motor.distanceToGo() == 0){
motor.moveTo(-motor.currentPosition());
Serial.println("Rotating Motor in opposite direction...");
}
motor.run();
}
Demonstration
Make sure you choose the correct board and COM port before uploading your code to the board. Go to Tools > Board and select NodeMCU 1.0.
Next, go to Tools > Port and select the appropriate port through which your board is connected.
Click on the upload button to upload the code into the ESP8266 development board.
After you have uploaded your code to the development board, press its RST button.
The stepper motor will start rotating clockwise and then anti-clockwise repeatedly.
In your Arduino IDE, open up the serial monitor and you will be able to see the status of the motor rotation as well.
Below you can view the demonstration of the stepper motor as well.
Conclusion
In conclusion, in this introductory guide to 28BYJ-48 stepper motor with ULN2003 motor driver, we have learned about the stepper motor and its driver ULN2003. We saw its introduction, specifications pinout, and interfacing with the ESP8266 NodeMCU. Then, we controlled the stepper motor by programming our ESP8266 NodeMCU board in Arduino IDE using the Stepper.h library. This included rotating it both clockwise and in an anti-clockwise direction. In the next tutorial, we will show you how to create a WebSocket web server to control the stepper motor.
Related tutorials and projects:
- Stepper Motor Interfacing with TM4C123 Tiva Launchpad
- 28BYJ-48 5 volt Stepper Motor Guide
- Stepper Motor Control with L293D Motor Driver IC and Arduino
- NEMA 23 Stepper Motor Pinout, features and example with Arduino
- STEPPER MOTOR INTERFACING WITH 8051 MICROCONTROLLER
- TMC2209 Stepper Motor Driver module – SilentStepStick
- DC Motor Speed and Direction Control with L293D Driver IC and Arduino
- NEMA 34 Stepper Motor
- STEPPER MOTOR INTERFACING WITH PIC16F877A MICROCONTROLLER
Hi, I substituted a ULN2003A chip for the motor controller board because I didn’t really want the LEDs flashing. It seems to work fine except that the motor stuttered and struggled. I changed the line
AccelStepper motor(AccelStepper::HALF4WIRE, IN1, IN3, IN2, IN4);
to
AccelStepper motor(AccelStepper::HALF4WIRE, IN1, IN2, IN3, IN4);
This stopped the stuttering and was the first combination I tried, and by luck was correct. Any idea why I had to do this? I assume this changes the order in which the coils are powered (like changing the HT leads from the distributor to the spark plugs on a car).
Yes, the motor which you are using might have a different coils placement. You can refer to its datasheet.
Thank you for this.
Using a nodeMCU with the arduino IDE. Had to disconnect the D8 wire before the sketch would upload – was getting a ‘no header packet received’ (something like that). Once uploaded, I could reconnect the D8 wire and the motor ran as expected. Any thoughts?
Thank you for the article.
Can I also operate the circuit without an additional 5V voltage? Is the 3.3V of the ESP sufficient for this?