Sub topic 12
Acceleration ramps
This chapter assumes you can already send STEP and DIR pulses at a fixed rate and convert RPM to a step interval. It also assumes you know that a stepper's torque falls as speed rises. Here you will learn why a motor cannot jump straight to its target speed, how a linear ramp is calculated, and how to tune and implement one.
Why a sudden start fails
An open-loop stepper has no feedback. The driver advances the magnetic field one step per pulse, and the rotor has to keep up. Three things work against it when you start abruptly:
- The rotor and load have inertia. Reaching a given speed takes torque, and the motor only has so much of it.
- Available torque drops as step rate rises, because coil current cannot build fully in the shorter time between steps.
- If the field moves ahead of the rotor by too much, the rotor loses synchronization. The motor hums, stutters, or stops. This is a missed step, and the controller never finds out.
Every motor has a speed it can start from without ramping, often called the pull-in or start speed. Above that speed it can only be reached by accelerating gradually. Stopping has the same problem in reverse. Cutting the pulses at high speed lets the load's momentum carry the rotor past the field.
The ramp model
A ramp raises the step rate steadily. With constant acceleration a (in steps per second squared) starting from speed v0 (steps per second), the speed after n steps is:
v(n) = sqrt(v0^2 + 2 * a * n)This follows from v^2 = v0^2 + 2as, with distance measured in steps. The delay before the next pulse is 1 / v(n). As n grows the delay shrinks, which is the ramp.
You can compute how long a ramp is before you run it:
- Steps to accelerate from
v0tovmax:(vmax^2 - v0^2) / (2 * a) - Time to do it:
(vmax - v0) / a
A trapezoidal profile combines three phases. The motor accelerates, cruises at vmax, then decelerates using the same formula run backward. If the move is too short to reach vmax, the cruise phase disappears and the profile becomes a triangle.
Choosing the parameters
Three numbers define the ramp.
| Parameter | Meaning | How to find it |
|---|---|---|
| Start speed `v0` | Speed the motor can begin at with no ramp | Start low (100 to 300 steps/s at full-step equivalent) and raise it until starts become unreliable, then back off |
| Acceleration `a` | How fast the step rate rises | Raise it with your real load attached until the motor stalls mid-ramp, then use about half of that value |
| Maximum speed `vmax` | Cruise step rate | Limited by supply voltage, motor inductance, and load. Find the point where it stalls at cruise, then back off 20 to 30 percent |
Test with the real mechanism attached. A bare shaft accelerates far faster than one carrying a lead screw, a belt, or a flywheel. The margin matters because friction, supply voltage, and temperature change over time.
Acceleration in steps per second squared depends on your step unit. Switching from 1/8 to 1/16 microstepping doubles the number of steps per revolution. The same value of a now produces half the physical acceleration. If you change the microstepping setting, scale v0, vmax, and a by the same factor to keep the motion identical.
A hand-written ramp
This sketch moves a fixed number of steps with a trapezoidal profile and no library. It assumes a STEP/DIR driver on pins 3 and 4, a 200-step motor, and 1/16 microstepping. That makes 3200 steps per revolution, so 6400 steps is two turns and 2000 steps/s is 37.5 RPM.
const int STEP_PIN = 3;
const int DIR_PIN = 4;
const float START_SPEED = 200.0f; // steps/s
const float MAX_SPEED = 2000.0f; // steps/s
const float ACCEL = 4000.0f; // steps/s^2
const long MOVE_STEPS = 6400;
void moveSteps(long total) {
unsigned long nextStep = micros();
for (long i = 0; i < total; i++) {
// Wait until this step is due (safe across micros() rollover).
while ((long)(micros() - nextStep) < 0) { }
digitalWrite(STEP_PIN, HIGH);
delayMicroseconds(3); // pulse width above the driver minimum
digitalWrite(STEP_PIN, LOW);
// Speed allowed by distance from the start and distance to the end.
float fromStart = sqrt(START_SPEED * START_SPEED + 2.0f * ACCEL * i);
float toEnd = sqrt(START_SPEED * START_SPEED + 2.0f * ACCEL * (total - 1 - i));
float speed = min(MAX_SPEED, min(fromStart, toEnd));
nextStep += (unsigned long)(1000000.0f / speed);
}
}
void setup() {
pinMode(STEP_PIN, OUTPUT);
pinMode(DIR_PIN, OUTPUT);
digitalWrite(DIR_PIN, HIGH);
delay(500);
moveSteps(MOVE_STEPS);
}
void loop() {
// Move once, then idle.
}Taking the smaller of the two speeds produces the whole trapezoid with one formula. Near the start, fromStart is the limit. In the middle, MAX_SPEED is. Near the end, toEnd is. For a short move, the first and last limits meet and you get a triangle automatically.
Why it schedules by absolute time
The code computes nextStep from the previous target rather than calling delayMicroseconds(interval) after each pulse. Two square roots and a division on an 8-bit AVR board take on the order of 100 to 200 microseconds. With a fixed delay, that overhead is added to every period, so the real speed is lower than planned, and the error grows at high step rates. With absolute scheduling, the math runs while the interval is already elapsing. This only works while the calculation takes less than the shortest interval. At 2000 steps/s the interval is 500 µs, which is fine. At 20000 steps/s it is 50 µs, which is not.
Limits of this approach
- It blocks.
moveStepsholds the processor until the move ends, so you cannot read a button or a serial port during motion. A non-blocking design checks the clock once perloop()pass and emits at most one step per pass. That is what the next chapter's library does. - Arduino timing has a floor.
micros()on a 16 MHz AVR advances in 4 µs increments, and the loop adds overhead. Above roughly 10,000 to 15,000 steps/s on an Uno-class board, pulse spacing becomes uneven. Uneven spacing is audible and costs torque. - Linear ramps are not the only choice. A trapezoid changes acceleration abruptly at the corners, which produces a jolt and can excite mechanical vibration. An S-curve profile ramps the acceleration itself and is smoother, but it is more code and rarely needed unless the load is fragile or a long, flexible belt resonates.
- Resonance still exists. Most motors have a speed band, often a few hundred steps/s in full-step mode, where vibration is strong. A ramp passes through that band quickly, which helps. Cruising inside it does not.
Deceleration deserves the same care
Deceleration is usually the more dangerous half. The load's kinetic energy has to be absorbed by the motor's holding torque, and a heavy load can overrun if the decel rate is too steep. Using the same value for a on both ends is a safe default. If your load is heavy and friction is low, test deceleration separately and consider a gentler rate than you use for acceleration.
Also remember that stopping a move midway, for example when a limit switch trips, should ramp down rather than cut pulses. For an emergency stop, a hard cut is acceptable, but treat the position as lost.
Exercise
A motor is set to 1/16 microstepping. You want it to ramp from 200 steps/s to 1500 steps/s at 3000 steps/s squared. How many steps and how much time does the ramp take? You then change to 1/8 microstepping and want the same physical acceleration and speeds. What values do you use?
Show answer
Steps = (1500^2 - 200^2) / (2 * 3000) = (2,250,000 - 40,000) / 6000, about 368 steps. Time = (1500 - 200) / 3000, about 0.43 s. At 1/8 microstepping there are half as many steps per revolution, so halve every value: start speed 100 steps/s, maximum 750 steps/s, acceleration 1500 steps/s squared.
Exercise
In the hand-written sketch, why does a 300-step move never reach MAX_SPEED?
Show answer
Reaching 2000 steps/s from 200 steps/s needs about 495 steps of acceleration and 495 steps of deceleration, which is 990 steps. A 300-step move ends before that, so the smaller of fromStart and toEnd limits the speed throughout. The profile is a triangle with a peak near sqrt(200^2 + 2 4000 150), about 1100 steps/s.