Other Arduino Codes and Videos by Robojax

ESPSTP-2: Send Stepper Motor 28BYJ-48 over WiFi Mobile Phone to any Angle using ESP32

Download Arduino Sketch File

ESPSTP-2: Send Stepper Motor 28BYJ-48 over WiFi Mobile Phone to any Angle

This tutorial shows you how to control Stepper Motor with ULN2003 to send it to any any angle, direction and speed using ESP32 MCU over WiFi from desktop and from mobile phone. We can define unlimited number of angle push buttons and also custom angle. All the texts are customization so you can use them for other languages or other purpose.

Your Arduino IDE (the software) must be ready to work with ESP32. See instructions here.
Stepper Motor control WiFi Screen-1
Stepper Motor control WiFi Screen-1
Click on image to enlarge
Stepper Motor control WiFi Screen-2
Stepper Motor control WiFi Screen-2
Click on image to enlarge
Stepper Motor control WiFi Screen-3
Stepper Motor control WiFi Screen-3
Click on image to enlarge
Stepper Motor control WiFi Screen-4
Stepper Motor control WiFi Screen-4
Click on image to enlarge
Stepper Motor control WiFi Screen-5
Stepper Motor control WiFi Screen-5
Click on image to enlarge

Resources for this sketch

Code to Control 28BYJ-48 Stepper motor using ESP32 over WiFi


