I am using an Arduino and a Pi to control two stepper motors connected to DRV8825 drivers. The Pi communicates with the Arduino through serial, telling the Arduino which motor to move and how far. In the code, my motors are called A and B(the driver connected to pins 2 and 3 is A and the driver connected to pins 4 and 5 is B).
I believe my issue is both a code and hardware problem. Whichever motor is designated as B moves very little, and the driver gets really hot. When the driver on the far left is motor A, there are no issues with it, but when the driver in the middle is motor A, it only works well for a short time before it starts to stall, and it's always hot. Because of this, I believe there is an issue with my code that makes whichever motor designated as B stall, but the driver in the middle has low performance regardless.
I want to eventually create a circuit with up to 10 drivers, so I was hoping that I could get some insight into what I'm doing wrong so that every motor gets close to equal performance. Thank you!
Here is my code:
#include <AccelStepper.h>
#define dirPin 2
#define stepPin 3
#define dirPin2 4
#define stepPin2 5
#define motorInterfaceType 1
int SPEED=200;
AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin);
AccelStepper stepper2 = AccelStepper(motorInterfaceType, stepPin2, dirPin2);
//Steps the motor
// Stepper object, number of steps, previous position of stepper motor
void stepMotor(AccelStepper myMotor, String motorSteps, long int previousPosition){
while (myMotor.currentPosition() != previousPosition+motorSteps.toInt()){
if (motorSteps.toInt()<0){
myMotor.setSpeed(-SPEED);
} else {
myMotor.setSpeed(SPEED);
}
myMotor.runSpeed();
}
}
void setup()
{
//Open serial communication
Serial.begin(9600);
// Setting the positions and max speeds of the two steppers
stepper.setCurrentPosition(0);
stepper.setMaxSpeed(1000);
stepper2.setCurrentPosition(0);
stepper.setMaxSpeed(1000);
}
void loop() {
long int prevPos=stepper.currentPosition();
long int prevPos2=stepper2.currentPosition();
String motor="";
//Stalls the program until new serial data comes in
while (Serial.available()<=0){}
if (Serial.available() > 0) {
motor = Serial.readStringUntil('\n');
Serial.println(motor);
if (motor=="A" || motor=="B") {
Serial.println("Motor received.");
}
}
//Motor end
//Stalls the program until new serial data comes in
while (Serial.available()<=0){}
if (Serial.available() > 0) {
String steps = Serial.readStringUntil('\n');
Serial.println(steps);
if(motor=="A"){
stepMotor(stepper, steps, prevPos);
} else if (motor=="B"){
stepMotor(stepper2, steps, prevPos2);
}
}
//Steps end
Serial.println("Finished!");
}