pololu a4988 Stepper motor max rpm test

I made simple code to test your stepper motors speed and acceleration

have fun

int stp = 13;  //connect pin 13 to step
int dir = 12;  // connect pin 12 to dir

int Speed = 2500; //lower this number to find your best starting speed

int MaxSpeed = 200; //Lower this number to find your max speed

int accel = 1; //change this number to experiment different acceleration


//with 17HS4401 500=Speed and 100=MaxSpeed was my best result


void setup()
{
  pinMode(stp, OUTPUT);
  pinMode(dir, OUTPUT);
}

void loop()
{

  if (Speed == MaxSpeed) 
  {
    digitalWrite(dir, LOW);
    digitalWrite(stp, HIGH);
    delayMicroseconds(Speed);
    digitalWrite(stp, LOW);
    delayMicroseconds(Speed);
  }

  if (Speed > MaxSpeed)
  {
    digitalWrite(dir, LOW);
    Speed = Speed - accel;
    digitalWrite(stp, HIGH);
    delayMicroseconds(Speed);
    digitalWrite(stp, LOW);
    delayMicroseconds(Speed);
  }

  else {
  }
}

Yan cnc programmer

You need linear acceleration ramps, yours are nothing like linear. Delay is the reciprocal of speed.

Also you won't get accurate values for the max speed because you aren't compensating for code delays. You should be accounting all the microseconds, not just the ones inside delayMicroseconds():

  if (micros () - last_time >= period)  // accurately lock to the clock
  {
    last_time += period ;
    digitalWrite(stp, HIGH);
    delayMicroseconds(5);  // wide enough for most stepper drivers
    digitalWrite(stp, LOW);
  }

So period accurately reflects the true period of the pulses generated.

However if you want linear speed ramps it possibly better to use DDS techniques and a fast timer interrupt to get direct control of speed (not period) and linear acceleration ramps.

@MarkT I can also use blink without delay for the same result as your code. I'm a beginner and your information about code delay and DDS technique is very interresting, do you have a link for more informations about those.

Thank you for your time

p.s. I was looking for a simple code to test my stepper and after a week of search the only things I found was people with the same problem as me so I figured I had to post my program.

Have a look at this Simple Stepper Code. The second example uses millis() and micros() for timing.

...R Stepper Motor Basics