How to Wirelessly Control a NEMA 17 Stepper Motor With an ESP32 and an A4988 Driver Board

by SuitableBat9 in Circuits > Remote Control

3253 Views, 5 Favorites, 0 Comments

How to Wirelessly Control a NEMA 17 Stepper Motor With an ESP32 and an A4988 Driver Board

image_2023-11-23_215712327.png

In this tutorial, you’ll learn how to control a NEMA 17 stepper motor with an ESP32 and an A4988 driver board.

Supplies

What You Will Need With Link

For the power supply i use this adafruit HUSB238 USB Type C Power Delivery Dummy Breakout. But you can use any 12v power supply but it has to be less than 1.2 amps or it will break the A4988 driver board

Installing Libraries

image_2023-11-23_221733618.png

For this you will need 1 libraries the AccelStepper to download this go to Tools>Manage Libraries or you can press Ctrl+Shift+I once you open library manager type in AccelStepper and download the one by Mike McCauley

Wiring Diagram

image_2023-11-23_221624155.png

You can use a capacitor but it is not required

Code

#include <WiFi.h>
#include <ESPAsyncWebServer.h>
#include <AccelStepper.h>


const char *ssid = "SSID-HERE";
const char *password = "PASSWORD-HERE";


AsyncWebServer server(80);


// Define stepper motor connections and steps per revolution
#define motorStep 14
#define motorDir 12
#define motorEnable 4
AccelStepper stepper(1, motorStep, motorDir);


void setup() {
  // Set up serial communication
  Serial.begin(115200);


  // Connect to Wi-Fi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }
  Serial.println("Connected to WiFi");


  // 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());


  // Initialize stepper motor
  stepper.setMaxSpeed(1000.0);
  stepper.setAcceleration(500.0);


  // Define web server routes
  server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send(200, "text/html", "<html><body><h1>Stepper Motor Control</h1><p><a href=\"/forward500\">Forward 500</a></p><p><a href=\"/backward500\">Backward 500</a></p><p><a href=\"/backward2000\">Backward 2000</a></p><p><a href=\"/forward2000\">Forward 2000</a></p></body></html>");
  });
  server.on("/forward500", HTTP_GET, [](AsyncWebServerRequest *request){
    stepper.move(500);
    stepper.runToPosition();
    request->send(200, "text/html", "<html><body><h1>Stepper Motor Control</h1><p><a href=\"/forward500\">Forward 500</a></p><p><a href=\"/backward500\">Backward 500</a></p><p><a href=\"/backward2000\">Backward 2000</a></p><p><a href=\"/forward2000\">Forward 2000</a></p></body></html>");
  });
  server.on("/backward500", HTTP_GET, [](AsyncWebServerRequest *request){
    stepper.move(-500);
    stepper.runToPosition();
    request->send(200, "text/html", "<html><body><h1>Stepper Motor Control</h1><p><a href=\"/forward500\">Forward 500</a></p><p><a href=\"/backward500\">Backward 500</a></p><p><a href=\"/backward2000\">Backward 2000</a></p><p><a href=\"/forward2000\">Forward 2000</a></p></body></html>");
  });
 server.on("/backward2000", HTTP_GET, [](AsyncWebServerRequest *request){
    stepper.move(-2000);
    stepper.runToPosition();
    request->send(200, "text/html", "<html><body><h1>Stepper Motor Control</h1><p><a href=\"/forward500\">Forward 500</a></p><p><a href=\"/backward500\">Backward 500</a></p><p><a href=\"/backward2000\">Backward 2000</a></p><p><a href=\"/forward2000\">Forward 2000</a></p></body></html>");
  });
 server.on("/forward2000", HTTP_GET, [](AsyncWebServerRequest *request){
    stepper.move(-2000);
    stepper.runToPosition();
    request->send(200, "text/html", "<html><body><h1>Stepper Motor Control</h1><p><a href=\"/forward500\">Forward 500</a></p><p><a href=\"/backward500\">Backward 500</a></p><p><a href=\"/backward2000\">Backward 2000</a></p><p><a href=\"/forward2000\">Forward 2000</a></p></body></html>");
  });


  // Start server
  server.begin();
}


void loop() {
  // Handle any necessary background tasks
  stepper.run();
}


How the Code Works

Network Credentials

Insert your network credentials on the following variables.

const char *ssid = "SSID-HERE";
const char *password = "PASSWORD-HERE";


Motor Speed

 stepper.setMaxSpeed(1000.0);
  stepper.setAcceleration(500.0);

You can set the max speed and acceleration by changing the number in this line

Web Server

By defualt i added 4 modes but you can add as many ad you want

 server.on("/forward2000", HTTP_GET, [](AsyncWebServerRequest *request){
    stepper.move(-2000);
    stepper.runToPosition();
    request->send(200, "text/html", "<html><body><h1>Stepper Motor Control</h1><p><a href=\"/forward500\">Forward 500</a></p><p><a href=\"/backward500\">Backward 500</a></p><p><a href=\"/backward2000\">Backward 2000</a></p><p><a href=\"/forward2000\">Forward 2000</a></p></body></html>");
  });

to change how far the motor moves change the stepper.move command

 stepper.move(-2000);


Uploading the Code

After inserting your network credentials upload the code to your board.

If you have an ESP32 you should select ESP32 in the ToolsBoard menu.

After that, select the COM port in Tools Port.

Finally, upload the code.


After successfully uploading the code to the board, open the Serial Monitor at a baud rate of 115200. Press the RST button so that it restarts the board and it starts running the code.

You will see a link go to that link in your browser and you will be able to control the NEMA 17 with an ESP32.

Help

If You Need Any Help Or Have Any Questions Post A Comment Below Or Join My Discord Server https://discord.gg/Cq7tSEfaC2