ESP32-Stepper-Motor-Control

This repo is about controling a stepper motor using DRV8825 stepper motor drivers using an ESP32 which is connected over Bluetooth. This project will be an addition to Project Silent Willow since I am attempting to control Project Silent Willow with one board instead of multiple boards.

A description of the code

Below is a line-by-line description of the code used (It might not get updated as often as the code gets updated)

/*
324Hz
win_21H2
Controling stepper motors with DRV8825 drivers with an ESP32 using Bluetooth (which I just found out is a thing on the ESP32)
Please note that this code does not work yet!
*/

#include "BluetoothSerial.h" // includes the BluetoothSerial library which allows the ESP32 board to communicate and send stuff using the serial monitor/connection
#include <AccelStepper.h> // includes the AccelStepper library which is used for controling the stepper motors
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif
BluetoothSerial SerialBT;

const int dirPin = 32; // sets the first stepper motors dir pin 
const int stepPin = 33; // sets the first stepper motors step pin 

const int stepPin1 = 25; // sets the second stepper motors step pin 
const int dirPin1 = 26; // sets the second stepper motors dir pin 

const int stepPin2 = 27; // sets the third stepper motors step pin 
const int dirPin2 = 14; // sets the third stepper motors dir pin 

const int stepPin3 = 12; // sets the fourth stepper motors step pin 
const int dirPin3 = 13; // sets the fourth stepper motors dir pin 

#define motorInterfaceType 1 // always use the value 1 when you work with drivers

String message = "";
char incomingChar; // sets the incomingChar variable as a character

unsigned long previousMillis = 0;  
const long interval = 10000;

AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin); // sets the different pins and hooks them up with a variable in this case it is "stepper"
AccelStepper stepper1 = AccelStepper(motorInterfaceType, stepPin1, dirPin1); // sets the different pins and hooks them up with a variable in this case it is "stepper1"
AccelStepper stepper2 = AccelStepper(motorInterfaceType, stepPin2, dirPin2); // sets the different pins and hooks them up with a variable in this case it is "stepper2"
AccelStepper stepper3 = AccelStepper(motorInterfaceType, stepPin3, dirPin3); // sets the different pins and hooks them up with a variable in this case it is "stepper3"

void setup() {
  stepper.setMaxSpeed(1000); // sets the max speed of stepper 0
  stepper1.setMaxSpeed(1000); // sets the max speed of stepper 1
  stepper2.setMaxSpeed(1000); // sets the max speed of stepper 2
  stepper3.setMaxSpeed(1000); // sets the max speed of stepper 3
  Serial.begin(115200); // begins the communications/serial baud rate at 115200
  SerialBT.begin("ESP32");
}

void loop() {
  if (SerialBT.available()){ // checks if there is a Bluetooth connection
    char incomingChar = SerialBT.read(); 
    if (incomingChar != '\n'){
      message += String(incomingChar);
    }
    else{
      message = "";
    }
    Serial.write(incomingChar); // writes to the serial monitor, whatever the computer gets as characters
  }

  if (message =="stepperon"){ // checks if the message is the same as "stepperon" and if it is, runs the code below
    stepper.setSpeed(400); // sets the stepper motor steps to 400
    stepper1.setSpeed(400); // sets the stepper motor steps to 400
    stepper2.setSpeed(400); // sets the stepper motor steps to 400
    stepper3.setSpeed(400); // sets the stepper motor steps to 400
    stepper.runSpeed(); // runs the speed which was set
    stepper1.runSpeed(); // runs the speed which was set
    stepper2.runSpeed(); // runs the speed which was set
    stepper3.runSpeed(); // runs the speed which was set
  } 
  
  else if (message =="stepperoff"){  // checks if the message is the same as "stepperoff" and if it is, runs the code below
    stepper.setSpeed(0); // sets the stepper motor steps to 0
    stepper1.setSpeed(0); // sets the stepper motor steps to 0
    stepper2.setSpeed(0); // sets the stepper motor steps to 0
    stepper3.setSpeed(0); // sets the stepper motor steps to 0
    stepper.runSpeed();  // runs the speed which was set
    stepper1.runSpeed(); // runs the speed which was set
    stepper2.runSpeed(); // runs the speed which was set
    stepper3.runSpeed(); // runs the speed which was set
  } 
  delay(20); // leaves a delay so it does not instantly update after the last update
}

Note that I am still developing the code so this repo will not be that detailed until I get more code and more descriptions. That is also the reason why there are not that many comments on different lines.

Here are some pictures of the setup/wiring

Make sure that you have the stepper motors connected to the correct pins on the DRV8825 because some stepper motors have different wiring setups and this configuration might not work

image

image

GitHub

View Github