Sub topic 10
Converting RPM to step timing
A stepper driver does not know about RPM. It moves one step for each pulse on its STEP pin, so your sketch must work out how long to wait between pulses. This chapter covers the formula, the limits that apply when you use it, and a non-blocking way to generate the pulses.
This chapter assumes you can write a basic Arduino sketch and that you have a STEP/DIR driver wired to a motor. It also assumes you know your driver's microstepping setting, from the earlier chapters on microstepping and STEP/DIR control.
From RPM to a pulse interval
Three quantities connect the motor's speed to the timing of the pulses.
- Steps per revolution is the motor's full steps per revolution multiplied by the microstepping factor. A 1.8° motor has 200 full steps. At 1/16 microstepping it needs 200 × 16 = 3200 pulses per revolution.
- Steps per second is
RPM × stepsPerRev / 60. Dividing by 60 converts revolutions per minute into revolutions per second. - Step interval is the time between the start of one pulse and the start of the next. It is the reciprocal of the step rate:
1,000,000 / stepsPerSecondmicroseconds. Combined with the formula above, that is60,000,000 / (RPM × stepsPerRev).
The helper function from the earlier example does this in one line:
// Returns the time in microseconds between step pulses
// for a given speed in RPM and steps per revolution
// (motor steps multiplied by the microstepping factor).
unsigned long stepIntervalMicros(float rpm, int stepsPerRev) {
return (unsigned long)(60000000.0f / (rpm * stepsPerRev));
}The table shows how the interval changes with speed and microstepping for a 200-step motor.
| Target RPM | Microstepping | Steps per revolution | Steps per second | Interval (µs) |
|---|---|---|---|---|
| 1 | 1/16 | 3200 | 53.3 | 18750 |
| 60 | full | 200 | 200 | 5000 |
| 60 | 1/16 | 3200 | 3200 | 312.5 |
| 300 | full | 200 | 1000 | 1000 |
| 300 | 1/16 | 3200 | 16000 | 62.5 |
| 600 | 1/16 | 3200 | 32000 | 31.25 |
The same RPM needs a step rate 16 times higher at 1/16 microstepping. Smooth motion costs timing headroom, and the table shows how quickly that headroom shrinks.
Pulse width is separate from the interval
The interval is the whole period. The pulse is the part of it where STEP is HIGH. Drivers only need a short pulse. The A4988 datasheet specifies a minimum of 1 µs for both the HIGH and LOW times. The DRV8825 needs about 1.9 µs. Check your driver's datasheet.
The first example in this tutorial used a 2000 µs HIGH and a 2000 µs LOW. That works at 250 steps per second, but it wastes time, because the driver acts on the rising edge. A fixed short pulse of about 3 to 5 µs followed by waiting out the remainder of the interval is more flexible. The pulse then stays valid at any speed, and the interval can drop as far as the rest of your code allows.
A blocking version and where it fails
The simplest approach computes the interval and calls delayMicroseconds():
const int STEP_PIN = 3;
const int DIR_PIN = 4;
const int MOTOR_STEPS = 200;
const int MICROSTEPS = 16;
const float TARGET_RPM = 60.0f;
unsigned long stepIntervalMicros(float rpm, int stepsPerRev) {
return (unsigned long)(60000000.0f / (rpm * stepsPerRev));
}
void setup() {
pinMode(STEP_PIN, OUTPUT);
pinMode(DIR_PIN, OUTPUT);
digitalWrite(DIR_PIN, HIGH);
}
void loop() {
unsigned long interval =
stepIntervalMicros(TARGET_RPM, MOTOR_STEPS * MICROSTEPS);
digitalWrite(STEP_PIN, HIGH);
delayMicroseconds(3);
digitalWrite(STEP_PIN, LOW);
delayMicroseconds(interval - 3);
}This runs at about the right speed, but it has three weaknesses:
- The delay range is limited. On AVR boards such as the Uno,
delayMicroseconds()is only reliable up to 16383 µs. Below about 3.2 RPM at 1/16 microstepping, the interval exceeds that and the delay becomes wrong. The very-low-speed row in the table (18750 µs) is already out of range. - Other code is blocked. Nothing else, such as reading a button or a serial command, can run while the sketch sits in a delay.
- The speed runs slightly fast.
digitalWrite()and the loop overhead take time. The error is small at slow speeds. At 62 µs intervals, a few microseconds of overhead is several percent.
A non-blocking version with micros()
This version checks the clock on each pass through loop() and emits a pulse only when a full interval has elapsed. It also supports intervals of any length and leaves room for other work.
const int STEP_PIN = 3;
const int DIR_PIN = 4;
const int MOTOR_STEPS = 200;
const int MICROSTEPS = 16;
const float TARGET_RPM = 120.0f;
unsigned long stepInterval;
unsigned long lastStepTime;
void setup() {
pinMode(STEP_PIN, OUTPUT);
pinMode(DIR_PIN, OUTPUT);
digitalWrite(DIR_PIN, HIGH);
long stepsPerRev = (long)MOTOR_STEPS * MICROSTEPS;
stepInterval = (unsigned long)(60000000.0f / (TARGET_RPM * stepsPerRev));
lastStepTime = micros();
}
void loop() {
unsigned long now = micros();
if (now - lastStepTime >= stepInterval) {
lastStepTime += stepInterval; // keep the schedule, do not drift
digitalWrite(STEP_PIN, HIGH);
delayMicroseconds(3); // pulse width above the driver minimum
digitalWrite(STEP_PIN, LOW);
}
// Other short, non-blocking work can go here.
}Three details matter in this code:
now - lastStepTimeuses unsigned subtraction. It stays correct whenmicros()wraps around after about 71 minutes, as long as both values areunsigned long.lastStepTime += stepIntervalkeeps the schedule. SettinglastStepTime = nowinstead would add the lateness of each check to every interval, and the speed would drift low.- The multiplication uses
long.MOTOR_STEPS * MICROSTEPSis 3200, which fits anint, butrpm * stepsPerRevcan easily pass 32767 on a 16-bitint. Do the arithmetic infloatorlong.
At 120 RPM and 1/16 microstepping, the interval is 156 µs, which this loop handles comfortably.
Limits on the achievable step rate
Even a correct formula cannot produce a step rate the board cannot deliver. Several limits apply.
- Software overhead. On an Uno, one
digitalWrite()takes roughly 4 to 5 µs, and the loop adds more. In practice, a pure-digitalWritesketch tops out at tens of thousands of steps per second, and less once you add acceleration math. - Interval truncation. The conversion to
unsigned longdrops the fraction. At 60 RPM and 1/16 microstepping, 312.5 µs becomes 312 µs, so the motor runs about 0.16% fast. At 31.25 µs the same truncation costs 0.8%. If you need exact long-term speed, accumulate the fractional part or use hardware timers. - Jitter. Interrupts, such as the
millis()timer interrupt on AVR, delay some pulses by several microseconds. The motor and its mechanical inertia average this out at moderate speeds. At very high step rates the jitter becomes a large fraction of the interval and can be audible as roughness. - The motor itself. A step rate the code can produce is not necessarily one the motor can follow. Torque falls as speed rises, and a motor started directly at a high step rate stalls. The next chapters cover torque loss and acceleration ramps.
Common errors
- Wrong microstepping constant. The driver is set to 1/16 but the code assumes full steps. The motor then turns 16 times slower than requested. The reverse error makes it too fast and it may stall.
- Integer overflow. Computing
rpm * stepsPerRevinintgives garbage for large values. Usefloatorlong. - Division by zero. An RPM of zero makes the formula divide by zero. Handle zero explicitly by not stepping.
- Using
delay()for sub-millisecond intervals.delay()takes whole milliseconds, so fast speeds become impossible.
Exercise
A 200-step motor runs at 1/8 microstepping. You want 150 RPM. What are the steps per revolution, the steps per second, and the interval in microseconds between pulses?
Show answer
Steps per revolution = 200 × 8 = 1600. Steps per second = 150 × 1600 / 60 = 4000. Interval = 1,000,000 / 4000 = 250 µs (or 60,000,000 / (150 × 1600) = 250 µs).
Exercise
Your sketch uses delayMicroseconds(interval - 3) with a 200-step motor at 1/16 microstepping. At 2 RPM, the motor moves at the wrong speed. Why, and what is the fix?
Show answer
At 2 RPM the interval is 60,000,000 / (2 × 3200) = 9375 µs. That is below the 16383 µs limit, so the delay itself is fine. At 1 RPM the interval is 18750 µs, which exceeds the limit and breaks the delay. If the fault appears at 2 RPM, check the microstepping constant instead. For intervals above 16383 µs, switch to the micros() approach, which has no such limit.