/*
 * Controlling 28BYJ-48 Stepper Motor over WiFi using ESP32   
 * sending it to any angle with predefined push buttons or typed angle
 * You will be able to set:
 * -Angle
 * -Speed
 * -Direction
 * 
 * Watch Video instrution for this code:https://youtu.be/DYuBUxHHSas
 * 
 * Full explanation of this code and wiring diagram is available at
 * my Arduino Course at Udemy.com here: http://robojax.com/L/?id=62

 * Written by Ahmad Shamshiri on Jun 13, 2020
 * in Ajax, Ontario, Canada. www.robojax.com
 * 
Related videos:
Introduction to using 28BYJ-48 stepper motor motor: https://youtu.be/Sl2mzXfTwCs
Contorl speed of this motor using Potentiometer: https://youtu.be/lrEW8xlSnuY
Control 28BYJ-48 Stepper motor with hand gesture: https://youtu.be/KfAnUgaSDmI
STPB-1 Control 28BYJ-48 Stepper motor with 3 push button for CW, CCW and STOP: https://youtu.be/q-H7GbyX5ho
STPB-2 Control 28BYJ-48 Stepper motor with 2 push button keep pressed: https://youtu.be/y-F-jjTOYyQ
STPB-3 ONE revolution CW CCW: https://youtu.be/MEEKvjj7vbY
STPB-4 Any (one) Angle push button: https://youtu.be/P_O-0_aOXuA
STPB-5 Any Angle (multiple) push buttons:  https://youtu.be/4LOpXFkWzD4
ESP8266 NodeMCU D1 Mini over WiFi: https://youtu.be/CzvGR0WTllI
ESP32 CW CCW Over WiFi : https://youtu.be/n2oeT6RcU5Q

 * Get this code and other Arduino codes from Robojax.com
Learn Arduino step by step in structured course with all material, wiring diagram and library
all in once place. Purchase My course on Udemy.com http://robojax.com/L/?id=62

If you found this tutorial helpful, please support me so I can continue creating 
content like this. You can support me on Patreon http://robojax.com/L/?id=63

or make donation using PayPal http://robojax.com/L/?id=64

 *  * This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.* 
 * This code has been download from Robojax.com
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.

   Copyright (c) 2015, Majenko Technologies
   All rights reserved.

   Redistribution and use in source and binary forms, with or without modification,
   are permitted provided that the following conditions are met:

 * * Redistributions of source code must retain the above copyright notice, this
     list of conditions and the following disclaimer.

 * * Redistributions in binary form must reproduce the above copyright notice, this
     list of conditions and the following disclaimer in the documentation and/or
     other materials provided with the distribution.

 * * Neither the name of Majenko Technologies nor the names of its
     contributors may be used to endorse or promote products derived from
     this software without specific prior written permission.

   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
   ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
   ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

const int CW =1;
const int CCW =2;
const int STOP =3;

int Pin1 = 13;//IN1 is connected to 13 
int Pin2 = 12;//IN2 is connected to 12  
int Pin3 = 14;//IN3 is connected to 14 
int Pin4 = 27;//IN4 is connected to 27 

long angles[] = {360, 270, 90, 20, 287};//angles of each push button
int directions[] ={CW, CCW, CW, CCW, CW};//direction of eacch push button
int speedFactors[] = {10, 1, 4, 10, 2};//speed for each push button
const String statusTitle[]={
            "Motor sent to ", 
            "Motor sent to ", 
            "Motor sent to ", 
            "Motor sent to ",
            "Motor sent to "};

const String directionTitle[]={"CW", "CCW"};
const String stopPushButtonTitle="STOP";
const String topTitle1 ="&deg; in ";
const String topTitle2 ="Speed factor: ";

const String topTitle3 ="Angle: ";
const String topTitle4 ="Direction: ";
const String topTitle5 =", Speed: ";
const String topTitle6 ="Send";
const String customMainTitle ="( C )Motor sent to ";

const int customValueButtonID=99;
const String motorStopped ="Motor Stopped";

int correction_CW = 150;//watch video for details
int correction_CCW = 150;//watch video for details


int poleStep = 0; 
long stepVale =0;
const int SPR=64*64;
long goToAngle=0;
int activeButton=0;
int speedValue =1;

int pole1[] ={0,0,0,0, 0,1,1,1, 0};//pole1, 8 step values
int pole2[] ={0,0,0,1, 1,1,0,0, 0};//pole2, 8 step values
int pole3[] ={0,1,1,1, 0,0,0,0, 0};//pole3, 8 step values
int pole4[] ={1,1,0,0, 0,0,0,1, 0};//pole4, 8 step values


int count=0;
int  dirStatus = STOP;// stores direction status 3= stop (do not change)

#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
#include <ESPmDNS.h>

const char *ssid = "Robojax";
const char *password = "YouTube2020";

WebServer server(80);

void handleRoot() {
  //Robojax.com ESP32 Stepper Push button Any Angle ESPSTP-2
 String HTML ="<!DOCTYPE html>\
  <html>\
  <head>\
  	
<title>Robojax ESP32 Stepper Motor</title>\
  	
<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\
  
<style>\
 
html,body{	
width:100%\;
height:100%\;
margin:0}
*{box-sizing:border-box}
.colorAll{
	background-color:#90ee90}
.colorBtn{
	background-color:#add8e6}
.angleButtdon,a{
	font-size:30px\;
border:1px solid #ccc\;
display:table-caption\;
padding:7px 10px\;
text-decoration:none\;
cursor:pointer\;
padding:5px 6px 7px 10px}a{
	display:block}
.btn{
	margin:5px\;
border:none\;
display:inline-block\;
vertical-align:middle\;
text-align:center\;
white-space:nowrap}
";
   
  HTML +="</style>

</head>

<body>
<h1>Robojax ESP32 Stepper ESPSTP-2  </h1>
";

    if(dirStatus !=STOP && activeButton !=customValueButtonID ){

          HTML +="
	<h2><span style=\"background-color: #FFFF00\">";   
          HTML +=statusTitle[activeButton];    
          HTML +=String(angles[activeButton]);  
          HTML +=topTitle1;                        
          HTML +=directionTitle[directions[activeButton]-1]; //subtract 1 to get proper title   
          HTML +="</span></h2>
";
          
          HTML +="
	<h2>";                                
          HTML +=topTitle2;          
          HTML +=String(speedFactors[activeButton]); 
          HTML +="<span style=\"background-color: #FFFF00\"> (";
          float  timeTakes =(float)speedFactors[activeButton] * float(angles[activeButton]) * ((float)4/(float)360);//calculat the time that takes to move to that angle 
          HTML +=String(timeTakes);                               
          HTML +=" seconds)";                     
          HTML +="</span></h2>
";            
 
   }if(activeButton ==customValueButtonID ){

          HTML +="
	<h2><span style=\"background-color: #FFFF00\">";   
          HTML +=customMainTitle;    
          HTML +=String(goToAngle);  
          HTML +=topTitle1;                        
          HTML +=directionTitle[dirStatus-1]; //subtract 1 to get proper title   
          HTML +="</span></h2>
";
          
          HTML +="
	<h2>";                                
          HTML +=topTitle2;          
          HTML +=String(speedValue); 
          HTML +="<span style=\"background-color: #FFFF00\"> (";
          float  timeTakes =(float)speedValue * float(goToAngle) * ((float)4/(float)360);//calculat the time that takes to move to that angle 
          HTML +=String(timeTakes);                               
          HTML +=" seconds)";                     
          HTML +="</span></h2>
";            
 
   }else if(dirStatus ==STOP)
   {
    HTML +="
	<h2><span style=\"background-color: #FFFF00\">"; 
    HTML +=motorStopped; 
    HTML +="</span></h2>
";          
   } 

        HTML +="	<div class=\"btn\">
		<a class=\"angleButton\" style=\"background-color:#";
        HTML +="f59898";//set the color of buttons if active or not
        HTML +="	\"  href=\"/stop";               
        HTML +="\">";      
        HTML +=stopPushButtonTitle;//tilte on button
       HTML +="</a>	
	</div>

";   
 for (byte i = 0; i < (sizeof(angles) / sizeof(angles[0])); i++) {

        HTML +="	<div class=\"btn\">
		<a class=\"angleButton\" style=\"background-color:#";
        if(i ==activeButton)
        {
          HTML +="f59898";//set the color of buttons if active or not
        }else{
          HTML +="90ee90";//set the color of buttons if active or not
        }

        HTML +="	\"  href=\"/go?";               
        HTML +="angle=";
        HTML +=angles[i];
        HTML +="\">";      
        HTML +=angles[i];//tilte on button

     HTML +="&deg; "; //degree symbol for HTML &deg;
     HTML +=directionTitle[directions[i]-1];
     HTML +="</a>	
	</div>

"; //degree symbol for HTML &deg;     
   }

  //custom angle
  HTML +="<form action=\"/go\" method=\"get\">
";  

  HTML +="<label for=\"angle\">
";
  HTML += topTitle3;  
  HTML +="</label> 
";  
  HTML +="<input type=\"text\" id=\"angle\" name=\"angle\" value=\"
";
  HTML +=String(goToAngle); 
  HTML +="\"  maxlength=\"4\" size=\"4\"> 
";    

  HTML +="<input type=\"radio\" id=\"cw\" 
";
  if( dirStatus  ==1){
    HTML +="checked=\"checked\"";
  }
  HTML +=" name=\"d\" value=\"1\">
";    
  HTML +="<label for=\"cw\">CW</label>
"; 
  
  HTML +="<input type=\"radio\" id=\"ccw\" 
";
  if( dirStatus  ==2){
    HTML +="checked=\"checked\"";
  }
  HTML +=" name=\"d\" value=\"2\">
";  
  HTML +="<label for=\"female\">CCW</label>
";

  HTML +="<label for=\"spd\">
";
  HTML +=topTitle5;
  HTML +="</label> 
";    
  HTML +="<input type=\"text\" id=\"spd\" name=\"s\" value=\"1\"  maxlength=\"2\" size=\"4\"><br>
";
  
  HTML +="<input type=\"submit\" value=\"
";  
  HTML +=topTitle6;
  HTML +="\">
";     
  HTML +="</form> 

"; 

  HTML +="	
</body>
</html>
";
  server.send(200, "text/html", HTML);  
  //Robojax.com ESP32 Stepper Push button Any Angle ESPSTP-2  
}//handleRoot()

void handleNotFound() {
  //Robojax.com ESP32 Stepper Push button Any Angle ESPSTP-2
  String message = "File Not Found

";
  message += "URI: ";
  message += server.uri();
  message += "
Method: ";
  message += (server.method() == HTTP_GET) ? "GET" : "POST";
  message += "
Arguments: ";
  message += server.args();
  message += "
";

  for (uint8_t i = 0; i < server.args(); i++) {
    message += " " + server.argName(i) + ": " + server.arg(i) + "
";
  }

  server.send(404, "text/plain", message);
  //Robojax.com ESP32 Stepper Push button Any Angle ESPSTP-2
}//end of handleNotFound()

void setup(void) {
  //Robojax.com 28BYJ-48 Stepper Motor Control
 
 pinMode(Pin1, OUTPUT);//define pin for ULN2003 in1 
 pinMode(Pin2, OUTPUT);//define pin for ULN2003 in2   
 pinMode(Pin3, OUTPUT);//define pin for ULN2003 in3   
 pinMode(Pin4, OUTPUT);//define pin for ULN2003 in4    

 
  Serial.begin(115200);//initialize the serial monitor
  Serial.println("Robojax 28BYJ-48 Stepper Motor Control");

  //Robojax.com ESP32 Stepper Push button Any Angle ESPSTP-2 
    
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  Serial.println("");
  
  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: http://");
  Serial.println(WiFi.localIP());

//multicast DNS   //Robojax.com 28BYJ-48 Steper Motor Control
  if (MDNS.begin("robojaxESP32")) {
    Serial.println("MDNS responder started");
    Serial.println("access via http://robojaxESP32");
  }

  server.on("/", handleRoot);
  server.on("/go", HTTP_GET, motorControl); 
  server.on("/stop",HTTP_GET, stopMotor); 
         
  server.onNotFound(handleNotFound);
  server.begin();
  Serial.println("HTTP server started");

}//end of setup

void loop(void) {
  //Robojax.com ESP32 Stepper Push button Any Angle ESPSTP-2
  server.handleClient();

    stepVale = (SPR * goToAngle)/360 ;
  
  
 if(poleStep>7){ 
   poleStep=0;
 } 
 if(poleStep<0){ 
   poleStep=7; 
 } 

        
 if(dirStatus ==CCW){ 
  
   poleStep++; 
   count++;   
   if(count+correction_CCW <= stepVale)
   {
    driveStepper(poleStep);      
   }else{
      driveStepper(8);  
   }
  //full explannation at Arduino Course on Udemy.com see link above
 }else if(dirStatus ==CW){ 
   poleStep--; 
   count++;   
   if(count+correction_CW <=stepVale)
   {
    driveStepper(poleStep);      
   }else{
      driveStepper(8);  
   }   
 }else{
  driveStepper(8);    
 }


  delay(speedValue);
  
   //Robojax.com ESP32 Stepper Push button Any Angle ESPSTP-2
}//end of loop

/*
 * motorControl()
 * updates the motor parameters upon receiving request from web interface
 * returns nothing
 * written by Ahmad Shamshiri
 * on Wednesday Jun 13, 2020 in Ajax, Ontario, Canada
 * www.robojax.com
 */
