Stepper motor not returning on its original position

hi all,

i am very new to arduino and stepper motors,

i have started a project where i just need the motor rotate clockwise and anticlockwise (for example, 1000 steps clockwise and 1000 anticlockwise), i have code which is working fine but the problem is when the motor rotates it loses its position and after every cycle it is getting off the position. does any one know why it is happening and what is the solution to keep the motor on its original position from where it start rotations.

thank you in advance.

sajid

If You post schematics showing the powering, and the code, we will have more information to start from.

hi,

thank you for your reply, here is he code i am using with arduino and shield. my motor is nema17 and drivers i am using are a4988

const int stepPin1 = 2;
const int dirPin1 = 5;
const int stepPin2 = 3;
const int dirPin2 = 6;

void setup() {
// Sets the two pins as Outputs
pinMode(stepPin1,OUTPUT);
pinMode(dirPin1,OUTPUT);
pinMode(stepPin2,OUTPUT);
pinMode(dirPin2,OUTPUT);
}
void loop() {
digitalWrite(dirPin1,HIGH); // X FRD 1
for(int x = 0; x < 20; x++) {//X FRD 1
digitalWrite(stepPin1,HIGH); //X FRD 1
delayMicroseconds(5000); //X FRD 1
digitalWrite(stepPin1,LOW); //X FRD 1
delayMicroseconds(5000);//X FRD 1
}
delay(200); // One second delay 1
//X FORWARD END BLOCK 1

digitalWrite(dirPin1,LOW); // X REV 1
for(int x = 0; x < 19.25; x++) {//X REV 1
digitalWrite(stepPin1,LOW); //X REV 1
delayMicroseconds(5000); //X REV 1
digitalWrite(stepPin1,HIGH); //X REV 1
delayMicroseconds(5000);//X REV 1
}
delay(100); //X REV 1
//X REVERSE END

digitalWrite(dirPin1,HIGH); // X FRD 2

for(int x = 0; x < 20; x++) {//X FRD 2
digitalWrite(stepPin1,HIGH); //X FRD 2
delayMicroseconds(5000); //X FRD 2
digitalWrite(stepPin1,LOW); //X FRD 2
delayMicroseconds(5000);//X FRD 2
}
delay(200); // One second delay 2
//X FORWARD END BLOCK 2

digitalWrite(dirPin1,LOW); // X REV 2
for(int x = 0; x < 19.25; x++) {//X REV 2
digitalWrite(stepPin1,LOW); //X REV 2
delayMicroseconds(5000); //X REV 2
digitalWrite(stepPin1,HIGH); //X REV 2
delayMicroseconds(5000);//X REV 2
}
delay(100); //X REV 2
//X REVERSE END

for(;;){}
}

hi again,

sorry ignore the previous code, i am using the below code

const int stepPin1 = 2;
const int dirPin1 = 5;
const int stepPin2 = 3;
const int dirPin2 = 6;

