sheen.bot Logo

Insights

Line-following robots: the physics and the code

17 Sept 2025·Sheen Robotics
Line-following robots: the physics and the code

A line-following robot reads reflected light, thresholds it into on-line or off-line, and steers with a control loop. Here is the physics, the code, and why tuning is the real lesson.

A line-following robot works by reading how much light bounces off the floor beneath it, deciding whether each sensor is over the dark line or the pale surface, then steering to keep the line centred. The physics is reflectance. The code is a small control loop that runs a few hundred times a second. Get both right and the robot hugs a tight curve; get the tuning wrong and it wobbles off into a table leg. This guide walks through how it works, the two common steering methods, and why tuning is the part that actually teaches engineering.

How the sensor reads the line

The core part is an infrared reflectance sensor. It pairs a tiny infrared LED with a phototransistor that measures how much of that light comes back. A dark line absorbs most of the infrared, so little returns. A pale floor reflects most of it, so a lot returns. The sensor sits a few millimetres above the ground and reports an analog value, usually read as a number from an analog-to-digital converter (ADC).

Most beginner robots use two or more of these sensors side by side, straddling the line. When the line drifts under the left sensor, the left reading drops. When it drifts right, the right reading drops. That difference is the whole basis of steering. Two practical truths matter here: sensor height changes the reading, and ambient light leaks in. A robot tested under classroom lights can behave differently next to a sunny window, which is why calibration comes next.

Turning a reading into a decision

A raw ADC number is not useful on its own because it drifts with the surface, the lighting and even a fading battery. The fix is thresholding. You sweep the sensors slowly across both the line and the floor, record the lowest and highest readings, and set the threshold at the midpoint. Anything below the threshold counts as on the line, anything above counts as on the floor, or the reverse depending on your wiring.

This calibration step is not optional. Do it on the actual track, in the actual room, every time you move. Matte black electrical tape on a light floor gives a clean contrast; glossy tape or a dark tiled floor gives a weak one. Ten minutes of calibration saves an hour of confused debugging later.

Two ways to steer: bang-bang and proportional

The simplest control is bang-bang, also called on-off. The logic reads: if the left sensor sees the line, turn left; if the right sensor sees the line, turn right; otherwise drive straight. It is easy to write and easy to understand, but it steers in hard jerks. The robot zig-zags down a straight line and has to crawl to stay on curves, because it only ever knows on or off, never how far off.

Proportional control fixes that by using the analog readings instead of a yes-or-no. You compute an error, for example error = left - right, and make the turn strength scale with it: turn = Kp * error. A small error produces a gentle correction; a large error produces a strong one. The single number Kp, the proportional gain, decides how aggressively the robot responds. This is the first real taste of control theory, and it is enough to make a robot glide around a track that bang-bang would rattle across. Adding the I and D terms to make a full PID loop is a natural next step once proportional feels solid.

Tuning is the real lesson

The code above is short. Almost all of the learning lives in tuning it, and that is a feature, not a chore. Tuning is where students meet the engineering method: change one variable, test, observe, repeat. Work through it in order:

  • Calibrate on the real track under the real lights before touching anything else.
  • Set a base speed slow enough that you can watch the robot and grab it.
  • Start Kp small and raise it until the robot tracks the line instead of drifting off.
  • If it starts oscillating, weaving side to side, lower Kp a little or drop the base speed.
  • Test on the sharpest curve on the track, not the easy straight. The straight lies to you.

Keep a notebook and change one thing at a time. A student who writes down that a high gain made the robot weave learns more than one who twiddles numbers at random until it happens to work.

Common failures and how to debug them

Most line-follower problems fall into a handful of patterns. Match the symptom to the likely cause before you rewrite any code:

  • Robot ignores the line completely: the threshold is wrong, the sensors are mounted too high, or the battery is low enough to weaken the LEDs.
  • Violent zig-zagging: Kp or the base speed is too high. Reduce one at a time.
  • Flies off on sharp corners: too fast, or the two sensors sit too close together to notice the turn early.
  • Drifts to one side on a straight: the two motors are not perfectly matched. Add a small fixed trim to one side.
  • Works in the morning, fails after lunch: sunlight has changed the readings. Recalibrate for the new lighting.
  • Every sensor reads almost the same: check the wiring and the pin numbers, and confirm the tape is matte, not shiny.

Trying it in a classroom

A line follower is one of the best first robotics projects because the whole loop, sense then decide then act, is visible in a single run across the floor. The rover build on the sheenbot∞ board gives students the reflectance sensors and motor control they need on one board, and complete kits are available from the store. If you would rather have it taught, the academy in Cape Town runs weekly classes and holiday workshops where line following is a regular build.

Takeaway

Reflectance sensing tells the robot where the line is, thresholding turns that into a clear decision, and proportional control turns the decision into smooth steering. The code is small on purpose. The real skill is the patient, one-change-at-a-time tuning that gets a wobbling robot to hold a curve, and that skill carries over to almost every other project. For more build write-ups, see the sheen.bot newsroom.

Frequently asked questions

How many sensors does a line follower need?

Two is enough to steer, because steering only needs to know whether the line has drifted left or right. Adding more sensors, five is common, lets the robot judge how far off it is more precisely and handle sharper turns, which makes proportional control smoother.

Why does my robot follow the line in one room but not another?

Ambient light and floor colour change how much infrared bounces back, so the threshold calibrated in one room is wrong in another. Recalibrate on the new surface and under the new lighting every time you move the robot.

Is proportional control the same as PID?

Proportional is the P of PID and is the first and most important term. PID adds an integral term to cancel steady drift and a derivative term to damp overshoot. Most teen projects work well on proportional alone, then add the other two as an upgrade.

#robotics#line-following#sensors#coding#projects

More Insights