Control Stepper Motor 28BYJ-48 Using TTGO T-Display ESP32

Control stepper motor with ESP32 and Arduino.

Introduction

A stepper motor is an electric motor that rotates in discrete step increments. The movement of each step is precise and repeatable; therefore, the motor’s position can be controlled precisely without any feedback mechanism. Nowadays, it is being used widely in 3D printers, CNC machine or laser engraving (like Snapmaker). In this tutorial, I will share the basic steps to control stepper motor movement using ESP32 board.

You can read following links to know more how stepper motor works:

Video

This video shows how to control stepper motor model 28BYJ-48 using ESP32 board.

Hardware Preparation

This is the list of items used in the video.

Sample Program

This sample program will allow user to control stepper movement through button 1 and 2 (on TTGO T-Display ESP32 board). You need to install following library on Library Manager:

  • TFT_eSPI by Bodmer Version 1.4.20
/*
Tutorial: Control 28BYJ-48 Stepper Motor Using TTGO T-Display ESP32
Board:
– TTGO T-Display ESP32
https://my.cytron.io/p-ttgo-t-display-esp32-1.14-display-module
Output:
– 12V 28BYJ-48 Stepper Motor
https://my.cytron.io/c-motor-and-motor-driver/c-dc-motor/c-stepper-motor/p-12v-28byj-48-stepper-motor-plus-uln2003-driver-board
Connection TTGO | Stepper Driver
GND | GND
27 | IN1
26 | IN2
25 | IN3
33 | IN4
Ext libraries:
– TFT_eSPI by Bodmer Version 1.4.16
*/
#include <SPI.h>
#include <TFT_eSPI.h>
TFT_eSPI tft = TFT_eSPI(); // Resolution 135 x 240
#define FF17 &FreeSans9pt7b
#define FF21 &FreeSansBold9pt7b
#define ROW1 0,16
#define ROW2 0,38
#define ROW3 0,60
#define ROW4 0,82
#define ROW5 0,104
#define ROW6 0,126
#define BUTTON1 35
#define BUTTON2 0
#define STEPPER_IN1 27
#define STEPPER_IN2 26
#define STEPPER_IN3 25
#define STEPPER_IN4 33
#define REVOLUTION_STEP 2048 // 1 revolution
boolean stepperDirection = false;
int stepperStep = 0;
int stepperStepCount = 0;
boolean stepperMove = false;
long prevMillisStepper = 0;
int intervalStepper = 2; // Minimum is 2, increase for lower speed
boolean button1Pressed = false;
boolean button2Pressed = false;
void setup()
{
pinMode(BUTTON1, INPUT_PULLUP);
pinMode(BUTTON2, INPUT_PULLUP);
pinMode(STEPPER_IN1, OUTPUT);
pinMode(STEPPER_IN2, OUTPUT);
pinMode(STEPPER_IN3, OUTPUT);
pinMode(STEPPER_IN4, OUTPUT);
tft.init();
tft.setRotation(1);
tft.fillScreen(TFT_BLACK);
tft.setFreeFont(FF21);
tft.setTextColor(TFT_BLUE);
tft.setCursor(ROW1);
tft.print("BUTTON1");
tft.setCursor(ROW5);
tft.print("BUTTON2");
tft.setFreeFont(FF17);
tft.setTextColor(TFT_RED);
tft.setCursor(ROW2);
tft.print("Clockwise");
tft.setCursor(ROW6);
tft.print("Counter Clockwise");
}
void loop()
{
if (digitalRead(BUTTON1) == LOW &&
button1Pressed == false) {
button1Pressed = true;
stepperDirection = false;
stepperMove = true;
stepperStepCount = 0;
stepperStep = 1;
}
else if (digitalRead(BUTTON1) == HIGH &&
button1Pressed == true) {
button1Pressed = false;
}
if (digitalRead(BUTTON2) == LOW) {
stepperDirection = true;
stepperMove = true;
stepperStepCount = 0;
stepperStep = 1;
}
else if (digitalRead(BUTTON2) == HIGH &&
button2Pressed == true) {
button2Pressed = false;
}
if (millis() – prevMillisStepper > intervalStepper) {
if (stepperMove == true) {
if (stepperDirection) {
if (stepperStep++ >= 3) {
stepperStep = 0;
}
}
else {
if (stepperStep– == 0) {
stepperStep = 3;
}
}
if (stepperStepCount++ == REVOLUTION_STEP) {
stepperMove = false;
stepperStep = 4;
}
switch (stepperStep) {
case 0:
digitalWrite(STEPPER_IN1, HIGH);
digitalWrite(STEPPER_IN2, LOW);
digitalWrite(STEPPER_IN3, LOW);
digitalWrite(STEPPER_IN4, LOW);
break;
case 1:
digitalWrite(STEPPER_IN1, LOW);
digitalWrite(STEPPER_IN2, HIGH);
digitalWrite(STEPPER_IN3, LOW);
digitalWrite(STEPPER_IN4, LOW);
break;
case 2:
digitalWrite(STEPPER_IN1, LOW);
digitalWrite(STEPPER_IN2, LOW);
digitalWrite(STEPPER_IN3, HIGH);
digitalWrite(STEPPER_IN4, LOW);
break;
case 3:
digitalWrite(STEPPER_IN1, LOW);
digitalWrite(STEPPER_IN2, LOW);
digitalWrite(STEPPER_IN3, LOW);
digitalWrite(STEPPER_IN4, HIGH);
break;
default:
digitalWrite(STEPPER_IN1, LOW);
digitalWrite(STEPPER_IN2, LOW);
digitalWrite(STEPPER_IN3, LOW);
digitalWrite(STEPPER_IN4, LOW);
break;
}
}
prevMillisStepper = millis();
}
}

Thank You

References:

Thanks for reading this tutorial. If you have any technical inquiries, please post at Cytron Technical Forum.

2 thoughts on “Control Stepper Motor 28BYJ-48 Using TTGO T-Display ESP32”

  1. This code is for inbuilt button in ESP32 if I change pin no. from
    #define BUTTON1 35
    #define BUTTON2 0
    to
    #define BUTTON1 34
    #define BUTTON2 35
    it is not working. How could I resolve it, And how could I add more rotations in it ?

Leave a Comment

Your email address will not be published.

Share this Tutorial

Share on facebook
Share on whatsapp
Share on email
Share on print
Share on twitter
Share on pinterest
Share on facebook
Share on whatsapp
Share on email
Share on print
Share on twitter
Share on pinterest

Latest Tutorial

micro:bit Quick Start Kit
Design 3D Model Using TinkerCAD and Print
3D Print Lithophane for Hari Raya Aidilfitri
With Raspberry Pi Imager, you can write Raspberry Pi OS and remote SSH easier
3D Printing Pelita Raya
Tutorials of Cytron Technologies Scroll to Top