i2c stops working altogether if I spin a stepper more than a few times

I am making a dosing station right now using an esp12e board, arduino nano and a drv8825. During development I just had the stepper spin one full rotation. Got it working just fine. ESP recieves a MQTT payload from local server and decides to tell the arduino over i2c to turn the stepper or not. I did this to free up the ESP because the watchdog on ESP would reset it if it spun the motor too long since its a blocking function. Again, worked fine. ESP recieves a message aprox every minute and tells arduino to spin.

Now that I have the arduino spinning the stepper 25 rotations, i2c fails after one instruction and now that it spins longer i have noticed there are a lot of timing issues in serial output. Here is the code and serial output. I trimmed it a lot so it would be easier to read and it still is doing some funky stuff.

#include <Wire.h>

#define dirPin1 A2
#define stepPin1 A0

void setup()
{
  Serial.begin(115200);
  while (!Serial){};
  Serial.println("i am an i2c slave stepper motor controller");
  
  Wire.begin(99);
  Wire.onReceive(recieveEvent);
  Wire.onRequest(requestEvent);

  pinMode(stepPin1, OUTPUT);
  pinMode(dirPin1, OUTPUT);
 
  digitalWrite(dirPin1, LOW);

}

int counter=1;
void loop()
{
  delay(50);
  Serial.print(".");
  if (counter%29 == 0)
  {
    Serial.println("");
    counter=1;
  }
  counter = counter +1;
}


void recieveEvent(int howMany)
{
  char incomingCommand[8];
  int i = 0;    
  while (0 < Wire.available())
  {
    char c = Wire.read();
    incomingCommand[i] = c;
    i +=1;
  }
  Serial.print("Incoming Message: ");
  for (int i=0; i<sizeof(incomingCommand); i++)
    Serial.print(incomingCommand[i]);
  
  if (incomingCommand[0] == 'd')
  {
    Serial.println("turning motor on");
    Serial.print("dispensing 5 mL of fluid");
    //25 rotations is aprox 5mL
    delay(5000);
    for (int i = 0; i < 25*200; i++)
    {
      digitalWrite(stepPin1, HIGH);
      delayMicroseconds(1000);
      digitalWrite(stepPin1, LOW);
      delayMicroseconds(1000);
    }
  }
  Serial.println("exiting of callback");
}

void requestEvent()
{
  Wire.write("ok");
}

Serial output

i am an i2c slave stepper motor controller
.............................
............................
............................
...........Incom

The motor spins at this point pausing the serial output and once done continues

ing Message: d,25|Pturning motor on
dispensing 5 mL of fluidexiting of callback
.................
............................
............................
............................
............................
............................
............................
............................
............................
............................
............................
............................

and it spits out my '.'s forever and ever after never getting a single i2c message again.

Serial output on my ESP says it is still submitting commands but arduino isnt having any of it.

What is causing the wierd serial output? I have Serial.prints before i spin the motor and even put a delay before the motor but serial lags until after motor is done. Its very confusing.

Wire.begin(99); ?

This says that the Arduino is a slave device at address 99.

Yes sir. ESP12E board is the master. It sends a message to arduino nano at address 99 to spin the motor or not.

No use of pull ups on the i2c lines. I was told they were not necessary as long as the arduino nano is a slave device.

just ask the motor to turn one revolution and it is fine and works for at least a couple hours. Ask it to turn 2 revolutions and it never receives another message. I have not attempted between 1 and 2 revolutions.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.