Robotics and Embedded Systems

Lesson 16: Line Following and Obstacle AvoidanceNew-age

This lesson is where the pieces become a robot. A line follower uses infrared sensors to stay on a track; an obstacle avoider uses an ultrasonic sensor to steer around what is in its way. Both are control loops: sense, decide, act, repeat — hundreds of times a second. And both are built from parts you already know: the sensors of Lessons 7 and 13, the motors of Lesson 14, the servo sweep of Lesson 15, and the non-blocking code of Lesson 11.

You will build both behaviours, test them, and learn the tuning that makes the difference between a robot that works and a robot that works well. Tuning is not guesswork: it is a method, and this lesson teaches it.

Key Terms.
  • Line follower — a robot that follows a line on the ground using downward-facing sensors.
  • Reflectance sensor — an infrared sensor that measures how much light a surface reflects.
  • Control loop — a repeated cycle of sensing, deciding and acting.
  • Proportional control — steering in proportion to the error, so small errors give small corrections.
  • Error — the difference between the desired state and the measured state.
  • Hysteresis — different thresholds for switching on and off, which prevents flickering.
  • State machine — a program organised as states with rules for moving between them.
  • Calibration — measuring the sensor values for the actual line and floor, and setting thresholds from them.

How a line follower sees the line

A line follower looks down, not ahead. Two or three infrared reflectance sensors point at the floor: a white floor reflects infrared strongly, a black line absorbs it, so the sensor over the line reads very differently from the sensors over the floor. The robot’s job is to keep the line centred under its sensors: if the left sensor sees the line, the robot has drifted right and must steer left; if the right sensor sees it, it must steer right.

Calibrate before you run. Measure the sensor readings on the white floor and on the black line, in the actual light where the robot will run, and set the threshold halfway between them. A threshold chosen in one room will fail in another, because the light is different — so calibrate every time you move the robot, and write the calibration into the code as a variable, not a magic number buried in an if statement.

Steering: from bang-bang to proportional

The simplest steering is bang-bang: if the line is on the left, turn left hard; if it is on the right, turn right hard. It works, but the robot zigzags, wasting speed and wobbling at every curve. Proportional control is smoother: steer in proportion to the error — the distance the line is from the centre. A small error gives a gentle correction; a large error gives a sharp turn. The robot follows the line smoothly instead of hunting across it.

Implement proportional control with a single number: error = leftReading − rightReading, then set the motor speeds as base speed plus or minus the error multiplied by a gain. The gain is the tuning knob: too small and the robot reacts too slowly and leaves the line; too large and it overcorrects and oscillates. Start with a small gain, increase it until the robot oscillates, then back off by a third. That is tuning, and it is a method, not luck.

Obstacle avoidance with a sweep

An obstacle avoider drives forward until its ultrasonic sensor sees something close — say, under 20 cm — then stops, sweeps its sensor left and right on the servo mount, and turns towards the clearer side. The sweep is the robot’s way of looking before it moves: it measures the world, compares the options, and chooses. The behaviour is a state machine: FORWARD, STOPPED, SCANNING, TURNING, with rules for moving between them.

Testing and tuning

Test each behaviour separately before combining them. Test the line follower on a straight line, then on gentle curves, then on sharp ones. Test the avoider with a single box, then with two boxes, then with a wall. At each stage, watch what the robot actually does and change one thing at a time — one threshold, one gain, one speed — so you know what fixed it. Changing three things at once and seeing an improvement tells you nothing.

Worked example — proportional line following

Two reflectance sensors return values from 0 (black line) to 1000 (white floor). The error is left − right: if the line is under the left sensor, left reads low and right reads high, so the error is negative and the robot must steer left. The motor speeds are set from the error with a gain of 0.2 and a base speed of 150.

int err = leftSensor() - rightSensor();
int leftSpeed  = 150 + err * 0.2;
int rightSpeed = 150 - err * 0.2;
drive(leftSpeed, rightSpeed);

Common mistakes

  • Using a threshold calibrated in one room in a different room. Light changes the readings — calibrate where you run.
  • Steering bang-bang when proportional control would be smoother. Zigzagging is a sign the gain is wrong, not that the robot is broken.
  • Changing three things at once during tuning, so you cannot tell what fixed it.
  • Trusting the ultrasonic sensor alone for safety. Keep a bumper switch as the last resort.

Discussion questions

  1. A line follower works on a white floor with a black line. Would it work on a black floor with a white line? What would you change?
  2. Proportional control steers in proportion to the error. What would happen if the gain were zero? What if it were enormous?
  3. Your robot avoids boxes but drives into a glass door. Why, and what sensor would help?

Practice questions

  1. Why must a line follower be calibrated where it will run? Answer: Because the sensor readings depend on the light in the room. A threshold set in one room will fail in another. Explanation: Light changes the readings.
  2. What is proportional control, in one sentence? Answer: Steering in proportion to the error, so a small error gives a gentle correction and a large error gives a sharp turn. Explanation: Correction matches the error.
  3. Your robot oscillates across the line instead of following it smoothly. What is the likely cause, and what do you change? Answer: The gain is too high: the robot overcorrects. Reduce the gain until the oscillation stops. Explanation: Too much gain causes hunting.
  4. An obstacle avoider stops, sweeps, and turns towards the clearer side. Why sweep instead of just turning? Answer: Because sweeping measures both sides before choosing. Turning blindly might turn into a worse obstacle. Explanation: Look before you turn.
  5. Why test one behaviour at a time instead of the whole robot at once? Answer: So that when something fails you know which part failed. Changing one thing at a time makes the cause visible. Explanation: Isolate, then combine.

✅ Check your understanding

Quick recall check, generated from this lesson’s key terms. It is practice only and is not graded.

Keep reading to complete this lesson