Stepper motor makes random- lenght rotation after providing power

Hello,
I have a problem. I have a stepper motor connected to a4988. I am using that code:

void setup()
{

pinMode(4,OUTPUT); // STEP 1
pinMode(3,OUTPUT); // DIR 1
pinMode(8,OUTPUT); // enable 1

}

void loop()
{

digitalWrite(8,HIGH) ;
digitalWrite(3,LOW) ;// O
digitalWrite(8,LOW) ;
digitalWrite(4,HIGH);
delay(15);
digitalWrite(4,LOW);
digitalWrite(8,HIGH) ;

digitalWrite(3,HIGH);
digitalWrite(8,LOW) ;
digitalWrite(4,HIGH);
delay (15);
digitalWrite(4,LOW);
digitalWrite(8,HIGH) ;

}

And everything is working fine but there is one major problem - it makes random length rotation when providing power, after that it works normally. Any problems how to solve that ? I also didn't have any condensator between VCC and GND for motors.

According to your code, naturally it will jump sometimes when you apply power.

That's because you have a loop..... and when you turn off the motor, you won't know which logic states you ended up with. So when you power-up again, the starting combination:

digitalWrite(4,HIGH);
digitalWrite(8,LOW);

might be different from the state at which the motor was holding when you turned off the motor.

I notice that you don't have delay added at the end of your code. Maybe you didn't need it. But just indicating it. Might just want to check the ordering of your sequences. Yours starts with 8,3,8,4 --- then it goes 4, 8, 3, 8, 4.... then it goes 4, 8.... then goes back to the beginning of the loop. In order to make the motor do exactly what you want.... just need to really think about what these control commands are doing when we're running the code; and think about where the states finally end up at power-off, and what starting states you have during power-up.

You may find some ideas in this Simple Stepper Code

...R
Stepper Motor Basics

Ok, so could someone just say what i need to correct in code? Because i still have no idea...

JashorPL:
Ok, so could someone just say what i need to correct in code? Because i still have no idea...

Did you try the code I gave a link to?

Does it display the same problem?

...R

I dont know. At the begining it starts spinning in one way, immediately after one rotation it makes another rotation in other direction , and after 3 seconds it makes not full rotation in other direction.

After changing numer of steps it makes full rotations - one in one direction, another in other direction, and after 3 seconds another in other direction

JashorPL:
After changing numer of steps it makes full rotations - one in one direction, another in other direction, and after 3 seconds another in other direction

That sounds like it is doing what it is supposed to do.

From your description I presume it is NOT making "random length rotation when providing power".

Does this all mean that you now have a suitable solution to your problem?

...R

I solved a problem in a other, very simple way i just switch on motors 10 seconds after providing power :slight_smile: Problem solved

JashorPL:
I solved a problem in a other, very simple way i just switch on motors 10 seconds after providing power :slight_smile: Problem solved

That removed the symptoms. The main thing is explaining why.... ie. what was the cause of your issue to begin with. And why activating the motors after 10 seconds makes it all behave.

I think it was difficult to understand your code because there was no explanation about what the pins 4, 3, and 8 were associated with. Your code has comments like 'step 1', 'dir 1' etc. Others probably won't know what these are for.

For example..... it appears that pin 8 is an enable/disable pin.

pinMode(4,OUTPUT); // STEP 1
pinMode(3,OUTPUT); // DIR 1
pinMode(8,OUTPUT); // enable 1

So why in the code below you appear to enable something, and then set a 'direction' and then disable something? And then you send a 'step' command. Doesn't appear to make sense.

digitalWrite(8,HIGH) ;
digitalWrite(3,LOW) ;// O
digitalWrite(8,LOW) ;
digitalWrite(4,HIGH);

JashorPL:
I solved a problem in a other, very simple way i just switch on motors 10 seconds after providing power :slight_smile: Problem solved

I would appreciate feedback about the effect of using my code as I have not experienced any problem with it

...R

Your code is working normally, just as it was supposed to.
Thanks to everyone for help.

Thank you.

That leaves me curious to know why you don't use it rather than the kludge of delaying the motor switch on.

...R

JashorPL:
I solved a problem in a other, very simple way i just switch on motors 10 seconds after providing power :slight_smile: Problem solved

Could come back to bite ya if you don't actually know what you actually 'solved'. Should explain why you need to do that.