void motorControl() {
  //Robojax.com ESP32 Stepper Push button Any Angle ESPSTP-2
//Watch details at my Arduino Course at Udemy.com
   //see this https://www.arduino.cc/reference/en/language/variables/utilities/sizeof/
 for (byte i = 0; i < (sizeof(angles) / sizeof(angles[0])); i++) {
   if(server.arg("angle") == String(angles[i]) )
   {
      goToAngle =angles[i];
      dirStatus =directions[i];
      count =0;
      activeButton =i;   
      speedValue=speedFactors[i];
   }//if
 }//for
//Watch details at my Arduino Course at Udemy.com
   if(server.arg("d").toInt() >0 )
   {
      goToAngle =server.arg("angle").toInt();
      dirStatus =server.arg("d").toInt();
      count =0;
      activeButton =customValueButtonID;   
      speedValue=server.arg("s").toInt();    
   }

  handleRoot();
  //Robojax.com ESP32 Stepper Push button Any Angle ESPSTP-2 code  
}//motorControl end



/*
 * @brief sends signal to the motor
 * @param "c" is integer representing the pol of motor
 * @return does not return anything
 * 
 * www.Robojax.com code June 2019
 */
void driveStepper(int c)
{
  //Watch details at my Arduino Course at Udemy.com
     digitalWrite(Pin1, pole1[c]);  
     digitalWrite(Pin2, pole2[c]); 
     digitalWrite(Pin3, pole3[c]); 
     digitalWrite(Pin4, pole4[c]);   
  //Robojax.com ESP32 Stepper Push button Any Angle ESPSTP-2 code     
}//driveStepper end here

/*
 * @brief stops the motor immediately 
 * @param none
 * @return does not return anything
 * written by Ahmad Shamshiri
 * www.Robojax.com code June 13, 2020 in Ajax, Ontario, Canada
 */
void stopMotor()
{
  //Robojax.com ESP32 Stepper Push button Any Angle ESPSTP-2 code
  //Watch details at my Arduino Course at Udemy.com
  dirStatus=STOP;
  handleRoot();
}//stopMotor()


   
Download Arduino Sketch File

If you found this tutorial helpful, please support me so I can continue creating content like this. support me via PayPal