Line Follower & PID Tuning
Comprehensive notes, formulas, and practice questions for Line Follower & PID Tuning.
Line Follower & PID Tuning
Line Follower & PID Tuning
Core Idea
A line-following robot uses one or more light sensors (usually IR reflectance sensors) to detect a contrasting line on the floor and steer itself to stay centered on it. The raw sensor reading only tells you how far off-center you are — the error. A PID controller (Proportional-Integral-Derivative) turns that error into a smooth, stable steering correction so the robot doesn't zig-zag off the track or overshoot every curve.
Key Formula / Algorithm
The PID control law computes a correction from the error (deviation from the line center):
In discrete robot code, this is usually written as:
error = setpoint - sensorReading
integral = integral + error
derivative = error - lastError
correction = Kp*error + Ki*integral + Kd*derivative
lastError = error
The correction is added to one motor's base speed and subtracted from the other, steering the robot back onto the line.
| Term | Symbol | What it does |
|---|---|---|
| Proportional | Corrects in proportion to current error — bigger error, bigger turn | |
| Integral | Corrects for small, persistent error the P-term can't eliminate (e.g. a steady drift) | |
| Derivative | Reacts to how fast the error is changing — dampens overshoot and oscillation |
How It Works (Step by Step)
- Read the sensor array and compute a weighted position of the line (e.g. average of sensor indices weighted by how dark each one reads).
- Compute
error = center - linePosition. - Start tuning with only (set ). Increase until the robot follows the line but starts to oscillate (wobble side to side).
- Add to damp that oscillation — it "predicts" the error and slows the correction as the robot approaches the line, reducing overshoot.
- Add a small only if the robot settles slightly off-center on straight sections (steady-state error) — too much causes slow, sweeping oscillations.
- Re-test on both sharp corners and straight sections, adjusting all three gains until motion is smooth at the robot's top speed.
Tuning symptoms cheat-sheet:
- Sluggish, cuts corners → increase .
- Fast oscillation / wobble → increase or decrease .
- Slow drift never fully corrected → increase slightly.
- Slow, wide oscillation → decrease .
Real-World Application
Line followers are the simplest working example of closed-loop feedback control, the same principle behind cruise control, drone stabilization, thermostats, and industrial robot arms holding a position. Warehouse AGVs (automated guided vehicles) at companies like Amazon use an evolved version of line/path following with PID or more advanced controllers to move shelves and pallets precisely along fixed paths.
Quick Check
- Your robot follows the line but wobbles rapidly side to side even on straight sections. Which gain should you adjust first, and in which direction?
- Why is the derivative term useful for reducing overshoot, when it only looks at how fast the error is changing rather than the error itself?
Key Takeaways (TL;DR)
- Core Idea
- Key Formula / Algorithm
- How It Works (Step by Step)
- Real-World Application
Master this topic with Drishti OS
Get unlimited mock tests, AI-powered mentorship, and complete video courses when you join.
Start Free Practice