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.
Basic 5 min
Goal: Slow the BASE_SPEED to 100. Note how much smoother the chassis tracks. What's the trade-off?
1. Think first (on paper, no typing yet):
- Before you change anything: predict what happens to the wobble, and to the time for one lap.
- Which one constant in the Worked Example sets the straight-line speed?
2. Code it. Open your sketch from the Worked Example and change only that one value. Nothing else.
3. Upload and observe. Run the same track at the old speed, then at 100. Watch how far the chassis swings off the line on each wobble.
It works if:
- The chassis still gets round the whole track at the slower speed.
- You can name one thing that got better and one thing that got worse.
Challenge 1 5 min
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.
1. Think first (on paper, no typing yet):
- Which sensor pattern means the line is lost? Where in your if/else chain is that case now, and what does it do?
- The line is most likely on the side the chassis was last turning towards. What would the sketch need to remember to make a good first guess, and when would you update it?
- How will it know it has searched one way for long enough before trying the other way, without using delay()?
2. Code it. Open your sketch from the Worked Example. Change only the lost-line case, plus any new variables it needs. Search at TURN_SPEED or slower.
3. Upload and observe. Pick the chassis up and put it down just beside the line, facing slightly away from it. Try both sides.
It works if:
- When no sensor sees the line, the chassis pivots slowly one way, then the other.
- As soon as any sensor sees the line again, it goes back to normal following.
- It never drives off in a straight line when it is lost.
Challenge 2 5 min
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.
1. Think first (on paper, no typing yet):
- Write the sensor patterns you expect to see with five sensors in a row: centre only, centre plus one inner, inner only, outer only, none. Which ones can really happen?
- Which patterns need a sharp turn at SHARP_SPEED, and which need a gentle one?
- In an if/else chain, which case must be checked first so a sharp bend is never treated as a gentle one?
2. Code it. Wire the two outer sensors to two free digital pins. Write your 5-state action table as comments at the top of your sketch first, then turn each row into one case.
3. Upload and observe. Test on a track with a gentle curve and a 90° bend.
It works if:
- Your action table is written down and every row matches one case in the code.
- On gentle curves only the inner three sensors trigger and the chassis turns gently.
- On the 90° bend an outer sensor fires and it pivots hard, without leaving the line.
Challenge 3 · 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.
Extra Mission 5 min
Part 1 — Design a line-guided helper robot
Picture a small robot that follows a tape line to do one useful job. It might carry a note between two desks. It might deliver a pen from one end of a table to the other. On paper, design your robot.
Your design must include:
- A one-sentence job for the robot and a drawing of its route.
- How many sensors it uses and how far apart they sit.
- What it does at the end of the route, and how it knows it has arrived.
- What it does if it loses the line.
Part 2 — Make it
Build the core of your design on today's 3-sensor follower. Mark the end of the route with a short strip of tape across the line. When all three sensors read 1 (111), the robot must stop and flash an LED three times. Add your Challenge 1 search behaviour for 000.
Bring back next class: your uploaded sketch, a short video of one full run, and a Serial Monitor screenshot showing the sensor states. Also bring analog reflectance sensors for ARD-L04-26.