ESP32 - DRV8825 Stepper Driver - NEMA17 - BLE Problems

Hallo all,

I am busy with a project and using an ESP32 and controlling a NEMA17 stepper motor via a MIT App Inventor App and the AccelStepper library. I use BLE communication with the ESP32. I also use a DRV8825 stepper motor driver.

When try and use micro-stepping the stepper motor skips micro-steps. When I run micro-stepping without the app and BLE it works perfect?

I need some help please?

Without seeing your code (hint hint) I can only guess that your loop() function is not running fast enough, but that is a guess. Some sort of delay is slowing loop().

Read the forum guidelines to see how to properly post code.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

// Include the AccelStepper library:
#include <AccelStepper.h>
// Include the Bluetooth Serial library: 
#include "BluetoothSerial.h"
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif

//Create a Bluetooth Serial Object
BluetoothSerial SerialBT; 

// Define stepper motor connections and motor interface type. Motor interface type must be set to 1 when using a driver:
#define dirPin 18
#define stepPin 17
#define motorInterfaceType 1
#define MS0 XX         // Pin XX connected to MS0 pin
#define MS1 XX         // Pin XX connected to MS1 pin
#define MS2 XX         // Pin XX connected to MS2 pin
#define home_switch XXX //

const int relayPin = 15;

// Variables
int steps;
int ginPos = 4800;
int richPos = 2400;
int klipPos = -2400;
int homePos = 9600;
int colPos = -9600;
int pourRelay = 0; 

byte dataIn; // Variable for the 1 Byte Number coming from the App. 
int dataRec; // Variable in Integer from App.
int m = 0;

//Step Sizes
// MS0 MS1 MS2
// LOW HIGH LOW - 1/4 Step
// HIGH HIGH LOW - 1/8 Step
// LOW LOW HIGH - 1/16 Step
// HIGH HIGH HIGH - 1/32 Step


// Create a new instance of the AccelStepper class:
AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin);

void setup() {
  Serial.begin(9600);
  SerialBT.begin("ESP32_TekTender"); //Bluetooth device name
  //Serial.println("The device started, now you can pair it with bluetooth!");

  pinMode(relayPin, OUTPUT);  
  digitalWrite(relayPin, LOW);
  
  // Set the maximum speed and acceleration:
  stepper.setMaxSpeed(500);
  stepper.setAcceleration(100);

}


void loop() {

   if (SerialBT.available()) {
    //Serial.write(SerialBT.read());
    dataIn = SerialBT.read();     //Read data from the App via BLE
    dataRec = dataIn;             //Store the received data in a variable
    //Serial.print("Data Received: ");   
    //Serial.println(dataRec);
     if (dataRec == 51) {
        m = 51;
        stepper.setCurrentPosition(0);
          while(stepper.currentPosition() != 800)
            {
              stepper.setSpeed(500);
              stepper.runSpeed();
            }
            delay(1000);
            stepper.setCurrentPosition(0);
      }        
  
     if (dataRec == 52) {
        m = 52;
         if (m == 52) {
            stepper.moveTo(richPos);
            stepper.runToPosition();
            delayMicroseconds(20);
            m=0;
        }
      }
      
     if (dataRec == 53) {
        m = 53;
        stepper.setCurrentPosition(0);
          while(stepper.currentPosition() != 1600)
            {
              stepper.setSpeed(500);
              stepper.runSpeed();
            }
            delay(1000);
            stepper.setCurrentPosition(0);
      }
      
     if (dataRec == 54) {
        m = 54;

          stepper.setCurrentPosition(0);
          while(stepper.currentPosition() != -800)
            {
              stepper.setSpeed(-500);
              stepper.runSpeed();
            }
            delay(1000);
            stepper.setCurrentPosition(0);
      }

      
     if (dataRec == 55) {
        m = 55;
        if (m == 55) {
           digitalWrite(relayPin, HIGH);
           delay(2000);
           digitalWrite(relayPin, LOW);
           delay(2000);
           m=0;
      }
     }
     if (dataRec == 56) {
        m = 56;
         if (m == 56) {
            stepper.moveTo(colPos);
            stepper.runToPosition();
            m=0;
        }
      }
   }
}
  

Hi,

Please see my code above, it will be of great help if you can assist me in finding the problem. If I use the acceleration and deceleration function of the AccelStepper library with micro-stepping on the DRV8825 and BLE communication it misses or is jerky. I thought it might be something with the BLE communication in the loop().

No you are using classic Bluetooth with BluetoothSerial.h.