void setup() {
// Sets the two pins as Outputs
pinMode(stepPin1,OUTPUT);
pinMode(dirPin1,OUTPUT);
pinMode(stepPin2,OUTPUT);
pinMode(dirPin2,OUTPUT);
}
void loop() {
digitalWrite(dirPin1,HIGH); // X FRD 1
for(int x = 0; x < 500; x++) {//X FRD 1
digitalWrite(stepPin1,HIGH); //X FRD 1
delayMicroseconds(1000); //X FRD 1
digitalWrite(stepPin1,LOW); //X FRD 1
delayMicroseconds(1000);//X FRD 1
}
delay(200); // One second delay 1
//X FORWARD END BLOCK 1

digitalWrite(dirPin1,LOW); // X REV 1
for(int x = 0; x < 500; x++) {//X REV 1
digitalWrite(stepPin1,LOW); //X REV 1
delayMicroseconds(1000.2); //X REV 1
digitalWrite(stepPin1,HIGH); //X REV 1
delayMicroseconds(1000.2);//X REV 1
}
delay(100); //X REV 1
//X REVERSE END

digitalWrite(dirPin2,LOW); //Y 1
for(int x = 0; x < 400; x++) {//Y 1
digitalWrite(stepPin2,LOW);//Y 1
delayMicroseconds(1000);//Y 1
digitalWrite(stepPin2,HIGH);//Y 1
delayMicroseconds(1000);//Y 1
}
delay(100);//Y REV
//Y FORWARD 1 END

digitalWrite(dirPin1,HIGH); // X FRD 2
for(int x = 0; x < 500; x++) {//X FRD 2
digitalWrite(stepPin1,HIGH); //X FRD 2
delayMicroseconds(1000); //X FRD 2
digitalWrite(stepPin1,LOW); //X FRD 2
delayMicroseconds(1000);//X FRD 2
}
delay(100); // One second delay 2
//X FORWARD END BLOCK 2

digitalWrite(dirPin1,LOW); // X REV 2
for(int x = 0; x < 500; x++) {//X REV 2
digitalWrite(stepPin1,LOW); //X REV 2
delayMicroseconds(1000.4); //X REV 2
digitalWrite(stepPin1,HIGH); //X REV 2
delayMicroseconds(1000.4);//X REV 2
}
delay(100); //X REV 2
//X REVERSE 2 END

digitalWrite(dirPin2,LOW); //Y 2
for(int x = 0; x < 400; x++) {//Y 2
digitalWrite(stepPin2,LOW);//Y 2
delayMicroseconds(1000);//Y 2
digitalWrite(stepPin2,HIGH);//Y 2
delayMicroseconds(1000);//Y 2
}
delay(100);//Y REV
//Y FORWARD 2 END

digitalWrite(dirPin1,HIGH); // X FRD 3
for(int x = 0; x < 500; x++) {//X FRD 3
digitalWrite(stepPin1,HIGH); //X FRD 3
delayMicroseconds(1000); //X FRD 3
digitalWrite(stepPin1,LOW); //X FRD 3
delayMicroseconds(1000);//X FRD 3
}
delay(100); // One second delay 3
//X FORWARD END BLOCK 3

digitalWrite(dirPin1,LOW); // X REV 3
for(int x = 0; x < 500; x++) {//X REV 3
digitalWrite(stepPin1,LOW); //X REV 3
delayMicroseconds(1001); //X REV 3
digitalWrite(stepPin1,HIGH); //X REV 3
delayMicroseconds(1001);//X REV 3
}
delay(100); //X REV 3
//X REVERSE 3 END

digitalWrite(dirPin2,HIGH); //Y rev
for(int x = 0; x < 1000; x++) {//Y rev
digitalWrite(stepPin2,HIGH);//Y rev
delayMicroseconds(1000);//Y rev
digitalWrite(stepPin2,LOW);//Y rev
delayMicroseconds(1000);//Y rev
}
delay(100);//Y REV
//Y FORWARD 2 END

for(;;){}
}

Yes you are missing steps because you are sending the pulses too fast for the load on the motor.

So simply slow down the speed.

A stepping motor can be made to run faster if the speed is ramped up to the final speed and ramped down before stopping, but this is harder to do.

What you have done is a classic test to see if your motor is being stepped too fast, and yours is.

The other way to get more torque out of a motor is to supply it with a higher voltage, or adjust your driver to give more current.


my motor is nema17

Which only tells you about the size of the mounting of the motor, nothing about the current it takes.

1 Like

Hi, @ausajid
Welcome to the forum.

To add code please click this link;

It will put your code in a scrolling window and make it easier to read.
A tip; before posting press < CTRL > < T > to Auto Format your code, it will place indents to make your code even easier to read.

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

Then its probably not fine :slight_smile:

Anyway you almost certainly need speed ramping for you motor (unless its ultra-tiny, which NEMA17 isnt), and you've not speed-ramped the pulses so it miss-steps and stalls etc.

The AccelStepper library allows automatic speed ramping and simplifies this greatly for you.

Hi,
We need a circuit diagram of your project?
Please include power supplies and labels on components and pinout.

What are you powering your project with?

Can you please post a link to data/spec of your stepper motor?

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

hi MarkT,

as i mentioned that i am very new on arduino, i do not know how to use AccelStepper library, can you please give me some hints.

best regards.

sajid

When you download the library it comes with examples of how to use it.

Look at the examples that come with it?
read the explanatory comments in AccelStepper.h?

hi,

thank you Grumpy_Mike,

i will try over the weekend,

i have also ordered L293D shield as well to see if that will work as most of the people are suggesting this driver. i will let you know the outcome.

regards.

sajid

That is only because most people are idiots. It is a very old obsolete driver chip with poor performance. It can remove 3 to 4 volts from your maximum drive voltage because they are made from transistors. Get one that uses FETs, they are much better.