IoT/ESP32 B.Tech/Diploma Beginner

ESP32 with Stepper Motor (28BYJ-48 and ULN2003 Motor Driver)

Learn how to control a stepper motor with the ESP32. We'll use the 28BYJ-48 unipolar stepper motor with the ULN2003 motor driver. The ESP32 board will be programmed using Arduino IDE.

  • Suitable forB.Tech/Diploma IoT/ESP32 students
  • Build time1-2 weeks
  • LevelBeginner
  • Available atAjvika Labs, Kakinada

About this project

In this guide, you’ll learn how to control a stepper motor with the ESP32. We’ll use the 28BYJ-48 unipolar stepper motor with the ULN2003 motor driver. The ESP32 board will be programmed using Arduino IDE.
We have a similar tutorial for the ESP8266 board:
ESP8266 NodeMCU with Stepper Motor (28BYJ-48 and ULN2003 Motor Driver)
We have tutorials for other motors with the ESP32:
ESP32 with
DC Motor
and L298N Motor Driver – Control Speed and Direction
ESP32
Servo Motor
Web Server with Arduino IDE
Parts Required
To follow this tutorial, you need the following parts:
28BYJ-48 Stepper Motor
ULN2003 Motor Driver
ESP32
(read
Best ESP32 Development Boards
)
Jumper Wires
5V Power Supply
You can use the preceding links or go directly to
MakerAdvisor.com/tools
to find all the parts for your projects at the best price!
Introducing Stepper Motors
A stepper motor is a brushless DC electric motor that divides a full rotation into a number of steps. It moves one step at a time, and each step is the same size. This allows us to rotate the motor by a precise angle to a precise position. The stepper motor can rotate clockwise or counterclockwise.
The following picture shows two 28BYJ-48 stepper motors.
Stepper motors are made of internal coils that make the motor shaft move in steps in one direction or the other when current is applied to the coils in a specific way.
There are two types of stepper motors: unipolar and bipolar stepper motors.
In this article, we won’t detail how the stepper motors are made and how they work internally. To learn in more detail how they work and the differences between each type of stepper motor, we recommend reading this article by the
DroneBotWorkshop blog
28BYJ-48 Stepper Motor
There are several stepper motors with different specifications. This tutorial will cover the widely used
28BYJ-48 unipolar stepper motor
with the
ULN2003 motor driver
28BYJ-48 Stepper Motor Features
Features of the stepper motor (for more details, consult the datasheet):
Rated voltage: 5V DC
Number of phases: 4
Speed variation ratio: 1/64
Stride angle: 5.625º/64
Frequency: 100Hz
The 28BYJ-48 stepper motor has a total of four coils. One end of the coils is connected to 5V, which corresponds to the motor’s red wire. The other end of the coils corresponds to the wires with blue, pink, yellow, and orange color. Energizing the coils in a logical sequence makes the motor move one step in one direction or the other.
The 28BYJ-48 Stepper Motor has a stride angle of 5.625°/64 in half-step mode. This means that the motor has a step angle of 5.625º—so it needs 360º/5.625º = 64 steps in half-step mode. In full-step mode: 64/2 = 32 steps to complete one rotation.
However, the output shaft is driven via a 64:1 gear ratio. This means that the shaft (visible outside the motor) will complete a rotation if the motor inside rotates 64 times. This means that the motor will have to move 32×64 = 2048 steps for the shaft to complete one full rotation. This means that you’ll have a precision of 360º/2048 steps = 0.18º/step.
So, in summary:
Total steps per revolution = 2048 steps
Step angle = 0.18º/step
If you’re using a different stepper motor, please consult the datasheet.
ULN2003 Motor Driver
To interface the stepper motor with the ESP32, we’ll use the ULN2003 motor driver, as shown in the figure below. The 28BYJ-48 stepper motor is many times sold together with the ULN2003 motor driver.
The module comes with a connector that makes it easy and simple to connect the motor to the module. It has four input pins to control the coils that make the stepper motor move. The four LEDs provide a visual interface of the coils’ state.
There are pins to connect
VCC
and
GND,
and a jumper cap that acts as an ON/OFF switch to power the stepper motor—if you remove the jumper, there is no power reaching the motor. You can use those pins to wire a physical switch.
ULN2003 Motor Driver Pinout
The following table shows the module pinout:
IN1
Control the motor: connect to a microcontroller digital pin
IN2
Control the motor: connect to a microcontroller digital pin
IN3
Control the motor: connect to a microcontroller digital pin
IN4
Control the motor: connect to a microcontroller digital pin
VCC
Powers the motor
GND
Common GND
Motor connector
Connect the motor connector
Wire Stepper Motor to the ESP32
In this section, we’ll connect the stepper motor to the ESP32 via the ULN2003 motor driver.
We’ll connect IN1, IN2, IN3, and IN4 to GPIOs

