
Your robot sits at the edge of a curve of black tape. You flip the switch, the motors whine, and for about two seconds it looks like it might work — then it veers off the line and stalls against a chair leg. That failure is normal, and it is also the most useful thing that will happen today. Building a line follower is less about clever code than about getting a handful of simple parts to agree with each other. Here is the whole build, from the parts bin to a robot that finishes a lap.
Gather the Parts Before You Solder Anything
You do not need an expensive kit. A first line follower is a small, slow, deliberately unambitious machine, and cheap parts make better teachers because you are not afraid to break them. Assemble roughly this:
- Two geared DC motors with wheels, plus a castor wheel or ball bearing for the front or rear
- A motor driver module — an L298N or a TB6612FNG are both easy to find and easy to wire
- A microcontroller board. An Arduino Uno or Nano is ideal; a Raspberry Pi Pico works too
- Three to five infrared reflectance sensors, or a ready-made array on a single board
- A battery pack: four AA cells (about 6V) or a two-cell lithium pack with a regulator
- A chassis. Acrylic, a small plastic project box, or stiff cardboard for the first attempt
- Jumper wires, a breadboard, zip ties, and a small screwdriver
For the track itself, black electrical tape on a light floor is hard to beat. Matte white paper or pale tiles reflect best; glossy surfaces and direct sunlight both confuse infrared sensors badly.
How a Line Follower Actually Thinks
Each sensor is an infrared LED paired with a phototransistor. The LED shines downward. White tape reflects light back into the transistor; black tape absorbs it and almost nothing returns. The sensor therefore reports "bright" or "dark" — and the boundary between those two states is a number you choose, called the threshold.
With one sensor you can only ask "am I on the line?" With five you can ask "where is the line?" That second question is the whole trick. Give each sensor a position value — say −2, −1, 0, +1, +2 from left to right — and take a weighted average of the sensors that see black. You now have a number that says how far off-centre the robot is. That number drives everything else.
Wiring the Sensors
Most modules have three pins: VCC, GND, and OUT. Power them from the microcontroller's 5V rail and connect all grounds together. If you bought digital modules with a small trim potentiometer, you will get a clean HIGH or LOW — convenient, but you must adjust the pot by hand for your particular floor. Analog modules give you a raw number (0 to 1023 on most Arduino boards) and much more control, because you can calibrate in software.
Whichever you use, mount the sensors on a bar in front of the wheels, about 5 mm above the ground and spaced roughly 10 mm apart so they cover the tape plus a little margin. A sensor array that is too high washes out; too low and it drags on bumps. Zip ties make this adjustment painless while you are still experimenting.
Wiring the Motors and Driver
Never connect a motor directly to a microcontroller pin. Motors pull far more current than a pin can supply, and the voltage spikes they generate will eventually kill your board. The driver module sits in between and does the heavy lifting.
Wire the battery positive to the driver's motor supply, battery negative to the driver's ground, and the microcontroller's ground to the same point. That shared ground is the single most common mistake in first builds: without it, the driver receives signals it cannot interpret and the robot behaves randomly. From the microcontroller, run two control wires per motor. On the L298N, one pin sets direction and a second pin (a PWM-capable one) sets speed.
Before you write a single line of logic, flash a test sketch that runs the left motor forward for two seconds, stops, then does the same for the right. If a wheel spins the wrong way, swap its two output wires. Fixing this now saves an hour of confused debugging later.
Writing the Control Loop
The software is short. Everything happens inside a loop that repeats every few milliseconds:
- Read all sensor values.
- Work out the line position as a weighted average of the sensors seeing black.
- Calculate the error: error = position − centre. Zero means perfectly centred.
- Set the left motor speed to base + (k × error) and the right to base − (k × error).
- Clamp both values to the range your driver accepts, usually 0 to 255.
- Repeat.
That constant k is the gain. If it is large, the robot reacts hard to small deviations. If it is small, the robot drifts. Start with a gentle value, keep the base speed low, and raise k only when the robot fails to hold a curve.
Avoid delay() in the loop. Every millisecond spent waiting is a millisecond the robot is not correcting, and at speed a curve arrives fast.
Tuning: The Part That Takes the Longest
Expect to spend more time tuning than building. Here is what the common symptoms mean.
It weaves down a straight line
The gain is too high. Halve it and try again. If it still oscillates, average two consecutive sensor readings so a single noisy sample cannot throw the steering off.
It leaves the line on every corner
Either the base speed is too high or the gain is too low. Slow down first — a slow robot that finishes is worth more than a fast one that parks in a wall. On tight corners, some builders also drop the speed when the outermost sensor sees black.
It works on the desk but misbehaves on the floor
This is almost always voltage. Motors draw more current under load, the battery sags, and a nearly flat pack changes how the sensors read. Recalibrate your threshold on the actual track, with a fresh battery.
One sensor never changes
Check its height and its wiring. Tall sensors lose contrast, and a loose jumper on a moving chassis fails intermittently, which is the worst kind of fault to find by guessing.
Where to Go After the First Lap
Once the robot completes a circuit without help, tidy the wiring — loose leads cause more faults than bad code. Then pick one improvement and measure it: add a software filter to smooth sensor readings, add two more sensors for finer position resolution, or replace the simple proportional steering with a full PID controller if you want to chase speed. Serial logging is worth the effort too; plotting the error value over time turns tuning from guesswork into something you can see.
Keep the first version working. Save that sketch, photograph the wiring, and label the battery pack. When the second build misbehaves at midnight, being able to compare it against a machine that already works is the fastest debugging tool you own. Try a track with a sharp angle and a crossing, then adjust one thing at a time and note what changed.
Photo: JOEBU-ART / Pixabay


