Line following is the foundation of autonomous robotics and a core skill for FIRST LEGO League and World Robot Olympiad. This comprehensive tutorial covers mechanical design, sensor calibration, proportional control, PID tuning, and competition strategies—everything needed to build a fast, reliable line follower.

Table of Contents

How Line Following Works

[CONTENT: Explain the physics - color sensor detects reflected light, darker surfaces reflect less light, edge detection strategy]

Edge Following vs Center Following

Strategy Pros Cons Best For
Edge Following More reliable, handles gaps Slightly slower on curves Competition missions
Center Following Faster on straight lines Loses line at gaps Continuous lines only

Building the Robot

[CONTENT: Mechanical design considerations - wheelbase width, sensor placement height, motor mounting, weight distribution]

Critical Design Factors

  • Sensor Height: 8-12mm above surface for optimal reading
  • Sensor Position: Centered between wheels for edge following
  • Wheelbase: Narrower = faster turns, wider = more stable
  • Weight: Low center of gravity prevents tipping on turns

[Include diagram/photo of optimal robot design]

Sensor Calibration

[CONTENT: How to calibrate color sensor for accurate line detection]

# [COMPLETE CALIBRATION CODE] from mindstorms import ColorSensor import time
sensor = ColorSensor('C')
print("Place sensor over BLACK line, then press button...") time.sleep(2) black_value = sensor.get_reflected_light() print(f"Black value: {black_value}")
print("Place sensor over WHITE surface, then press button...") time.sleep(2) white_value = sensor.get_reflected_light() print(f"White value: {white_value}")
# Calculate target (edge of line) target = (black_value + white_value) / 2 print(f"Target value: {target}") print(f"Use this as TARGET in your line follower code!") 

Basic Line Follower (On/Off Control)

[CONTENT: Simple bang-bang control - if dark turn left, if light turn right]

# [COMPLETE BASIC LINE FOLLOWER CODE] # Simple on/off control (jerky but functional) 

Result: Robot zigzags along line. Works but slow and jerky.

Proportional Control (Smooth)

[CONTENT: Introduce proportional steering - steering amount proportional to error]

# [COMPLETE PROPORTIONAL CONTROLLER CODE] # Smooth steering based on distance from target # Include KP tuning parameter 

Result: Much smoother! Robot makes gentle corrections instead of sharp turns.

PID Control (Competition-Grade)

[CONTENT: Full PID implementation with all three terms explained]

# [COMPLETE PID LINE FOLLOWER CODE] # Professional-grade controller with P, I, and D terms # Include all tuning parameters 

PID Tuning Guide

[CONTENT: Step-by-step process for tuning KP, KI, KD values]

The Tuning Process

  1. Step 1: Set KI and KD to 0. Increase KP until oscillation starts
  2. Step 2: Reduce KP to 60-70% of oscillation threshold
  3. Step 3: Add KD to dampen remaining oscillation (start with KD = KP/8)
  4. Step 4: Add tiny KI to eliminate steady-state error (KI = 0.01 to start)
  5. Step 5: Test at different speeds and adjust

Typical Values

Robot Speed KP KI KD
Slow (20%) 0.8-1.2 0.01-0.05 0.3-0.5
Medium (40%) 1.2-1.8 0.005-0.02 0.5-0.8
Fast (60%+) 1.5-2.5 0.001-0.01 0.8-1.5

Troubleshooting Common Issues

Problem: Robot Overshoots Turns

Solution: Increase KD (derivative term) to predict and slow down before overshoot


Problem: Robot Oscillates/Wobbles

Solution: Reduce KP, or increase KD for dampening


Problem: Robot Drifts Off Over Time

Solution: Increase KI (integral term) to correct accumulated error


Problem: Robot Loses Line at Gaps

Solution: Add "keep last direction" logic when sensor reads white


Problem: Different Performance on Curves vs Straights

Solution: Use adaptive PID with lower gains on sharp curves


Competition Tips


  • Practice on Different Line Types: Thick/thin, black/white, tape/printed
  • Test Battery Levels: Performance changes as battery drains - account for this
  • Use Position Control: runfordegrees instead of runatspeed for consistency
  • Have Backup Gains: Test multiple PID sets and switch based on course
  • Smooth Acceleration: Ramp up speed to prevent wheel slip at start
  • Lighting Calibration: Recalibrate on competition day (lighting differs)

Get MINDSTORMS for Line Following Get SPIKE Prime

Video Tutorial

[CONTENT: Embed or link to video demonstration of building and programming]


Competition Success Stories

[CONTENT: Examples of teams that won with optimized line followers, lessons learned]