 # 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.