Stepper Motor Missed Step

Hello, I have some problem from my project. My stepper always having an issue where i told the stepper to move 100 steps forward and then move 100 steps backward which bring it to the initial position(but it didnt, sometimes it stop below the initial position sometimes above).

I’ve tried to look for the position feedback from serial monitor(in which print the value of x that hold the current position of the stepper), and x is increasing every time my step value is HIGH while direction value is HIGH, and decrease when step value is HIGH and direction value is LOW. and also pin enable is high when the value from input serial is equal to the value of current position of the stepper.

in serial monitor they look like this
100 0
100 1
100 2

100 99
100 100

and
100 1
100 0

im using arduino mega and A4988 and small stepper motor x27

only 3 wire connecting the arduino and A4899 which is:
pin2 = step
pin3 = direction
pin4 = enable

and vmot input is from 12v and all ground connected to each other

make me a bit confuse when seeing that the stepper dont move exactly like arduino think it was

const int stStep = 2;
const int stDir = 3;
const int stEn = 4;
int stMove = 0;
int stCurrent = 0;
int ctLoop = 0;

bool StringComplete = false;
String inputString = "";
void setup()
{
  Serial.begin(115200);
  pinMode(stStep, OUTPUT);
  pinMode(stDir, OUTPUT);
  pinMode(stEn, OUTPUT);
  
}

void loop()
{
    readSerial();
    if (ctLoop >= 1)
    {
        if(stMove > stCurrent)
        {
          digitalWrite(stEn, LOW);
          digitalWrite(stDir, HIGH);
          digitalWrite(stStep, HIGH);
          stCurrent++;
        } 
        else if (stMove < stCurrent)
        {
          digitalWrite(stEn, LOW);
          digitalWrite(stDir, LOW);
          digitalWrite(stStep, HIGH);
          stCurrent--;
        }
        else
        {
          digitalWrite(stEn, HIGH);
        }
        ctLoop = 0;
    }
    ctLoop++;
    digitalWrite(stStep, LOW);
}

void readSerial()
{
  if(Serial.available() > 0)
  {
    char inChar = (char)Serial.read();
    inputString += inChar;
    if(inChar == '\n')
    {
      StringComplete = true;
    }
  }

  if(StringComplete == true)
  {
    stMove = inputString.substring(0, inputString.length()-1).toInt();
    StringComplete = false;
    inputString = "";
  }
}

You need to post a link to the datasheet for your stepper motor.

Have you adjusted the current limit on the A4988 to match your motor?

When you have problems with a stepper missing steps it is always a good idea to try a very much slower speed - perhaps down by a factor of 10.

...R
Stepper Motor Basics
Simple Stepper Code

i cant find the datasheet for my stepper motor, i took it apart from second hand cheap cluster but it look like a lot to x27 stepper motor.

i'm not adjust anything because some youtuber recommended not to.

i think the problem is when every time i told the stepper to move back, not forward. but after going backward the stepper keep missing accuracy until me resetting the position manually.

is it possible that my arduino pin didnt change state for direction fast enough? - just my opinion though.

i'm not adjust anything because some youtuber recommended not to.

Sorry that is rubbish, these things are not preset to any value when you get them so you need to get enough current through the motor.

As Robin says slowing steps down, or increasing the current is the way to prevent steps being missed.

Steps are missed because the motor stalls due to insufficient torque. Even with no load on the shaft a motor has a self stalling stepping rate. Motors can be made to run faster in several ways:-

  1. Reduce overall step rate.
  2. Increase the current through the coils.
  3. Increase the voltage you are driving them at. The stepping motor driver should compensate so that the current remains the same.
  4. Build up the speed, that is, start with a slower speed and have it ramp up to the final speed. When you do this it is likely that you have to slow down the pulses before stopping them altogether to prevent the motor over running.

is it possible that my arduino pin didnt change state for direction fast enough?

Absolutely not.
However, it is possible that the momentum of the motor put an extra demand on the torque when changing direction and so exceeded the self stall torque. Try putting a small delay before changing direction to give the motor time to stop.

nadepp:
i cant find the datasheet for my stepper motor, i took it apart from second hand cheap cluster but it look like a lot to x27 stepper motor.

If you have a small stepper motor from a car instrument cluster then I can't help. I have no experience of them.

It would be a good idea to edit your Original Post and change the title so it mentions that you are using a small instrument stepper motor. That way you will catch the attention of people who can help.

...R

i know the problem, after changing the v mot input on A4988 from 12v to 5v it move smoothly. i dont know why but it seems to move perfectly.

im not increasing the voltage instead of decreasing it.

i need to look closely to the datasheet next time.

thanks

after changing the v mot input on A4988 from 12v to 5v it move smoothly. i dont know why but it seems to move perfectly.

Sounds like you were putting too much current through the motor.

nadepp:
i know the problem, after changing the v mot input on A4988 from 12v to 5v it move smoothly. i dont know why but it seems to move perfectly.

im not increasing the voltage instead of decreasing it.

i need to look closely to the datasheet next time.

thanks

Nice going! Rather than adjust in the current on the controller, you just changed the voltage. Next you will wonder why there is not more torque from the motor.

Paul