You're offline — cached pages and worlds still work
Drishti Innovations logo
Drishti Innovations

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 u(t)u(t) from the error e(t)e(t) (deviation from the line center):

u(t)=Kpe(t)+Ki0te(τ)dτ+Kdde(t)dtu(t) = K_p\,e(t) + K_i\int_0^t e(\tau)\,d\tau + K_d\,\frac{de(t)}{dt}

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.

TermSymbolWhat it does
ProportionalKpK_pCorrects in proportion to current error — bigger error, bigger turn
IntegralKiK_iCorrects for small, persistent error the P-term can't eliminate (e.g. a steady drift)
DerivativeKdK_dReacts to how fast the error is changing — dampens overshoot and oscillation

How It Works (Step by Step)

  1. 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).
  2. Compute error = center - linePosition.
  3. Start tuning with only KpK_p (set Ki=Kd=0K_i = K_d = 0). Increase KpK_p until the robot follows the line but starts to oscillate (wobble side to side).
  4. Add KdK_d to damp that oscillation — it "predicts" the error and slows the correction as the robot approaches the line, reducing overshoot.
  5. Add a small KiK_i only if the robot settles slightly off-center on straight sections (steady-state error) — too much KiK_i causes slow, sweeping oscillations.
  6. 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 KpK_p.
  • Fast oscillation / wobble → increase KdK_d or decrease KpK_p.
  • Slow drift never fully corrected → increase KiK_i slightly.
  • Slow, wide oscillation → decrease KiK_i.

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

  1. 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?
  2. 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