ESP32 and A4988 - Serial monitor garbage

88 Views Asked by At

I have simplified my project as much as possible to narrow down the problem I have.

I am running an ESP32 (Espressif ESP32 WLAN Dev Kit Board Development) with an A4988 stepper motor driver. I have connected the A4988 to the ESP32 via GND and VIN (5V). GND + 3.3V had the same effect.

I also bought a second ESP32 for testing to rule out the possibility of a hardware defect.

My code in Arduino IDE:

    #include <Wire.h>
    
    // General variables
    const int DEVICE_TARGETQUANTITY = 200;
    
    // Stepper motor variables
    const int DIR = 17;
    const int STEP = 19;
    const int EN = 18;
    const int HIGHSPEED = 100;
    const int LOWSPEED = 500;
    int stepperSpeed;
    
    
    // Tasks
    TaskHandle_t Stepper;
    
    void setup()
    {
      Serial.begin(115200);
    
      // Pins for stepper motor
      pinMode(STEP, OUTPUT);
      pinMode(DIR, OUTPUT);
      pinMode(EN, OUTPUT);
    
      xTaskCreatePinnedToCore(
            StepperCode, /* Function to implement the task */
            "Stepper", /* Name of the task */
            10000,  /* Stack size in words */
            NULL,  /* Task input parameter */
            0,  /* Priority of the task */
            &Stepper,  /* Task handle. */
            tskNO_AFFINITY); /* Core where the task should run */
    }
    
    void loop()
    {
    
    }
    
    void StepperCode(void * parameter) { // Moves the stepper motor
      for(;;) {
        if (counterNumber < DEVICE_TARGETQUANTITY) {
          stepperSpeed = (DEVICE_TARGETQUANTITY - counterNumber > 2) ? HIGHSPEED : LOWSPEED;
    
          digitalWrite(EN, LOW);
          digitalWrite(DIR, LOW);
    
          digitalWrite(STEP, HIGH);
          delayMicroseconds(150);
          digitalWrite(STEP, LOW);
          delayMicroseconds(stepperSpeed);
        }
      }
    }

My problem is that after a random time (5s - 2 min) the stepper motor stops and the serial monitor outputs endless garbage: enter image description here

The same problem appears at a baudrate of 9600.

Does anyone have any ideas what else I could try to get closer to the cause?

Update 21.02.2024: I'm using breakout boards for the stepper driver. I observed, that by changing the breakout board (using the same type) the problem gets worse. So I bought a completly different type of breakout board - but the problem still keeps the same.

0

There are 0 best solutions below