Build pathBeginnerA weekend

Build a Line Follower Robot: From Simulator to Real Track

A working line-following robot you understand end to end—from how it reads the line to the PID loop that steers it—built from parts you tuned in simulation first.

Build a Line Follower Robot: From Simulator to Real Track technical schematicSENSOR ERROR

A line follower is the best first robot: it senses, decides, and moves, yet every part of it is simple enough to understand completely. This build path treats it as exactly that—a path. Instead of a parts list and a wiring photo to copy, you unlock one node at a time, learning the mechanism behind each step before you build it.

You start by understanding how a row of infrared (IR) sensors becomes a single steering error, and how a Proportional-Integral-Derivative (PID) loop turns that error into smooth motion. Five sensors are the sweet spot for a first build: three work at low speed but give coarse position, eight give finer resolution at high speed but complicate wiring and calibration. The weighted average that collapses those readings into one signed number—negative when the line is left, positive when it is right—is the only signal the steering loop ever sees, which is why getting that number right matters more than picking a particular board.

That signal then has to become two different wheel speeds. Differential drive means the robot steers by spinning its wheels at different rates, and the tech tree walks you from the raw motor driver through pulse-width modulation (PWM) and the kinematics that turn a single correction into left and right commands. You will also see where odometry fits in: counting wheel motion tells you how far you have travelled, and why that estimate drifts without external reference — a theme that returns in every maze robot.

Before spending anything or risking a miswire, you tune the behaviour in the browser simulator, drawing a track, changing sensor count and spacing, and dragging Proportional, Integral and Derivative gains while watching error, lap time and time-on-track. The simulator is a teaching model, not hardware: it assumes a flat, high-contrast binary track, ideal calibration, no ambient-light change and a perfectly periodic loop with no wheel slip. Gains found here transfer as a close starting point, but you will still calibrate the real array and re-tune on your own tape.

Only then do you wire the real robot: reflectance outputs to analog-capable pins, PWM and direction to the L298N (Logic pins cannot source motor current — the dual H-bridge switches the real current instead), a separate motor supply on its own rail, and a common ground tying every module together. The final node is assembly and iteration on a real taped course, where you transfer the simulator gains and adjust for surface, battery voltage and loop timing. The same structure returns later in the line maze solver — which adds a decision layer on top of this follower — so getting this foundation solid saves time twice.

Follow the tech tree below top to bottom. Each node opens once its prerequisites are done, and your progress is saved on this device, so you can build the robot over a weekend without losing your place.

Bill of materials

Part Qty Approx. cost Notes
Arduino Uno or Nano 1 $5–8 A Nano is the better choice here: it breaks out A6 and A7, so an 8-channel array fits
IR reflectance array 1 $4–10 5 channels to learn on, 8 if you want speed later. Insist on analog output
L298N or TB6612FNG 1 $2–4 TB6612FNG if buying — it keeps ~1.5 V the L298N burns as heat
TT gearmotor 2 $4 The yellow ones. Encoder versions cost $2 more and save a rebuild
2WD chassis with wheels and caster 1 $6–10 Usually sold as a kit with the motors
Battery pack 1 $3–8 6×AA NiMH, or 2×18650 with a protected holder. Not a 9 V block
Jumper wires, tape, switch $5 19 mm matte electrical tape for the track

Total: roughly $30–45. The single most valuable upgrade in that list is motors with encoders — not for this project, which does not need them, but because a line follower almost always becomes a line maze solver, and that one does.

For the track: matte black tape on a matte light floor. Glossy tape reflects like white at the wrong angle, and a glossy floor does the same, which produces a robot that works on one surface and not another for reasons that look like software.

How it all connects

From To Why it matters
Array VCC, GND Arduino 5 V, GND The emitters are the current draw here — 100–200 mA on an 8-channel array
Array OUT1–8 A0–A7 Analog, one channel each. This is why a Nano beats an Uno
Driver PWMA, PWMB D9, D3 Must be PWM-capable pins
Driver direction pins D8, D7, D5, D4 Any digital pins
Driver STBY (TB6612FNG only) D12, driven HIGH Leave it floating and nothing happens at all, silently
Battery + Driver VM / +12V Never the Arduino’s 5 V pin
Battery Driver GND, Arduino GND, array GND One common ground, starred back to a single point

Two failures account for most first-run problems, and neither produces an error message. A missing common ground means the direction signals have no reference, so nothing moves while everything looks correctly wired. Motor current through the Arduino sags the 5 V rail, resets the board, and presents as a software crash.

Build it in milestones

Do not wire the whole robot and then debug it. Each stage below has a test, and a stage that does not pass its test will hide inside the next one.

# Milestone The test A pass looks like
1 Motors turn Drive each wheel forward and back at 200 duty, on blocks Both spin both ways; note the duty at which each starts
2 Deadband measured Ramp duty from 0 in steps of 5 You have two numbers, and they differ — often by 5–10
3 Straight line Drive both wheels forward for 2 m on the floor Under 10 cm of drift; if not, trim one motor in software
4 Sensors read Print all channels over the line and over the floor Every channel’s two readings differ by hundreds of counts
5 Calibration Sweep across the line for 3 s, then print normalised values 0 on floor, near 1000 on the line, on every channel
6 Position Move the robot across the line by hand, print the weighted position Smooth and monotonic — no jumps as the line crosses a sensor boundary
7 P only Drive with proportional gain only, no I or D It follows a gentle curve, weaving a little. Weaving is expected here
8 Add D Raise derivative until the weave damps Smooth tracking on straights and gentle curves
9 Full track Run a closed loop with a tight corner It completes laps without losing the line
10 Line-loss recovery Lift the robot off the line mid-run It turns the way it was already turning, rather than driving straight on

