Tutorial 03 thumbnail

WolfieWeb Robotics Academy

Tutorial 03 — Line Following (IR Sensors + PID)

Make your robot follow a line — smoothly

This tutorial upgrades your robot car into a real line follower using an IR reflectance sensor array. You’ll calibrate sensors, verify wiring, then run a PID loop that keeps the robot centered on a black tape line.

IR reflectance sensors Calibration PID control loop Python (RPi)
Line following tutorial hero image

What you’ll build

  • Robot reads a line using 3–5 IR sensors mounted near the floor
  • Robot estimates “line position” (left/center/right)
  • PID controller adjusts left/right motor speed to stay centered

Parts

  • IR reflectance sensors: 3x to 5x (or a ready-made array)
  • Black electrical tape or vinyl tape for the track
  • Jumper wires + mounting screws/zip ties
  • Your existing motor driver + Raspberry Pi (from Tutorial 01)

Tip: Start with 3 sensors (L/C/R). It’s simpler and works great.

Wiring overview (generic)

Wiring diagram:

Line sensor wiring to Raspberry Pi GPIO

This diagram shows a typical 3-sensor setup (LEFT / CENTER / RIGHT) wired to Raspberry Pi GPIO. If you use a 5-sensor array, wire the extra sensors to free GPIO pins and update the pin list in code/calibrate.py and code/line_following.py.

Most IR line sensors have: VCC, GND, and OUT (digital) or AOUT (analog).

  • VCC → 3.3V (preferred) or 5V (only if the OUT is 3.3V-safe)
  • GND → Pi GND
  • OUT pins → Pi GPIO inputs (use pull-ups/downs as needed)

Important: If your sensor outputs 5V logic, level-shift to 3.3V before feeding the Pi.

Before you run line following

  • Run sensor test (prints which sensors see the line)
  • Run motor test (verify left/right directions match your code)
  • Mount sensors low (3–8mm from floor) and centered
  • Use a wide line at first (18–25mm)

Python code (starter)

Folder: code/ (included in this package)

python3 calibrate.py python3 test_sensors.py python3 line_following.py

This is a clean starter that assumes you already have motor control from Tutorial 01. Replace the placeholder motor driver class with your actual pins/driver.

Sensor-specific wiring (show both options)

Option A — TCRT5000 digital modules (3 sensors):

TCRT5000 digital sensor wiring to Raspberry Pi

Use this if your sensors have a digital OUT pin. Make sure the OUT signal is 3.3V-safe for the Pi.


Option B — Analog line sensor array (QTR / similar) via MCP3008:

Analog line sensor array wiring using MCP3008 ADC

Use this if your sensor array outputs analog values. You’ll read it through an MCP3008 ADC over SPI. If you tell me the exact array model, I’ll adapt the code to match it.

Tutorials in Video Format

IR Reflectance Sensor Basics: Explains how IR sensors detect black vs white surfaces, how reflectance works, and why these sensors are ideal for line-following robots.

PID for Robots: Walks through proportional and derivative tuning for smooth line following, showing how PID stabilizes steering in real robots.

Full Build Walkthrough: Shows sensor mounting, wiring, code logic, testing, and real-world troubleshooting for a complete line-following robot.

Add these to your page once you pick the exact videos you want:

  • How IR reflectance sensors work (line following basics)
  • PID explained for robots (practical tuning)
  • Example build walkthrough (mounting sensors + wiring)

If you send me the 3 YouTube links you want, I’ll drop them in with captions and timestamps.