Building a robot that can follow a line on the floor is one of the most rewarding first projects in home robotics. It sounds simple, and it is — but it also teaches you the core ideas behind almost every automated machine: sensing, calibration, steering, and feedback control. Best of all, you can build one with just two light sensors, two motors, and a handful of cheap parts.
A line follower does one thing well: it stays on a track. That narrow focus is exactly why it is such a good teacher. You will learn how to read sensors, how to compare their values to a threshold, and how to translate that information into motor commands. You will also discover why calibration matters more than raw sensor quality.
Unlike a walking robot or a drone, a line follower has no balance problems. It rolls on wheels, so you can concentrate on the control logic. And because the track is just black tape on a white surface, you can test and improve your robot in minutes.
You do not need a 3D printer or a workshop full of tools. A simple line follower can be built from:
Total cost is often under £20 if you already have a microcontroller. The sensors are the key part: they shine infrared light at the floor and measure how much bounces back. A white surface reflects a lot; a black line absorbs most of it.
Each reflectance sensor gives you a reading that changes with the surface beneath it. Most modules output an analogue voltage, but some have a built-in comparator that gives a digital high or low. For a beginner, analogue is actually easier because you can see the raw numbers and set your own threshold.
Calibration is the first practical skill you will learn. Place the robot so the sensor is over the white floor and note the reading. Then move it over the black line and note that reading. The threshold is halfway between them. Do this for each sensor separately, because no two sensors are identical.
Two details matter here. First, keep the sensors about 3 to 6 mm above the floor. Too high and the reading becomes weak; too low and the sensor may scrape. Second, avoid testing near bright sunlight or flickering fluorescent lights, as they can swamp the infrared signal.
Mount the two sensors at the front of the robot, close together — about 1 to 2 cm apart. They should sit just ahead of the drive wheels so the robot reacts before the wheels reach the line. The motors go at the rear, one on each side. A castor wheel or a smooth plastic skid at the front keeps the chassis level.
Wire the motors to the motor driver, and the driver to your microcontroller. The sensors connect to two analogue input pins. Make sure all grounds are connected together — the microcontroller, the motor driver, and the battery pack must share a common ground. If the motors cause the microcontroller to reset, add a separate battery pack for the motors or a large capacitor across the motor driver’s power pins.
The magic of a line follower is its feedback loop. It constantly compares what the sensors see with what you want: the robot centred on the line. That difference is the error, and the robot uses it to steer.
The simplest version is called bang-bang control. Read both sensors. If both see white, drive both motors forward at the same speed. If the left sensor sees black, the robot has drifted right, so slow the left motor or stop it and keep the right motor running — this turns the robot left. If the right sensor sees black, do the opposite. If both see black, you have reached a crossing or a dead end, so stop or reverse briefly.
That logic is enough for a slow, gentle track. For smoother steering, use proportional control: set each motor’s speed based on how far off-centre the robot is. For example, if the left sensor reads mostly black, reduce the left motor speed by a small amount proportional to the error. This avoids the jerky on-off motion of bang-bang control and lets you drive faster.
Your first runs will almost certainly need adjustment. Start with a simple oval track made from black electrical tape on white paper or a smooth floor. Run the robot at low speed. If it oscillates wildly, slow it down or move the sensors slightly further apart. If it loses the line on curves, reduce speed further or increase the sensor height a little.
Battery voltage matters more than you might expect. As batteries drain, motor speed drops, and your carefully tuned thresholds may no longer work. Recalibrate after a fresh charge, or use a voltage regulator for the sensors. Also check for sensor cross-talk: if the two sensors are too close, the infrared from one can bounce into the other. A small cardboard divider between them fixes this.
Finally, remember that a line follower is a starting point, not a toy. Once you have it working, try adding a third sensor for smoother centre detection, or replace the black tape with a coloured line and swap the sensors for colour-sensitive ones. Every improvement teaches you a little more about how machines sense and react to the world.
Leave A Comment