Arduino Line Follower with Obstacle Avoidance
Level 16 · Level 16 — Around It · 40 minutes
A line to follow, and three things parked on it.

Preview the project free; a course seat is required to run and submit. No hardware kit needed.
Your project challenge
Drive the track without hitting a barrier. Keep Level 10's five-eye follower. Add the HC-SR04 ultrasonic eye: - VCC to 5V, GND to GND - TRIG to A0, ECHO to A1 Leave the line to pass a barrier, either side, then find it. 3000 mm of the 6331 mm lap passes, with 35% of the run on the line.
Components you’ll use
- Arduino Uno
- L298N Motor Driver
- Battery
- DC Gear Motor × 2
- 5-Channel IR Line Array
- HC-SR04 Ultrasonic Distance Sensor
What you'll build
A line follower that leaves the tape on purpose, then finds it again.
How it works
- Three barriers stand square across the tape: 300 mm wide, 150 mm either side.
- The thing telling you where to go is now telling you to drive into something.
- So the sketch changes job: FOLLOWING, TURNING, PASSING, SKIRTING.
- Pivot on the spot, or your shoulder clips the thing as you curve away.
- Your one eye points forward. Turned away, it sees nothing at all.
The key idea
One variable holds which job you are on. That is the whole lesson. Memory is not new: Level 10's five-eye sketch kept lastError, Level 13 a map of the room. Those remember readings. This remembers a job. "I see no line" means two opposite things: following, you lost it; passing, you left it on purpose.
2 x 102 mm/s / 120 mm between the wheels = 1.7 rad/s. A quarter turn is about 900 ms.
Blocked: under 22 cm. Clear: past 40 cm.
⚠ Watch out
Almost everyone re-finds the line they never left. Arc back too soon and the tape is beside you, barrier not passed. You rejoin and drive into it. Forever.
The interactive editor includes the wiring guide, code editor and simulation controls.