Skip to main content

Line following

Time: 11:05 AM to 11:35 AM
In this section, your robot will follow a line on the ground without any human input. This is your first taste of autonomous behavior, but it is not AI. It uses simple rules, not learning.

Set up the track

Place a strip of black tape on a light-colored surface. If your kit includes a printed track, use that instead.

Run the line tracking script

sudo python3 6.line_tracking.py
Watch as the robot follows the line autonomously.

Read the sensor data

Check what the sensor sees in real time:
px.grayscale_module.get_grayscale_data()
This returns three analog brightness values, one for each sensor in the array. Watch how the values change as the sensor moves across light and dark surfaces.

How it works

The robot is not “seeing” the line. It reads three brightness values and uses a simple if/else rule to steer:
  • If the left sensor is over the dark line, steer left
  • If the right sensor is over the dark line, steer right
  • If the center sensor is over the line, go straight
This is rule-based behavior, not AI. The robot does exactly what it was programmed to do, nothing more.

Experiment

Try increasing the follow speed and observe how it affects tracking stability. At what speed does the robot start losing the line?
The relationship between speed and tracking accuracy is a real engineering trade-off. Faster movement means less time to react to sensor changes.