ESP32 - OTA - Stepper motor+Button (distance Update)
This tutorial instructs you how to use OTA with ESP32 to make a project that enables you to change the code over the air
Video Tutorial
you can watch this video tutorial
Hardware Required
1 | × | ESP-WROOM-32 Dev Module | |
1 | × | Micro USB Cable | |
1 | × | Button | |
1 | × | Stepper motor | |
1 | × | Breadboard | |
n | × | Jumper Wires |
About This project
We will make an OTA project that include Stepper motor and Button
Review Topics
Please review this topics to get a better understand of this project
Wiring Diagram
stepper Connections
ESP32 Code
// Include required libraries #include <WiFi.h> #include <ESPmDNS.h> #include <WiFiUdp.h> #include <ArduinoOTA.h> #include <Stepper.h> const int stepsPerRevolution = 4096; // 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); const char* ssid = "WE_87B9D0"; const char* password = "jae05161"; void setup() { // set the speed at 5 rpm myStepper.setSpeed(5); pinMode(4,INPUT_PULLUP); Serial.begin(115200); Serial.println("Booting"); WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); while (WiFi.waitForConnectResult() != WL_CONNECTED) { Serial.println("Connection Failed! Rebooting..."); delay(5000); ESP.restart(); } //Hostname defaults to esp3232-[MAC] ArduinoOTA.setHostname("myesp32"); ArduinoOTA .onStart([]() { String type; if (ArduinoOTA.getCommand() == U_FLASH) type = "sketch"; else // U_SPIFFS type = "filesystem"; // NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end() Serial.println("Start updating " + type); }) .onEnd([]() { Serial.println("\nEnd"); }) .onProgress([](unsigned int progress, unsigned int total) { Serial.printf("Progress: %u%%\r", (progress / (total / 100))); }) .onError([](ota_error_t error) { Serial.printf("Error[%u]: ", error); if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed"); else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed"); else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed"); else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed"); else if (error == OTA_END_ERROR) Serial.println("End Failed"); }); ArduinoOTA.begin(); Serial.println("Ready"); Serial.print("IP address: "); Serial.println(WiFi.localIP()); } void loop() { ArduinoOTA.handle(); if(digitalRead(4)==LOW) { // step one revolution in one direction: Serial.println("clockwise"); myStepper.step(stepsPerRevolution); delay(100); } }
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
- 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
See Also
References
※ NOTE THAT:
Some components works on 3.3v and others works on 5v!