I'm not certain about what your issue is with the interaction between micro stepping, accelStepper, and Bluetooth Serial, but there are likely background operations between the processor and the bluetooth radio and you may have to see the multi core and RTOS features of the ESP32 to have the BT and Stepper running on different cores.

You can also try replace the polling of the receive

if (SerialBT.available()) {

with callback notification when a message is received, but again, I'm not certain this is your issue.

// Include the AccelStepper library:
#include <AccelStepper.h>
// Include the Bluetooth Serial library:
#include "BluetoothSerial.h"
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif

//Create a Bluetooth Serial Object
BluetoothSerial SerialBT;

// Define stepper motor connections and motor interface type. Motor interface type must be set to 1 when using a driver:
#define dirPin 18
#define stepPin 17
#define motorInterfaceType 1
#define MS0 XX         // Pin XX connected to MS0 pin
#define MS1 XX         // Pin XX connected to MS1 pin
#define MS2 XX         // Pin XX connected to MS2 pin
#define home_switch XXX //

const int relayPin = 15;

// Variables
int steps;
int ginPos = 4800;
int richPos = 2400;
int klipPos = -2400;
int homePos = 9600;
int colPos = -9600;
int pourRelay = 0;

byte dataIn; // Variable for the 1 Byte Number coming from the App.
int dataRec; // Variable in Integer from App.
int m = 0;

volatile boolean updateFlag = false;//new message received

//Step Sizes
// MS0 MS1 MS2
// LOW HIGH LOW - 1/4 Step
// HIGH HIGH LOW - 1/8 Step
// LOW LOW HIGH - 1/16 Step
// HIGH HIGH HIGH - 1/32 Step


// Create a new instance of the AccelStepper class:
AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin);

void setup() {
  Serial.begin(9600);
  SerialBT.begin("ESP32_TekTender"); //Bluetooth device name
  //Serial.println("The device started, now you can pair it with bluetooth!");

  pinMode(relayPin, OUTPUT);
  digitalWrite(relayPin, LOW);

  // Set the maximum speed and acceleration:
  stepper.setMaxSpeed(500);
  stepper.setAcceleration(100);

  SerialBT.register_callback(changeSettings);//Arduino IDE

}


void loop()
{

  //if (SerialBT.available()) {
  //Serial.write(SerialBT.read());
  //dataIn = SerialBT.read();     //Read data from the App via BLE
  //dataRec = dataIn;             //Store the received data in a variable
  //Serial.print("Data Received: ");
  //Serial.println(dataRec);
  if (updateFlag)
  {
    updateFlag = false;

    if (dataRec == 51) {
      m = 51;
      stepper.setCurrentPosition(0);
      while (stepper.currentPosition() != 800)
      {
        stepper.setSpeed(500);
        stepper.runSpeed();
      }
      delay(1000);
      stepper.setCurrentPosition(0);
    }

    if (dataRec == 52) {
      m = 52;
      if (m == 52) {
        stepper.moveTo(richPos);
        stepper.runToPosition();
        delayMicroseconds(20);
        m = 0;
      }
    }

    if (dataRec == 53) {
      m = 53;
      stepper.setCurrentPosition(0);
      while (stepper.currentPosition() != 1600)
      {
        stepper.setSpeed(500);
        stepper.runSpeed();
      }
      delay(1000);
      stepper.setCurrentPosition(0);
    }

    if (dataRec == 54) {
      m = 54;

      stepper.setCurrentPosition(0);
      while (stepper.currentPosition() != -800)
      {
        stepper.setSpeed(-500);
        stepper.runSpeed();
      }
      delay(1000);
      stepper.setCurrentPosition(0);
    }


    if (dataRec == 55) {
      m = 55;
      if (m == 55) {
        digitalWrite(relayPin, HIGH);
        delay(2000);
        digitalWrite(relayPin, LOW);
        delay(2000);
        m = 0;
      }
    }
    if (dataRec == 56) {
      m = 56;
      if (m == 56) {
        stepper.moveTo(colPos);
        stepper.runToPosition();
        m = 0;
      }
    }
  }
}


void changeSettings(esp_spp_cb_event_t event, esp_spp_cb_param_t *param) 
{
  if (event == ESP_SPP_DATA_IND_EVT) //received data
  {
    //dataIn = SerialBT.read();     //Read data from the App via BLE
    //dataRec = dataIn;
    dataRec = SerialBT.read();     //Read data from the App via BLE
    updateFlag = true;
  }
}

Hi, thank you for the advice. I've will try it tonight and provide feedback.

I will be trying the BLE Low Energy option soon as well.