Sub topic 12
Acceleration ramps
Assume you know how a STEP/DIR driver works, how to generate step pulses, and how to calculate steps per revolution. This chapter explains why a stepper motor needs a speed ramp and how to add one with AccelStepper.
Why instant speed changes cause missed steps
A stepper motor produces torque by energizing its coils in sequence. At rest, the rotor can usually follow a low step rate. If you immediately command a high step rate, the rotor may not have enough torque to move to each new magnetic position. It can vibrate, stall, or lose position. This is called a missed step.
An acceleration ramp changes the step rate gradually:
Acceleration is measured in steps per second per second. An acceleration of 500 means the commanded speed increases by 500 steps per second every second. If the motor starts at 0 steps per second, it reaches 1000 steps per second after two seconds, if the move is long enough.
The motor needs distance to accelerate and decelerate. A short move may never reach the requested maximum speed because the library must begin braking before the destination.
Using AccelStepper
The AccelStepper library can control a driver that has separate STEP and DIR inputs. setMaxSpeed() sets the speed limit in steps per second. setAcceleration() sets the ramp rate. moveTo() selects an absolute target position.
The run() function must be called repeatedly. Each call checks whether another step is due and updates the ramp. Do not place a long delay() in the loop, or the library cannot generate pulses at the correct times.
#include <AccelStepper.h>
const int STEP_PIN = 3;
const int DIR_PIN = 4;
AccelStepper stepper(AccelStepper::DRIVER, STEP_PIN, DIR_PIN);
void setup() {
stepper.setMaxSpeed(1000); // steps per second
stepper.setAcceleration(500); // steps per second squared
stepper.moveTo(1600); // move 1600 steps forward
}
void loop() {
if (stepper.distanceToGo() == 0) {
stepper.moveTo(-stepper.currentPosition());
}
stepper.run();
}This example moves between two positions. The motor accelerates from rest, travels at up to 1000 steps per second, decelerates before the target, and then reverses. With a 200-step motor in full-step mode, 1600 steps equals eight revolutions. With 1/8 microstepping, the same command equals one revolution if the driver receives 1600 pulses per revolution.
Choosing ramp values
Start with conservative values. Verify the motor can complete the move, then increase speed or acceleration one setting at a time.
| Symptom | Likely adjustment |
|---|---|
| Motor stalls at startup | Lower acceleration |
| Motor stalls near top speed | Lower maximum speed |
| Motor stops before reaching top speed | Use a longer move or lower the target speed |
| Motion is reliable but slow | Increase one value gradually |
Acceleration does not correct incorrect coil wiring, an unsuitable current limit, or an overloaded motor. Check those causes first if the motor only hums or vibrates.
Exercise
A motor uses 200 full steps per revolution and a driver is set to 1/4 microstepping. How many STEP pulses are needed for one revolution?
Show answer
800 pulses, because 200 × 4 = 800.