, and

. You can use any other suitable digital pins (check our
ESP32 pinout reference guide
).
You can follow the next schematic diagram.
Note:
you should power the motor driver using an external 5V power supply.
Motor Driver
ESP32
IN1
GPIO 19
IN2
GPIO 18
IN3
GPIO 5
IN4
GPIO 17
Control Stepper Motor with the ESP32 – Code
There are different ways to control stepper motors with a microcontroller. We’ll use the Arduino built-in
Stepper.h
library. This library provides an easy way to move the motor by a defined number of steps.
Copy the following code to your Arduino IDE.
/*
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
Based on Stepper Motor Control - one revolution by Tom Igoe
*/
#include <Stepper.h>

const int stepsPerRevolution = 2048; // change this to fit the number of steps per revolution

// ULN2003 Motor Driver Pins
#define IN1 19
#define IN2 18
#define IN3 5
#define IN4 17

// initialize the stepper library
Stepper myStepper(stepsPerRevolution, IN1, IN3, IN2, IN4);

void setup() {
// set the speed at 5 rpm
myStepper.setSpeed(5);
// initialize the serial port
Serial.begin(115200);
}

void loop() {
// step one revolution in one direction:
Serial.println("clockwise");
myStepper.step(stepsPerRevolution);
delay(1000);

// step one revolution in the other direction:
Serial.println("counterclockwise");
myStepper.step(-stepsPerRevolution);
delay(1000);
}
We adapted this code from the examples provided by the Stepper library (
File
>
Examples
>
Stepper
>
stepper_oneRevolution
).
How the Code Works
First, include the
Stepper.h
library.
#include <Stepper.h>
Define the steps per revolution of your stepper motor—in our case, it’s 2048:
const int stepsPerRevolution = 2048; // change this to fit the number of steps per revolution
Define the motor input pins. In this example, we’re connecting to GPIOs 19, 18, 5, and 17, but you can use any other suitable GPIOs.
#define IN1 19
#define IN2 18
#define IN3 5
#define IN4 17
Initialize an instance of the stepper library called
myStepper
. Pass as arguments the steps per revolution and the input pins. In the case of the 28BYJ-48 stepper motor, the order of the pins is
IN1
IN3
IN2
IN4
—it might be different for your motor.
Stepper myStepper(stepsPerRevolution, IN1, IN3, IN2, IN4);
In the
setup()
, set the stepper speed using the
setSpeed
method. The stepper speed is in rpm.
myStepper.setSpeed(5);
Initialize the Serial Monitor at a baud rate of 115200.
Serial.begin(115200);
In the
loop()
, we’ll rotate the stepper motor clockwise and counterclockwise. You can use the
step()
method on the
myStepper
object. Pass as an argument the number of steps you want to take. For a full rotation (revolution), you need 2048 steps (
stepsPerRevolution
variable).
myStepper.step(stepsPerRevolution);
To rotate the motor counterclockwise, you need to pass the number of steps with the minus “
–
” sign.
myStepper.step(-stepsPerRevolution);
Demonstration
Upload the code to your board. After uploading, the motor will make one clockwise rotation and a counterclockwise rotation over an

Technologies used

ESP32ESP8266ArduinoBLEServoL298NStepperArduino IDE

Hardware components

  • ESP32
  • ESP8266
  • Arduino
  • Servo
  • L298N
  • Motor Driver
  • Stepper Motor
  • Servo Motor

Software & tools

  • Arduino IDE

What you get

  • Working project / hardware kit, tested before delivery
  • Complete source code with explanation
  • Project demo and guidance for your viva
  • Project report, PPT and IEEE paper support (on request)
Delivery & Payment
Delivery takes a minimum of two weeks from the date your inquiry is confirmed. Payment is in two parts: • 50% advance to start the project • Remaining 50% before delivery Note: • Documentation and PPT are not included by default • If you need documentation/PPT, it can be arranged for extra time and cost We'll confirm the exact timeline and cost with you after reviewing your requirements.