Stepper motor not holding position

Hello everyone, I am new to arduino programming. My project includes 3 stepper motor , A4988 motor driver , CNC shield and arduino uno. I have able to achieve the desired steps of motor required for my application but then the motor is not holding position at all. Any help / guidance would be appreciated.

    pinMode(MOTOR_X_ENABLE_PIN, OUTPUT);
    motorX.setEnablePin(MOTOR_X_ENABLE_PIN);
    motorX.setPinsInverted(false, false, false); // Remove inversion if not needed
    motorX.setAcceleration(1000); // Adjust acceleration value
    motorX.setMaxSpeed(100); // Adjust max speed value
    motorX.setSpeed(50); // Adjust speed value
    motorX.enableOutputs();

    pinMode(MOTOR_Y_ENABLE_PIN, OUTPUT);
    motorY.setEnablePin(MOTOR_Y_ENABLE_PIN);
    motorY.setPinsInverted(false, false, false); // Remove inversion if not needed
    motorY.setAcceleration(1000); // Adjust acceleration value
    motorY.setMaxSpeed(100); // Adjust max speed value
    motorY.setSpeed(50); // Adjust speed value
    motorY.enableOutputs();

    pinMode(MOTOR_Z_ENABLE_PIN, OUTPUT);
    motorZ.setEnablePin(MOTOR_Z_ENABLE_PIN);
    motorZ.setPinsInverted(false, false, false); // Remove inversion if not needed
    motorZ.setAcceleration(1000); // Adjust acceleration value
    motorZ.setMaxSpeed(100); // Adjust max speed value
    motorZ.setSpeed(50); // Adjust speed value
    motorZ.enableOutputs();


}


void loop() {
  // // Achieveing the Home position
  //   // Calculate the steps for each motor based on the joint angles
  // int stepX = (Theta_1 / steps_angles);
  // Serial.println(stepX);
  // int stepY = (Theta_2 / steps_angles);
  // Serial.println(stepY);
  // int stepZ = (Theta_3 / steps_angles);
  // Serial.println(stepZ);

  // int acceleration = 100; // steps per second per second
  // int deceleration = 100; // steps per second per second
  // // Move the motors to the desired positions
  // motorX.moveTo(stepX);
  // motorY.moveTo(stepY);
  // motorZ.moveTo(stepZ);

  // while (motorX.distanceToGo() != 0 || motorY.distanceToGo() != 0 || motorZ.distanceToGo() != 0) {
  //   motorX.run();
  //   motorY.run();
  //   motorZ.run();
  // }






  // Calculate the steps for each motor based on the joint angles
  int stepsX = (theta1 / steps_angles);
  Serial.println(stepsX);
  int stepsY = (theta2 / steps_angles);
  Serial.println(stepsY);
  int stepsZ = (theta3 / steps_angles);
  Serial.println(stepsZ);

  int acceleration = 10; // steps per second per second
  int deceleration = 10; // steps per second per second
  // Move the motors to the desired positions
  motorX.moveTo(stepsX);
  motorY.moveTo(stepsY);
  // motorZ.moveTo(stepsZ);

  while (motorX.distanceToGo() != 0 || motorY.distanceToGo() != 0) {
    motorX.run();
    motorY.run();
    // motorZ.run();
  }

  delay(5000000);
    // Reset motor positions to zero
  motorX.setCurrentPosition(0);
  motorY.setCurrentPosition(0);
  motorZ.setCurrentPosition(0);
    // // Run motors
    // motorX.run();
    // motorY.run();
    // motorZ.run();

    // Optionally add any other continuous operations here
}

This is my code before this is the inverse kinematics calculation to achieve the desired position.

Why is so much of your code commented out?

Also, I see you have a delay of 5 million milliseconds, which is about 1.4 hours. Is this intentional?

If you are using 3 stepper motors, you will most likely need 3 motor drivers

A good way to proceed is to get one stepper working as expected first, then add additional features.

Forum members will want to see a clear schematic diagram of your wiring and links to the product pages or data sheets of all the parts.

Did you set the motor driver current limit to 1 Ampere or less, as required by the A4988 driver?

1 Like

In void loop the first part is commented because right now to avoid confusion initially, it achieves to position twice .
the delay I kept to check the holding torque because the motor needs to hold the position until further command.
Yes I am using 3 motor drivers. Its a complete kit available on AMAZON , arduino uno, CNC shield and stepper motor drivers.
link of product - https://www.amazon.de/HEZHU-Erweiterungsplatinen-Treiber-Kühlkörper-Arduino/dp/B0BNHT3C22/ref=asc_df_B0BNHT3C22/?tag=googshopde-21&linkCode=df0&hvadid=696220738236&hvpos=&hvnetw=g&hvrand=10432121094956749015&hvpone=&hvptwo=&hvqmt=&hvdev=c&hvdvcmdl=&hvlocint=&hvlocphy=9043537&hvtargid=pla-2071791825820&psc=1&mcid=93b396baec9a34009ac62248a856449a&th=1&psc=1&gad_source=1

Yes i did set current limit as per required.

Hi, @mit_1503
Welcome to the forum.

What is pulling the stepper away from its position?

What is your project?

Can I suggest some new simple code for one stepper to see the problem and try to fix it.

Tom.. :smiley: :+1: :coffee: :australia:

The weight of the link attached to each motor is like beam attached to it which is making hard for motor to hold position.
My project is based on RCM manipultor.
Yes please.

Remote Control Manipulator...

I think you will find they have position feedback so as to keep the arms in a set position and to know exactly what pistion/angle they are at.

Like a servo.

What is your actual load, as in N-m of torque?
What is your stepper rated at, as in N-m of torque?

Can you please tell us your electronics, programming, arduino, hardware experience?

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

I have stepper motor rated to 36 Ncm torque with 0.9 degree step angle . About actual torque I don't have data as this was setup provided to me some work was done by previous student.
I am doing internship so you can say I don't have intermediate experience also with this hardware.

Perhaps the torque due to the attached hardware exceeds the motor torque.
See this tutorial: