Robotics and Motion

How a Line Following Robot Works

Learn how a line following robot uses reflectance sensors, simple steering logic and careful tuning to track a dark line, and where beginners usually go wrong.

On this page
  1. A small robot with a clear job
  2. The sensors: seeing contrast
  3. The decision logic
  4. Calibration and thresholds
  5. Tuning for speed and stability
  6. Handling junctions, gaps and lost lines
  7. Building it
  8. What it teaches

A small robot with a clear job

A line follower is a small wheeled robot that drives along a dark line on a light surface, or the reverse. It is a popular first robot because the task is simple to describe, the parts are cheap, and the results are visible: it either stays on the line or it does not. It also contains a complete sense, decide and move cycle in miniature, which makes it a good way to learn how feedback works.

The sensors: seeing contrast

The robot detects the line with reflectance sensors, usually mounted underneath the front of the chassis. Each one contains an infrared LED that lights the surface and a light-sensitive part that measures how much comes back. A pale surface reflects more infrared than a dark line, so the reading differs depending on what is underneath.

Some points about these sensors:

  • Many are analog. They output a voltage that varies with reflection, which the controller reads as a number. Others have a built-in comparator and give a plain on or off. The practical difference between analog digital signals matters here: analog gives you more information, while digital is simpler to read.
  • Height matters. Sensors usually work best a few millimetres from the surface. Too high and the contrast fades; too low and they may drag. Check the datasheet for the recommended range.
  • Ambient light matters. Strong sunlight contains infrared and can confuse readings. Indoor testing is the easiest starting point.
  • The line itself matters. Black electrical tape on a light board is a common choice. Glossy tape can reflect unevenly, and matte surfaces are usually easier.

How many sensors? Two are enough for a basic follower. Three or five let the robot tell how far it has drifted, not just which side, and arrays of eight or more are used on faster robots.

The decision logic

With two sensors straddling the line, the logic is simple:

  • Both see the surface: go straight.
  • Left sensor sees the line: the robot has drifted right, so steer left.
  • Right sensor sees the line: steer right.
  • Both see the line: this might be a junction or a stop mark.

Steering is done by differential drive: the robot has two driven wheels, and speeding one up relative to the other turns it. Slowing the inside wheel, or even reversing it, tightens the turn.

On-off steering

The simplest approach is to turn hard whenever the line is detected on one side. It works, but the robot tends to zigzag, because it always overcorrects and then corrects back. It can still be fine for slow robots on gentle curves.

Proportional steering

With several sensors, you can assign each a position (for example, far left, left, centre, right, far right) and work out where the line is relative to the robot’s centre. The difference between where the line is and where it should be is the error. The robot then steers in proportion to that error: a small drift gives a gentle correction and a large drift gives a firm one. This is the first step toward PID control, where the correction also considers how the error has built up and how quickly it is changing.

Calibration and thresholds

No two surfaces or sensors behave identically, so calibration is part of the design. A common method is to read the sensor over the line and over the background, then place the threshold between the two values. Some robots do this at start-up by sweeping over both. Choosing a threshold in the middle works better than a fixed number copied from another build, since lighting, tape and sensor height all shift readings.

Noise can make a reading flicker near the threshold. A little hysteresis, meaning a slightly different threshold for switching on than for switching off, or an average of several quick samples, reduces false changes.

Tuning for speed and stability

If the robot oscillates left and right, the correction is too strong or the loop reacts too slowly. If it runs wide on curves, the correction is too weak or it is moving too fast for the sensors to react. Useful adjustments include:

  1. Slow down first. Get reliable tracking at low speed, then increase speed gradually.
  2. Adjust the steering gain. Change one value at a time and watch the behaviour.
  3. Move the sensors forward. Sensors mounted ahead of the wheels see the line earlier, giving more time to react, though the geometry changes how turns feel.
  4. Slow down in curves. Reduce base speed when the error is large.
  5. Keep the loop fast and steady. Long delays or slow printing to a serial monitor inside the loop can harm performance.

Changing one thing at a time and writing down results is a sound debugging strategy here, and it avoids chasing several changes at once.

Handling junctions, gaps and lost lines

Real tracks are not just smooth curves. Typical complications include:

  • Intersections, where all sensors see the line at once.
  • Sharp corners at right angles that exceed the sensors’ reach.
  • Gaps in the line, where the robot should keep going straight for a short distance.
  • Lost line, where no sensor sees it. A sensible response is to turn toward the last side the line was seen, or stop.

As these cases accumulate, a tangle of if-statements becomes hard to read. State machines handle this more tidily: states such as following, turning at a junction and searching for the line each have their own simple rules, and sensor events decide when to switch.

Building it

For a basic build you need a chassis, two geared DC motors, a motor driver (the controller pins cannot supply the current themselves), a microcontroller board, the sensors and a battery. Wiring on a breadboard is fine for first tests, but loose jumper wires are a common source of intermittent faults, so good breadboard wiring habits save frustration. Sensor LEDs need current-limiting resistors, which is where understanding resistor values helps, and the datasheet usually states the recommended value.

Power the motors and logic carefully. Motor current spikes can reset a controller, which looks like random misbehaviour. If you use lithium-based cells, follow the manufacturer’s charging and storage guidance. Test with the wheels lifted before putting the robot on the floor, so a software mistake does not send it off the table.

What it teaches

A line follower shows the full loop in a form you can watch: sensors turn the world into numbers, simple logic turns numbers into steering, and motors turn steering into movement. Every improvement you try, such as better calibration, smoother steering or smarter handling of junctions, applies to bigger robots later.

The complete guide to robotics and motion

How Hobby Robots Sense, Decide and Move
Read the guide

Keep exploring

Guides from other topics

All topics →