Stepper Motor

In this tutorial, we are going to learn:

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

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
2×Buttons
n×Jumper Wires

About This Project

In this project we are going to control our stepper using two buttons each button will move the stepper in some direction for distance that entered in the serial monitor

About stepper motor

Please review our tutorial about stepper motor here.

About Buttons

Please review our tutorial about Buttons here.

About Serial-IO

Please review our tutorial about Arduino IDE serial monitor here.

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 = 1024;  // change this to fit the number of steps per revolution
int command;
int distance;
// 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() {

  pinMode(16,INPUT_PULLUP);
  pinMode(4,INPUT_PULLUP);
  
  // set the speed at 5 rpm
  myStepper.setSpeed(30);
  // initialize the serial port
  Serial.begin(115200);
}

void loop() {

  if(Serial.available()) // if there is data comming
  {
    Serial.println("Please enter number between 1 to 9");
    command = Serial.read()-48; 
    distance = command*100;
    delay(100);
  }


  if(digitalRead(16)==HIGH)
  {
    Serial.println("clockwise button pressed");
    myStepper.step(distance);
    delay(1000);
  }


  
  if(digitalRead(4)==LOW)
  {
    Serial.println("counterclockwise button pressed");
    myStepper.step(-distance);
    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!