Stepper Motors Move Randomly On Power Up

Hello everyone!

I have just started working with Arduino(s), the mega2560 specifically.

As a first test, I wanted to see if I could control 5 stepper motors with it, using the “accelstepper” library

All of the steppers are controlled by the A4988 drivers, with the control pins of each, going to the arduino.

I made a simple program to act as a binary counter, where every time the latching button changes state, it counts, and the tape on the motors move to point either up or down.

THE THING IS!

On the start up of the Arduino, all of the steppers move and rotate with no pattern-- landing in random starting positions. (resulting me in unplugging the power to the steppers for such beginning sequence)

#include <AccelStepper.h>

// Define some steppers and the pins the will use
AccelStepper stepper1(AccelStepper::DRIVER, 45, 44);
AccelStepper stepper2(AccelStepper::DRIVER, 47, 46);
AccelStepper stepper3(AccelStepper::DRIVER, 49, 48);
AccelStepper stepper4(AccelStepper::DRIVER, 51, 50);
AccelStepper stepper5(AccelStepper::DRIVER, 53, 52);



void setup()
{  
  delay(2000); // attpemt to fix
    pinMode(23, INPUT);
    stepper1.setMaxSpeed(400.0);
    stepper1.setAcceleration(200.0);
    stepper1.moveTo(50);
    
    stepper2.setMaxSpeed(400.0);
    stepper2.setAcceleration(200.0);
    stepper2.moveTo(50);
    
    stepper3.setMaxSpeed(400.0);
    stepper3.setAcceleration(200.0);
    stepper3.moveTo(50); 

    stepper4.setMaxSpeed(400.0);
    stepper4.setAcceleration(200.0);
    stepper4.moveTo(50); 

    stepper5.setMaxSpeed(400.0);
    stepper5.setAcceleration(200.0);
    stepper5.moveTo(50);
    delay(2000); // attempt to fix

}

bool originalState = digitalRead(23);
bool lastState1 = digitalRead(23);
bool lastState2 = digitalRead(23);
bool lastState3 = digitalRead(23);
bool lastState4 = digitalRead(23);
bool lastState5 = digitalRead(23);
bool newState1 = digitalRead(23);

void loop()
{
    newState1 = digitalRead(23);
    if (newState1 != lastState1){
      stepper1.moveTo(-stepper1.targetPosition());
      lastState1 = newState1;
      lastState2 = !lastState2;
      if (lastState2 == originalState){
        stepper2.moveTo(-stepper2.targetPosition());
        lastState3 = !lastState3;
        if (lastState3 == originalState){
          stepper3.moveTo(-stepper3.targetPosition());
          lastState4 = !lastState4;
          if (lastState4 == originalState){
            stepper4.moveTo(-stepper4.targetPosition());
            lastState5 = !lastState5;
            if (lastState5 == originalState){
              stepper5.moveTo(-stepper5.targetPosition());
            }
          }
        }
      }
    }
    stepper1.run();
    stepper2.run();
    stepper3.run();
    stepper4.run();
    stepper5.run();
}

Is this something simple that I am overlooking, that can prevent this?

also (this may be because I am using USB 3.0, but when plugging in the board to my PC to upload the code, I have to wait about 10 minutes, or else I get some sort of ser_send write error)

It appears that when you start up you move each stepper forward by 50 steps and then, based on button presses, you alternate between moving backward by 100 steps (to -50) and moving forward by 100 steps (back to +50). The steppers should alternate between the position 50 steps forward and 50 steps backward from where there were when power was applied. Is that not what it is doing?

Since they never go back to the 0 position before power is removed the new 0 position will be different each time. Each time you reset the Arduino you should see all of the motors move forward 50 steps.

Steppers have no position feedback so they are bad at going to specific positions. Have you thought of using hobby servos? They can turn to any position in at least a 180° range.

I think if you add this line

Serial.println("End of setup");

as the last line of code in setup() you will find that the motors don't move until after setup() has completed.

...R

johnwasser:
It appears that when you start up you move each stepper forward by 50 steps and then, based on button presses, you alternate between moving backward by 100 steps (to -50) and moving forward by 100 steps (back to +50).

I am very impressed that you read my code! And you are completely correct with how it works! (and yes, the motors dont ever go back to their original position, but that is fine for this application)

Let me elaborate:

It works as:
-2 second delay
-rotate the motors to position +50, relative to where ever they start
-2 second delay
-execute counting code

What happens:
-motors spaz
-2 second delay
-rotate the motors to position +50, relative to where ever they start
-2 second delay
-execute counting code

Robin2:
I think if you add this line

Serial.println("End of setup");

as the last line of code in setup() you will find that the motors don't move until after setup() has completed.

...R

As excited as I was, preparing to ask, "Why does this work??", it didn't :frowning:

aakk4477:
As excited as I was, preparing to ask, "Why does this work??", it didn't :frowning:

Are you saying that you added my code as the last item in setup() AND your steppers moved BEFORE that message was printed?

If so that suggests a very different cause for the problem so it is important to be precise.

Please post the complete program with my addition.

...R

aakk4477:
What happens:
-motors spaz

Could you be more specific?
Sound like a wiring problem, like noise on the output pins before they are set to OUTPUT. If there is noise on the STEP and DIRECTION pins of the stepper driver the motor will move.
Do the drivers have an ENABLE pin? If so, you should connect them together and use a pull-up or pull-down resistor to bias them to 'disabled'. Then use an output pin to enable them AFTER the STEP and DIRECTION pins are all configured.