Stepper motor moving randomly while starting

Hello,
I was working on a project with stepper motor and when i connected my arduino to laptop and gave the a4988 stepper motor drive the power, the motor moves in random direction for about a second which misaligns everything. It is really necessary for me not to rotate those motor while starting. I have connections needed, in the forum page below.

Please keep in mind that i am using 6 stepper motors to hold a rubiks cube. and when i supply power to the connection all motors move randomly for about a second and misaligns the cube in such a way that it gets very strained and is about to break. and whenever i correct it and give power supply again the whole thing repeats itself.

The connections for all the stepper motors and the arduino with the power supply is very complicated and is hard to draw a diagram. i will try to make the diagram and upload it as soon as possible.

Thanks in advance.

A stepper motor won't move unless it is given step instructions. So what is it in your program that is giving unwanted step instructions.

I'm guessing if it moves for 1 second that it is moving considerably more than one or two steps. Do you know how many? Is it always the same number?

Is it only happening to one motor?

Noise on the line between the Arduino and the driver is a possibility. It may help to tie those lines to ground with a resistor - say 4700 ohms. It might also help to use digitalWrite(pin, LOW) for those lines very early in setup().

Another thing to check is what happens between the time the library (AccelStepper ?) is initialized and when your code actually tells it to move a motor.

...R

i don't know what is giving motors the signal to move and the rotation is random e.g. it moves 3 steps cw then 1 step ccw and then 2 step cw and 4 step ccw. its not regular and it moves around 20 steps cw or ccw. and yes all the motors are moving randomly at the same time. and i don't think that there is a problem with the program. Thee is a problem with the connection which is giving some random signals to the motors.

I am not using AccelStepper library. and when the motors are at rest after moving randomly the rotation of motors is as provided by me using the program. After those random rotations everything works fine. whenever i pass some argument using the ide, it is executed immediately.

Can’t help unless you post your code. Use # and post between tags.

Well i don’t really know programming, i got this program from this forum only. Its attached as the number of lines is too many.

well, i really don’t think that there is a problem with the program.
Thanks for the help.

Stepper Motor Program.txt (12 KB)

here is the circuit diagram of my connection. Pardon me for the bad drawing.

Some of the variable names in that program are reminiscent of an example I wrote.

You don't say if you tried any of my earlier suggestions.

Here's another one. Disconnect all but one motor and see if there is a problem. If not I would suspect your power supply is not up to the task.

...R

That sketch should only change the stepper motor when it receives specific characters on the serial port, and it will print some text out over the serial port to show what it is doing.

Does the serial output show that your sketch is receiving serial input? If so, where is it coming from? If not, the steppers are probably not being moved because of anything in the code and you could prove that by deleting all the code inside loop() and showing that the problem still happens.

It's not causing your problem, but the calls from loop() to pinMode() are redundant, since you set all the pins to outputs in setup().

It's not causing your problem, but it's bad practice to call pinMode() for pin 1 after calling Serial.begin(), since pin 1 is a serial pin.

If you have no programming experience you shouldn’t be trying to write a program for a chopper stepper driver. You should be using the AccelStepper Library.

Try installing the AccelStepper library
https://www.pjrc.com/teensy/td_libs_AccelStepper.html

Evidently there is more to driving a stepper driver than just manipulating the Step pin.

BTW,
Did anybody notice the pinMode statements inside the Case statement ?

  case 'f':  {   
                      Serial.println("Starting Stepper Motor 1 CW");
                      digitalWrite(ledPin, LOW);
  
                      delay(1);

                      pinMode(directionPin1, OUTPUT);
                      pinMode(stepPin1, OUTPUT);
                      pinMode(ledPin, OUTPUT);
                      
                      digitalWrite(directionPin1, HIGH);
                      for(int n = 0; n < numberOfSteps; n++) 
                      {
                        digitalWrite(stepPin1, HIGH);
                        delayMicroseconds(pulseWidthMicros);
                        digitalWrite(stepPin1, LOW);
    
                        delay(millisbetweenSteps);
    
                        digitalWrite(ledPin, !digitalRead(ledPin));
                      }
                     
                     delay(100);
                    }
                    break;

one_stepper_Microstep_Sixteenth_step_Accel_Speed_Position_.ino (1.19 KB)

raschemmel:
If you have no programming experience you shouldn't be trying to write a program for a chopper stepper driver. You should be using the AccelStepper Library.

Sorry to disagree. What's difficult about

if (curMicros - prevMicros >= stepIntervalMicros) {
   prevMicros += stepIntervalMicros;
   digitalWrite(p, HIGH);
   digitalWrite(p,LOW);
}

