Learning Goals 5 min
The classic robotics demo: a chassis that follows a black line on a white floor. Today you wire the sensors and write a bang-bang follower; tomorrow we replace it with a PID controller. By the end of this lesson you will:
- Wire 3 × IR reflectance sensors (TCRT5000) or a QTR-3RC sensor bar under the chassis.
- Calibrate the sensors for "line" vs "background" thresholds.
- Implement a 3-state bang-bang follower (drive straight / turn left / turn right) on the L03-10 chassis.
Warm-Up 10 min
Hardware:
- L03-10 2WD chassis.
- 3 × TCRT5000 modules (each has IR LED + phototransistor + on-board comparator outputting digital HIGH/LOW). Or a QTR-3RC sensor bar (three sensors on one PCB).
- Black electrical tape on a white floor → makes the track.
How IR reflectance works
The IR LED shines down. The phototransistor measures how much bounces back. White paper reflects a lot; black tape reflects almost nothing. The comparator on each TCRT5000 module flips its digital output:
- Over white floor → high reflection → digital LOW (logic varies by module; check yours).
- Over black tape → low reflection → digital HIGH.
Adjust the small potentiometer on each module to set the threshold.
New Concept · 3-sensor bang-bang follower 25 min
Sensor placement
Mount the 3 sensors in a row under the front of the chassis, spaced ~1 cm apart. The middle sensor sits over the line when the chassis is perfectly on track. The outer two sense when you've drifted left or right.
| Left sensor | Middle sensor | Right sensor | Interpretation |
|---|---|---|---|
| 0 | 1 | 0 | On line — drive straight |
| 1 | 0 | 0 | Line is on the left — turn left |
| 0 | 0 | 1 | Line is on the right — turn right |
| 1 | 1 | 0 | Line on the left half — turn gently left |
| 0 | 1 | 1 | Line on the right half — turn gently right |
| 0 | 0 | 0 | Lost the line — stop or search |
| 1 | 1 | 1 | All on line (intersection / very thick) — drive straight |
Wiring
| Sensor | UNO pin |
|---|---|
| Left D-out | D11 |
| Middle D-out | D12 |
| Right D-out | D13 |
| VCC, GND | 5V, GND |
If you're using the L03-10 chassis with motors already on D4–D9 / D5–D6, pick free digital pins as above.
The bang-bang sketch
#include "motor.h"
const int SENSOR_LEFT = 11;
const int SENSOR_MIDDLE = 12;
const int SENSOR_RIGHT = 13;
const Motor motorL = {9, 8, 5};
const Motor motorR = {7, 6, 3};
const int BASE_SPEED = 150;
const int TURN_SPEED = 120;
const int SHARP_SPEED = 180;
void setup() {
Serial.begin(9600);
pinMode(SENSOR_LEFT, INPUT);
pinMode(SENSOR_MIDDLE, INPUT);
pinMode(SENSOR_RIGHT, INPUT);
motorInit(motorL);
motorInit(motorR);
}
void loop() {
bool l = digitalRead(SENSOR_LEFT) == HIGH;
bool m = digitalRead(SENSOR_MIDDLE) == HIGH;
bool r = digitalRead(SENSOR_RIGHT) == HIGH;
Serial.print(l); Serial.print(m); Serial.println(r);
if (m && !l && !r) {
// straight
motorSpeed(motorL, BASE_SPEED);
motorSpeed(motorR, BASE_SPEED);
} else if (l && !r) {
// line drifted left -> turn left
motorSpeed(motorL, -TURN_SPEED); // reverse left wheel
motorSpeed(motorR, TURN_SPEED);
} else if (r && !l) {
// line drifted right -> turn right
motorSpeed(motorL, TURN_SPEED);
motorSpeed(motorR, -TURN_SPEED);
} else {
// lost (000) or unclear (111) -> stop briefly
motorSpeed(motorL, 0);
motorSpeed(motorR, 0);
}
}The robot moves forward when on the line, pivots when it drifts off, stops when lost. Crude but it works.
Why bang-bang wobbles
The 3 sensors give only 7 possible states. The robot only ever sees one of three responses: straight / hard-left / hard-right. The chassis swings back and forth across the line — a Z-shaped path even when the line is straight. Watching it is amusing; engineers find it unprofessional.
Tomorrow, PID + an analog reflectance reading (5 sensors or QTR-3A on analog inputs) gives a smooth glide.
Worked Example · Calibrate and run 25 min
Step 1 — make a track
Stick a strip of 19 mm black electrical tape on a white floor / large sheet of paper. Make a closed loop with gentle curves (50 cm radius is fine).
Step 2 — calibrate sensors
Put a calibration sketch in your UNO that prints all 3 sensor digital readings constantly:
void loop() {
Serial.print(digitalRead(SENSOR_LEFT));
Serial.print(digitalRead(SENSOR_MIDDLE));
Serial.println(digitalRead(SENSOR_RIGHT));
delay(100);
}Hold each sensor over white → output should be 0 (or whichever "background" logic level your module uses).
Move it over black → output flips to 1.
If the output doesn't flip cleanly, turn the threshold pot.
Step 3 — mount the sensors on the chassis
Use double-sided tape or a 3D-printed bracket. Sensors point down, 5–10 mm above the floor, 1 cm apart side-to-side, roughly under the front wheel axis.
Step 4 — upload the follower sketch
The §3 sketch.
Step 5 — release on the line
Place the chassis on the line with the middle sensor over the tape. Power on. The chassis drives forward; when it drifts, it pivots until the middle sensor reacquires.
Step 6 — observe the wobble
On a straight section, the chassis still wobbles left-right because there's no smooth transition between "straight" and "turn". This is the bang-bang signature. PID fixes it.
Try It Yourself 15 min
Goal: Slow the BASE_SPEED to 100. Note how much smoother the chassis tracks. What's the trade-off?
Reveal
Slower = smoother (more time for sensor to react before drifting too far). Trade-off: slow demo. Real competitive line-followers run fast and overcome wobble with better algorithms (PID).
Goal: Add a "search" behaviour when the line is lost (000): spin slowly left, then right, until any sensor sees the line again. Resume forward.
Goal: Five sensors instead of three. Far-left and far-right detect sharp turns; middle three handle gentle navigation. Build a 5-state action table.
Mini-Challenge · Run a lap, time it 10 min
- Make a closed loop on the floor.
- Time how long your chassis takes to complete one lap.
- Note where it loses the line / has trouble.
- Save the time. Tomorrow with PID we'll halve it.
Recap 5 min
3-sensor bang-bang follower: 7 input states map to 3 actions (straight / left / right). Wobbles but works. Calibrate thresholds, mount low, control ambient IR. Tomorrow we move to PID + analog reading for smooth tracking.
- TCRT5000
- The classic cheap IR reflectance sensor. IR LED + phototransistor + comparator on a tiny PCB.
- QTR sensor bar
- Pololu's family of sensor bars (QTR-3RC, QTR-8A, etc.) with 3 or 8 sensors. Analog-only versions exist (QTR-8A) for PID use.
- Bang-bang controller
- Two- or three-state controller: fully on or fully off. Simple, jerky.
- Reflectance threshold
- The boundary between "reflects" (white) and "doesn't reflect" (black). Tunable per sensor via on-board pot.
- Ambient IR
- Background infrared light from the sun, halogen, incandescent. Can confuse sensors; run in controlled lighting.
- Search behaviour
- Action taken when the line is lost. Typically: pivot slowly to scan, then resume on reacquisition.
Homework 5 min
- Build and tune the 3-sensor follower. Record a lap time.
- Read ahead to ARD-L04-26 (Tuning a Line Follower). Bring analog reflectance sensors (QTR-8A or 3 × TCRT5000 modules with analog outputs) tomorrow.