Stepper Motor

In this tutorial, we are going to learn:

  • How to control a stepper motor using ESP32 and ULN2003 driver

Video Tutorial

you can watch this video tutorial

Hardware Required

1×ESP-WROOM-32 Dev Module
1×Micro USB Cable
1×Stepper Motor
1×ULN2003 driver
n×Jumper Wires

About 28BYJ-48 Stepper Motor

According to the data sheet, when the 28BYJ-48 motor runs in full-step mode, each step corresponds to a rotation of 11.25°. That means there are 32 steps per revolution (360°/11.25° = 32).

In addition, the motor has a 1/64 reduction gear set. It means it that it actually has 32 x 64 = 2048 steps. Each step is equivalent to 360°/2048 = 0.1758°.

Conclusion: if motor do 2048 steps (in full-step mode), the motor rotate one revolution

Pinout

28BYJ-48 stepper motor includes 5 pins. We do not need to care detail about these pins. We just need to plug it to the connector of ULN2003 motor driver.

ESP32 Button LED Wiring Diagram

About ULN2003 Stepper Motor Driver Module

The ULN2003 is one of the most common motor driver Module for stepper motor.

  • The module has four LEDs that show activity of four control input lines (to indicate stepping state). They provide a splendid effect when stepping
  • The module also comes with an ON/OFF jumper to isolate power to the stepper motor.

Pinout

ESP32 Button LED Wiring Diagram

ULN2003 module includes 6 pins and one female connector:

  • IN1 pin: is used to drive the motor. Connect it to an output pin on Arduino.
  • IN2 pin: is used to drive the motor. Connect it to an output pin on Arduino.
  • IN3 pin: is used to drive the motor. Connect it to an output pin on Arduino.
  • IN4 pin: is used to drive the motor. Connect it to an output pin on Arduino.
  • GND pin: is a common ground pin. It MUST connect to both GNDs of Arduino and the external power supply.
  • VDD pin: supplies power for the motor. Connect it to the external power supply.
  • Motor Connector: this is where the motor plugs into.

※ NOTE THAT:

  • The voltage of the external power supply should be equal to the voltage of stepper motor. For example, if a stepper motor works with 12V DC, we need to use a 12V power supply. In case of 28BYJ-48 stepper motor, it works with 5V DC, we will use 5V power supply.
  • Even if a stepper motor requires 5V power supply, Please do NOT connect VDD pin to the 5V pin on ESP32. Instead, connect it to an external 5V power supply. That is because the stepper motor draws too much power.

How To Program For Stepper Motor

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.

  • 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;
  • 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);
  • 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);

Wiring Diagram

ESP32 Button LED Wiring Diagram

Image is developed using Fritzing. Click to enlarge image

click to see ESP32 pin out

ESP32 Code

Copy

#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);
}          

Quick Steps

  • power up your board
  • Open Arduino IDE
  • Select the right board
  • Select the right port
  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to ESP32
  • Arduino IDE Upload Code
  • Press and keep pressing the button several seconds
  • See the changes you made

Book Tutorial

We are considering to make the book tutorials. If you think the book tutorials are essential, you can download it. download book

References

※ NOTE THAT:

Some components works on 3.3v and others works on 5v!