I do agree that AccelStepper can be helpful if you want to have your motors accelerate and decelerate. But you do lose some control of the situation when you use it.

The OP's problems are not due to Accelstepper or the lack of it.

...R

The OP's problems are not due to Accelstepper or the lack of it.

No contest.

@Robin2
You are right, i am using the code that you gave me on some other thread of this forum. Thanks for that.
when only one motor is connected the motor starts properly. i mean there is no random movement of the motor at the time of switching on the power supply. But when all motors are connected, all motors move in clockwise direction only. I don't know why this is happening. It moves around 1 to 3 steps in clockwise direction on all motors.

@PeterH
nothing is showing in serial output. Its just the blank page. And i deleted the code from inside the loop and started the motor and those rotated once again. Also i removed the pinmode() from the code and its still happening.

I really don't know what the problem is.
One thing that i noticed was that when those motors are not fitted in their respective places and are kept loose, they move only one time while turning on power and if power is turned on and off any number of times it doesn't move.
on the other hand, if these motors are placed then these motors turn in clockwise direction and that too 3 to 4 steps. i think they move 3 to 4 steps because they can't turn more simultaneously.

Is there any particular reason why this is happening?

You might consider tying the driver RESET pin to the arduino RESET pin. (check for logic state compatibility or if they are opposite state). Is the driver reset pin active LOW ? You might also consider adding a power on reset circuit since this scenario is precisely what power on reset circuits are designed to prevent.

What sort of driver are you using and how is it connected? The spontaneous nature of the movement makes me wonder whether the stepper driver / motor driver may have input pins that are not being driven by the Arduino and have been left to float, or perhaps it isn't grounded properly.

@raschemmel
The sleep bar and the reset bar are connected. It's according to the circuit diagram that was in the related website and many people suggested on this forum only.
I don't get this part

check for logic state compatibility or if they are opposite state

And can you please explain what you mean by

You might also consider adding a power on reset circuit since this scenario is precisely what power on reset circuits are designed to prevent.

@PeterH
I am using a4988 stepper motor drive, the connections are there in the reply#5. And i also had the same doubt that may be the pins are free but i checked it many times whether the connections were fine or not and they were all fine.
and what do you mean by not grounded properly. If there were a problem with the connection in grounding then the motors won't work afterwords, but they do once the power is switched on and the motors have moved a bit.

One more thing, after the motors have moved a bit they work perfectly. They move very precisely from that particular position.
So there is no worry about proper functioning of components.

From your drawing, you have the enable pins unconnected. Tie them to an arduino pin that is set HIGH first thing in void setup.
Then before void loop or first thing in void loop, set it LOW to enable the drivers.

@steinie44
the name written beside that last pin is enable bar. So i think i will have to set the pin as low first and then high.
I will try it and post the results.

check for logic state compatibility or if they are opposite state

I didn’t look up the datasheet for your driver so I didn’t know if it had RESET or RESET-BAR , which you clarified.
Most of these drivers have RESET & SLEEP tied together but if since the driver and the arduino both have the “same active logic state” for RESET (RESET-BAR) then you can use a power-on reset circuit. Since you apparently have no industrial uC programming experience, you are not familiar with the concept of a power on reset circuit. All complex systems that are uProcessor controlled use power-on reset circuits (including your car) to guarantee everything is in a known state at all times. The way it works is a circuit (either discrete components (like a resistor and capacitor) or an ic (like a LM555 or a special POR chip has in input pin and an output pin. The input pin is tied to the 5V power line. The output pin is tied to uC reset (AND ALL THE OTHER CHIP RESET PINS, WHICH COULD NUMBER IN THE DOZENS). When the power comes on the POR circuit detects the rising voltage on the 5V power pin and immediately pulls it’s output pin LOW , thus initiating a RESET of ALL systems /chips /circuits in the system.
Your motors will never be able to move if the driver is in reset mode. (which is typically 300mS)

I have had no problem using Pololu A4988 drivers wired exactly as in the Pololu web page. It is not necessary to connect enable - but in the circumstances it may help. And I believe @steinie44 has the logic the correct way round.

...R

palashrc0945:
@Robin2
You are right, i am using the code that you gave me on some other thread of this forum. Thanks for that.
when only one motor is connected the motor starts properly. i mean there is no random movement of the motor at the time of switching on the power supply. But when all motors are connected, all motors move in clockwise direction only. I don't know why this is happening. It moves around 1 to 3 steps in clockwise direction on all motors.

Sorry, I forgot to go back to read the earlier posts. What you say strongly suggests to me a power supply problem. I can't remember if you said what stepper motors you are using. I don't think you have told us what power supply you have - volts and amps.

...R