Milestone 6 is the one people skip and should not. If the position signal jumps as the line crosses between two sensors, no control loop can be tuned — the discontinuity looks like a sudden enormous error, and the robot will jerk at exactly that point every lap. A jump there means either the array is not calibrated per channel, or a channel’s threshold is excluding it too early.

What good looks like

Measurement Beginner build Well-tuned
Speed on straights 0.15–0.25 m/s 0.4–0.6 m/s
Weave amplitude on a straight ±15 mm Under ±5 mm
Tightest corner it holds ~30 cm radius ~15 cm radius
Loop rate 50 Hz 200 Hz or better
Laps before losing the line A few Indefinitely, until the battery sags

Loop rate deserves attention because it silently caps everything else. A control loop running at 50 Hz sees the line every 20 ms, and at 0.5 m/s that is 10 mm of travel between decisions. Removing every delay() from the loop and reading only the sensors you use typically takes a first build from 50 Hz to several hundred, and the tracking improves without touching a gain.

Troubleshooting

Symptom Likely cause Fix
Nothing moves at all No common ground, or STBY low Tie all grounds; drive STBY HIGH
Board resets when it starts driving Motor current through the Arduino Separate motor supply; only ground is shared
Drives straight past the line Array not calibrated, or too high Recalibrate on this surface; set ride height to 3–8 mm
Follows, but weaves badly P too high, or D too low Halve P, then raise D
Oscillates faster and faster P far too high Reduce until it settles, then tune from there
Drifts off on straights Motor mismatch Trim in software, then recheck at your running speed
Veers only at low speed Two different stiction thresholds Measure each motor’s deadband and compensate separately
Loses the line on sharp corners Too fast, or array too close to the wheels Slow down; move the array forward for more lead time
Drives away when it loses the line Error set to 0 on line loss Hold the last error instead — zero means “centred”
Works, then degrades over a run Battery sagging, so gains no longer suit Expected; close a speed loop, or accept a shorter run
Fails near a window Ambient infrared Emitter-off subtraction, or shroud the array

Where to take it next

The line follower is deliberately a foundation, and three directions build directly on it.

Add junction detection and it becomes a line maze solver — the same PID loop underneath, with a thin decision layer on top that classifies branches and remembers the route. This is the single best next project, because it reuses everything here and adds one genuinely new idea.

Add encoders and you can hold a commanded speed rather than a commanded duty, which removes the “it degrades as the battery drains” problem entirely and makes the tune stable across a whole run.

Go faster, which is harder than it sounds and teaches the most. Speed exposes every approximation: the loop rate becomes the limit, the array needs to be further forward for lead time, the corners need slowing before they arrive rather than after, and the tyres start to slip. That last one is the wall — see wheel slip and traction for where it sits.

Project roadmap

The build path

Follow the tech tree from parts to a robot that follows a taped line. Each node unlocks when its prerequisites are done, and your progress saves on this device.

0 / 15 done

100%
Build

Wire the robot

45 min

Build

Assemble and run a real track

60 min

Goal

Line follower complete

You built it

Components

Tutorials in this path

Practise before you wire

Tune it in the live simulator

The build path routes through a browser lab. Find gains that follow the track cleanly here, then transfer them to the real robot.

Frequently asked questions

Do I need to buy parts before I start this project?

No. The whole sensing-and-control half of the project runs in the browser simulator, so you can understand how a line follower reads the line and tune its PID gains before buying anything. Only the final build steps—wiring and assembly—need the physical Arduino, IR array, and motor driver.

How long does it take to build a line follower robot?

If you have the parts, a first working line follower is a weekend project: an afternoon to learn the sensing and control and tune it in the simulator, then a few hours to wire the Arduino, IR array, and motor driver and iterate on a taped track. The tuning you did in simulation transfers directly, which removes most of the trial and error.

What is the hardest part of building a line follower?

Almost always the control loop, not the wiring. Getting the robot to follow gentle curves without wobbling means tuning the PID gains for your specific motors, sensor height, and speed. That is exactly why this build path has you tune in the simulator first—so the gains are close before the robot ever touches the floor.

Why does my line follower oscillate around the line?

Oscillation usually means proportional gain is too high, derivative damping is too low, or the robot is moving faster than its sensor update rate can support. Reduce base speed first, then lower Kp about 20% and add a little Kd. On hardware, a long delay() in the loop adds dead time that makes the derivative term ineffective no matter how high you set it — keep the control interval fixed.

How many IR sensors does a line follower need?

Five is a practical starting point: enough to compute a smooth weighted position and see which way the line curves, while keeping wiring and the position calculation easy to inspect. Three sensors work for a slow, wide-line practice track; competition robots often use eight for finer resolution at high speed, but every extra sensor is another calibration and another wire to get right.

How do I calibrate the IR reflectance array?

Before a run, sweep the array slowly across both the black tape and the white board and record each sensor's minimum and maximum reading, then normalize live readings against that range. Calibration matters because sensor height, surface reflectivity and room lighting all shift the raw values, and an uncalibrated array biases the weighted position even when the robot is centred.