Robotics
How to soft start a motor with a PWM ramp
A soft start brings a motor up to speed gradually instead of all at once. On an Arduino, write the PWM speed in small steps with a short wait between them: 0, 5, 10 and so on up to 255, 20 ms apart. Fifty-two small steps feel like one smooth push, the wheels grip instead of spinning, and whatever the robot is carrying stays put.
Why a jump is rough
analogWrite(9, 255) to a stopped motor asks for full speed now. The motor delivers its biggest torque from a standstill, so the robot lurches. If that acceleration is more than the tyres' grip allows, the wheels spin on the floor before they bite. The current surge is hard on gears and batteries, and it can make the supply sag enough to reset the board.
The ramp
void setup() {
for (int speed = 0; speed <= 255; speed = speed + 5) { // 0 -> 255 in fives
analogWrite(9, speed);
analogWrite(10, speed);
delay(20);
}
}
The for loop counts speed from 0 to 255 in fives, writing it to both wheels each time and waiting 20 ms. That's 52 values, counting the 0, so the climb takes just over a second. It lives in setup(), which runs once; when it finishes, loop() simply writes 255 to both wheels on every pass and the robot carries on at full speed.
Step and delay
The step sets how smooth the ramp is; the delay sets how long it takes:
| Step | Delay | Passes | Ramp |
|---|---|---|---|
| 5 | 20 ms | 52 | about 1 s, smooth |
| 5 | 1 ms | 52 | about 52 ms: a jump in disguise |
| 15 | 20 ms | 18 | about 0.36 s |
| 50 | 20 ms | 6 | about 0.12 s, six lumps, ending at 250 |
A ramp of 52 ms is quicker than the wheels can even follow, so it lurches almost as hard as no ramp at all.
Live experiment
Pull away without spilling
The rover pulls away carrying a dish of soup filled to the brim, ramping both wheels from 0 to 255 in steps of 5 with a delay between steps. With no ramp, or delay(1), it lurches and the soup spills; delay(5) sloshes it up to the brim; delay(20) glides away over about a second.
“Soft start” is lesson 4 of 6 in Rover Ridge, Robotics World 1. Free with an account. Lessons unlock in order, so you’ll do the 3 before it first.
Where you'll meet it
Lifts and trains ramp their speed up and down so nobody falls over. Big factory motors have soft starters that do it in hardware, and the speed controllers on drones and electric skateboards limit how fast the throttle can rise for the same reasons: grip, gears and current.
Free lesson
Now build it yourself
Each lesson comes in three short parts: see the idea, try it on a live board, then build it yourself, with quick checks as you go.
“Soft start” is lesson 4 of 6 in Rover Ridge, Robotics World 1. Free with an account. Lessons unlock in order, so you’ll do the 3 before it first.
Or start from the very first lesson, no account neededPractice questions
Three quick questions from the lessons. Pick an answer to check it.
Show the answer
Answer: The wheels try to speed up faster than the grip on the floor allows. Grip has a limit. Ask for too much acceleration at once and the wheels spin instead of pushing.
- Why not “The motor is too weak”? A weak motor would struggle to spin the wheels at all. Slip means the wheels are turning faster than the grip can follow.
- Why not “It doesn't”? Grip has a limit, so asking for full speed at once can spin the wheels, especially on a smooth floor.
- Why not “The battery is too strong”? The slip comes from asking for too much acceleration at once, not from the battery. A ramp fixes it with the same battery.
Show the answer
Answer: Lurches almost as hard as with no ramp: the whole climb takes about 52 ms. 52 steps of 1 ms is about a twentieth of a second, quicker than the wheels can even follow, so it's a jump in disguise and the soup goes over. delay(20) spreads the same 52 steps over about a second.
- Why not “Glides away over about a second”? That's the delay(20) ramp. With delay(1) the same 52 steps are over in about 52 ms.
- Why not “Starts more gently than with delay(20)”? A shorter delay makes the ramp shorter and harsher, not gentler.
- Why not “Never reaches full speed”? The loop still counts all the way to 255, just very quickly.
Show the answer
Answer: Over in about 0.1 s, in six coarse jumps. speed goes 0, 50, … 250, six passes, and six lots of 20 ms is about a tenth of a second. Big steps make a short, lumpy ramp. Small steps and a longer delay make a smooth one.
- Why not “Ten times longer”? Fewer steps with the same delay make the ramp shorter, not longer.
- Why not “The same”? With steps of 50 there are only six passes instead of 52, so it's over far sooner.
- Why not “Smoother”? Bigger steps make bigger jumps in speed, so the ramp is lumpier, not smoother.
“Soft start” is lesson 4 of 6 in Rover Ridge, Robotics World 1. Free with an account. Lessons unlock in order, so you’ll do the 3 before it first.
Questions people ask
How long should a motor soft start take?
Long enough that the wheels never slip or the load never jolts, short enough not to feel sluggish. For a small robot, about a second is a good start. The time is the number of steps times the delay: 52 steps of 20 ms is about 1 s.
Why does my motor jerk when it starts?
Writing 255 to a stopped motor asks for full speed at once. The motor makes its biggest torque from a standstill, so the robot lurches and the wheels can spin before they grip. The surge of current can also make the supply dip and reset the board.
Can I ramp the speed without delay()?
Yes. Keep the speed in a variable and, each time millis() shows another 20 ms has passed, add 5 until it reaches 255. loop() keeps running between steps, so sensors are still read during the ramp.
Should a robot ramp down to stop as well?
Often, yes. A soft stop keeps a load from sliding forward and is easier on gears. An emergency stop is the exception: then you want the wheels off at once.
Related topics
- How does PWM work?How a pin that's only on or off dims an LED: duty cycle and analogWrite's 0–255.
- MOSFET as a switch, explainedA gate voltage, not a current, opens the channel: threshold, R_DS(on) and the flyback diode.
- How a two-wheeled robot steers and sets its speedHow a two-wheeled robot turns with no steering wheel, and how PWM sets each motor's speed.
- millis() vs delay() on ArduinoWhy delay() freezes your sketch, and how millis() runs several jobs at once.