A wobbling line follower usually has a proportional gain that is too high. Lower Kp first, then add some Kd to damp the swing.
What the message means
The robot corrects in proportion to the error. If the correction is too strong, it overshoots and swings back, and the swing grows.
Fix it in this order
- Set Ki and Kd to zero and start with a small Kp.
- Raise Kp until the robot follows the line but starts to wobble, then reduce it a little.
- Add Kd slowly to damp the wobble.
- Keep Ki zero unless the robot has a steady offset.
- Lower the base speed while you tune.
- Change one value at a time and write down each test.
Why it happens
In proportional control the correction is the error times a gain. A high gain makes the robot over-correct, so it crosses the line, errors the other way and corrects again, and the swing can grow. The robot also reacts with a delay, which makes the overshoot worse.
The derivative term reacts to how fast the error changes and so damps the swing. Tune one term at a time, starting with a small proportional gain, and expect different values on a different robot, speed or track.
If you see this, do this
| You see | Do this |
|---|---|
| Swings wider and wider | Kp too high or speed too high. |
| Slow to react on curves | Raise Kp or lower speed. |
Related
PID for a line follower, sensors not detecting the line.
Last reviewed: November 2026. These are the usual causes and the order that finds them fastest. Test on your own board; clones and cables differ.
Why this happens
The usual line follower control law turns a line position error into a steering correction. The correction is Kp times the error, plus Kd times the change in the error since the last reading. The correction is added to one wheel’s speed and subtracted from the other’s. A high Kp makes the robot correct hard, but the robot reacts with a delay: sensor delay, motor response and the distance the sensors sit ahead of the axle. By the time the robot has turned, it has already gone too far, so it must correct back, and the swing can grow.
Kd reacts to the speed of the error change, which gives the controller a way to ease off as the robot is already swinging back toward the line. The trade-off is noise: a small flicker in the sensor gets magnified by a large Kd. A steady speed and a steady loop time matter too, since the effect of the numbers depends on how often they are applied.
How to confirm the cause
- Print or plot the error and the correction in the serial plotter. A wave with steady height means Kp is near the limit, a wave that grows means Kp is too high, and a wave that dies out means the gains are fine.
- Print the loop time with micros(). If it varies a lot, the derivative term will be noisy and unreliable.
- Halve the base speed. If the wobble disappears, the gains are acceptable and the speed was the issue.
- Check how far the sensors sit ahead of the wheels. A sensor array far forward sees problems early but also makes steering more sensitive.
- Test with Kd set to 0 and then add small amounts. Note the result of each step in a list.
Worked example
The control law with example numbers. Five sensors are given positions -2, -1, 0, 1, 2:
// Line under the two right-hand sensors (+1 and +2): error = 1.5
// Kp = 20, Kd = 10, lastE = 1.0, base = 120
float e = position(); // your function, -2.0 to +2.0
float correction = Kp * e + Kd * (e - lastE); // 20*1.5 + 10*(1.5-1.0) = 35
lastE = e;
setMotors(base - correction, base + correction); // 85 and 155
// Flip the signs if your robot steers away from the line.Common mistakes
- Tuning at full speed, where everything happens too quickly to see.
- Using a large Kd with noisy sensors, which makes the motors twitch.
- Changing Kp, Kd and the speed at the same time.
- Adding the integral term at the start, which can wind up and produce slow, large swings.
- Ignoring a very short or irregular loop time.
If this did not fix it
- Motor dead zone: below a certain PWM the motors do not move, so small corrections do nothing and large ones overshoot.
- Sensor calibration: unevenly calibrated sensors shift the position estimate.
- Battery voltage falling during a run changes the speed that a given PWM produces.