Two Nema17 are not turning correctly

Hi! I using two Nema17 with Arduino uno and a A4988 driver. One of the motors used to turn just in one direction but accidentally I turned it with my hand and now it is just vibrating and doesn't turn anymore. I think the problem is I unset the power, but I don't know. Also, I don't understand what is wrong with my code, because there's only Motor X that's not turning and I tried to change it but it's still not working.

I would appreciate any help.
Thank you!

#include <CNCShield.h>

#define NO_OF_STEPS               200
#define SLEEP_BETWEEN_STEPS_MS    5

CNCShield cnc_shield;
StepperMotor *motorX = cnc_shield.get_motor(0);
StepperMotor *motorY = cnc_shield.get_motor(1);

void setup()
{
  Serial.begin(9600);
  cnc_shield.begin();
  cnc_shield.enable();

  Serial.println("==== Simultaneous motor control ====");
  Serial.println(" For each motor, indicate: number of steps, direction (clockwise or counterclockwise), and speed (steps/sec).");
}

void loop()
{
  // Collecting information for motor X
  Serial.println("\n=== Motor X ===");
  int nbPasX = getSteps();
  int directionX = getDirection();
  int vitesseX = getSpeed();
  motorX->set_speed(vitesseX);
  motorX->set_dir(directionX);

  // Collecting information for motor Y
  Serial.println("\n=== Motor Y ===");
  int nbPasY = getSteps();
  int directionY = getDirection();
  int vitesseY = getSpeed();
  motorY->set_speed(vitesseY);
  motorY->set_dir(directionY);

  // Calculating the delay between steps for each motor
  int delayTimeX = 1000 / vitesseX;  // Delay for motor X
  int delayTimeY = 1000 / vitesseY;  // Delay for motor Y

  // Executing movements in parallel
  Serial.println("\nMotors moving simultaneously...");
  for (int i = 0; i < max(nbPasX, nbPasY); i++) {
    if (i < nbPasX) motorX->step();
    if (i < nbPasY) motorY->step();
    delay(min(delayTimeX, delayTimeY));
  }

  Serial.println("\nMovements completed.");
  delay(2000);  // Wait before restarting
}

// Function to get the number of steps
int getSteps() {
  Serial.print("Number of steps: ");
  while (Serial.available() == 0) {}
  String pasStr = Serial.readStringUntil('\n');
  return pasStr.toInt();
}

// Function to get the direction (H = clockwise, A = counterclockwise)
int getDirection() {
  Serial.print("Direction: (H = clockwise, A = counterclockwise) : ");
  while (Serial.available() == 0) {}
  String dirStr = Serial.readStringUntil('\n');
  dirStr.trim();
  return (dirStr == "A" || dirStr == "a") ? COUNTER : CLOCKWISE;
}

// Function to get the speed in steps per second
int getSpeed() {
  Serial.print("Speed (steps/second): ");
  while (Serial.available() == 0) {}
  String speedStr = Serial.readStringUntil('\n');
  return speedStr.toInt();
}

That makes two of us. Post an annotated schematic showing exactly how you have wired this. It could be a wiring problem, bad motor, bad code or any combination of the above. It is like a three legged stool, the motor, code, and wiring are the three legs. If anything is wrong with one or more the stool will not be level.

1 Like

You should test your hardware against a library example. Test one motor at a time. Then, expand the example code to include your two motors. Wherever you copied the code you posted got it wrong.

Note: there is a fork to this library by "alex" to add functions, so indicate which library you are using in your code (add a comment).

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