Skip to main content

Lesson 2-6 · Two-Sensor Line Following

Module 2 · Line Tracking

Lesson 2-6 · Two-Sensor Line Following

50–60 minControl loopsFollowing the centerSlides (PowerPoint)
👩‍🏫

Teacher mode is on. Toggle it off (bottom-right) to preview the student view.

Last lesson the robot followed one edge with a single sensor. Now you'll use both sensors together — and the code actually gets simpler. The robot straddles the line and follows its center, giving smoother, more reliable tracking.

Learning Objectives

By the end of this lesson you will be able to:

  • Explain why two sensors track better than one
  • Compute error with two sensors: error = left − right
  • Predict the sign of the error from the robot's position
  • Rewrite get_error() for two sensors and see that the follower's loop barely changes
  • Explain why this follower needs no minus sign in arcade(BASE_EFFORT, correction), when Lesson 2-5's did

The difference is the error​

With two sensors straddling the line, you don't need a setpoint anymore. Just subtract one reading from the other:

error = left − right

When both sensors read the same, the difference is zero and the robot drives straight. When one drifts onto the line, the difference tells you exactly how much and which way:

Work out the direction from the readings rather than memorising the table. A high reading means that sensor is over the line. If the left sensor is the one on the line, the line is off to the robot's left — which means the robot has wandered to the right of it. The sensor that finds the line points back toward where the robot should be.

Positionleftrighterror = L − RWhich way to steer
Centered0.50.50.0drive straight
Drifted right0.80.2+0.6steer left, back toward the line
Drifted left0.20.8−0.6steer right, back toward the line
Both on white0.10.10.0straight (lost line)
Both on black0.90.90.0straight (an intersection!)
Three top-down views of a robot, drawn as a rectangle with two small sensor circles at its front, over a vertical strip of black tape. Centered: each sensor sits on an edge of the tape, left 0.5, right 0.5, error 0.0, drive straight. Drifted right: the robot has moved to the right of the tape, so the left sensor is over the tape and the right sensor is on white; left 0.8, right 0.2, error +0.6, steer left. Drifted left: the right sensor is over the tape; left 0.2, right 0.8, error -0.6, steer right.
Two sensors straddle the line. The sensor that finds the tape is on the side the line is on — so the robot has drifted the other way, and steers back toward the high reading.
Knowledge Check

The left sensor reads 0.8 and the right reads 0.2. What is the error, and which way has the robot drifted?

The control loop (and the missing minus sign)​

Here is the payoff for putting the error behind a function last lesson. The follower in test_follow() reads get_error(), scales it, steers. Switching to two sensors means rewriting get_error() — and the loop that calls it barely notices. Two changes to robot.py, both in place:

In the LINE SENSOR section, replace the body of get_error() so it returns get_left() - get_right(), and delete SETPOINT — two sensors don't need one. The comment outline for the new body:

def get_error():
# return the left reading minus the right reading
pass

In test_follow(), the minus sign in front of the correction comes out: arcade(BASE_EFFORT, correction). That's the only change to the loop.

Why no minus now? A positive error means the robot drifted right of the line, so it must steer left to get back, and in arcade() a positive turn steers left. The math already lines up with the robot, so there's nothing to flip.

Knowledge Check

Why does the minus sign from Lesson 2-5 come out of arcade() here?

Activity · Feel the sign, then follow the circle​

Step 1 · Explore the error by hand​

Rewrite get_error() first, and test it before anything drives: add " Error:", round(get_error(), 2) to the print in test_sensors(), set the run line to test_sensors(), and slide the robot across the line by hand. Watch the printed error go positive, then zero, then negative, and say out loud which way the robot has drifted each time. Check yourself against the table above.

Step 2 · Run the follower​

Take the minus sign out of test_follow() and set the run line back to it. Put the robot with the line running between the sensors, press the button, and watch it track the circle. If it steers away from the line as soon as it drifts, you have the sign backwards — go back to step 1 and work out which way a positive error should steer. Notice what you did not have to change: the loop is the same three lines it was with one sensor. The function changed; its caller didn't.

Step 3 · Compare with one sensor​

Temporarily put the one-sensor get_error() back (and the SETPOINT), with its minus sign, and run test_follow() again right after. Which is smoother? Which handles the curves and higher speeds better? Then restore the two-sensor version — that's the one the rest of the course uses.

Step 4 · Challenge — push the speed​

Turn BASE_EFFORT up to 0.4–0.5 for both versions and watch what happens to each. Which one degrades first, and what does that tell you about how much information one sensor gives the robot?

Real-world connections​

Comparing two sensors to find a position is a workhorse technique:

Audio

Stereo & hearing

You locate a sound from the tiny difference between your two ears — same differential idea.

Navigation

Differential GPS

Comparing two receivers cancels error and pins a position down to centimeters.

Robotics

Sensor arrays

Advanced line followers use 5–8 sensors, but the principle is the one you just learned.

Wrap-up​

  • What's the two-sensor error formula, and why no setpoint? (left − right; equal readings already mean zero.)
  • Positive error means the robot drifted which way? (Right — the left sensor found the line, so the line is left of the robot.)
  • Why did the minus sign come out? (A positive error now needs a left steer, which is already a positive turn in arcade().)

Resources​