Build a line-following robot
ActivitiesProjects
Make a small wheeled robot steer along a taped track using readings from reflectance sensors.
A line-following robot combines a small wheeled chassis, downward-facing infrared sensors, and a control loop that reads a dark track against a light floor and steers back onto it. You can start with a pre-built platform designed for line tracking or assemble one from separate parts—for example, an Arduino Uno, a motor shield, two geared DC motors with wheels and a caster, a 7.4–12 V battery pack, a power switch, and a five-channel reflectance sensor array. That parts list is only one working layout: whichever controller, motor driver, battery, and sensors you choose must match electrically and follow their own wiring specifications.
How the sensors read the track
Reflectance sensors measure how much infrared light bounces back from the surface right beneath the robot, producing distinct readings over a bright background and a dark line. Pololu QTR sensors come in two output styles: RC-type sensors are read by timing an electrical decay on a digital I/O pin, whereas A-type sensors output an analog voltage.
Mounting height and surface finish shift those readings substantially. In a test of a single RC-type sensor over a whiteboard-like surface and black electrical tape, pulse widths measured 120 microseconds on white and 2.6 milliseconds on black at 1/8 inch, then lengthened to 780 microseconds and 10 milliseconds at 3/8 inch. Because the exact numbers depend on your surface materials, sensor height, and microcontroller threshold, inspect live readings over both colors rather than relying on published cutoff values.
Laying a smooth test course
White posterboard or dry-erase whiteboard stock paired with 3/4-inch black electrical tape makes a sample starting setup, though line width and turn radius should suit your chassis and sensor spacing. Sketch straight segments and arcs on the board first so the path flows evenly. When laying the tape around bends, stretch its outer edge tangentially to follow the arc without creases. If your course spans multiple sheets, join them with light masking tape or clips so the boards do not drift apart as the robot runs.
Calibration and bench checks
Before setting the robot loose on the floor, build in a deliberate start control so it does not begin driving while you are still handling it or uploading code. Calibrating on the actual track matters more than guessing fixed thresholds: depending on your setup, the robot can turn right and left across the line on its own, or you can lift the wheels and sweep every sensor manually across both black and white. Recording each sensor's minimum and maximum values lets the program normalize readings to a consistent scale, such as 0 to 1000, and viewing sensor values before a run confirms the array is reading cleanly.
Keep the wheels off the table for one more check: verify that a forward command spins both motors in the forward direction and that placing the line under the left side triggers a left turn. Start your first floor runs at a reduced base speed before pushing the robot faster.
Steering from simple switches to PID control
A straightforward first algorithm estimates the line's position across a five-sensor array on a 0 to 4000 scale, with 2000 directly under the middle sensor. Splitting that range at 1000 and 3000 gives three steering states: drive both wheels forward when the line sits near the middle, or stop one wheel and drive the other (for instance, at speed 100 on a 0–255 scale) when the line drifts to either side. That simple switching gets a robot moving quickly, but abrupt on-off corrections make it shake through curves. Adding intermediate speed steps is an easy first refinement.
For smoother tracking, replace fixed cases with proportional-integral-derivative (PID) feedback. The proportional term measures how far the line sits from center, the integral sums position error over time, and the derivative tracks the rate of change of the proportional error between successive readings. Weighting these terms adjusts wheel speeds continuously rather than snapping between three settings. Even a proportional-derivative controller needs tuning on your own build: example gains such as KP 0.2 and KD 5 reflect one specific five-sensor robot, and motor RPM, robot weight, wheel diameter, and track type all change how aggressively the robot steers.
Lost-line recovery and line mazes
If the robot loses the line, your code needs a plan for what happens next. One firmware pattern stores which side last saw the line, searches slowly back in that direction, and halts after 750 milliseconds without reacquisition—or stops immediately if it has no prior direction stored. Any recovery rule should be tested on the physical track at low speed before you rely on it.
Once your robot follows curves smoothly, adding intersections and a marked finish turns the track into a line maze. On a loop-free maze, a simple preference to turn left whenever possible can explore the course. Introducing loops changes the problem, since a pure left-turn rule can get trapped cycling around a loop or bypass a section altogether.

