Line-following robot logic explains how a robot uses sensors to detect a line and choose motor speeds to stay on track. This cheat sheet helps students connect sensor readings, decisions, and motor commands in a clear control loop. It is useful for building, testing, and debugging classroom robots that follow black tape, colored paths, or reflective lines.
Students need these ideas to make a robot respond smoothly instead of drifting, wobbling, or losing the line.
The core logic compares sensor readings to a threshold, calculates an error, and adjusts the left and right motors. A simple two-sensor robot can turn left, turn right, go forward, or search depending on which sensor sees the line. More advanced robots use proportional control, where correction = Kp x error, to make smoother steering changes.
Good line following depends on calibration, steady sensor placement, reasonable speed, and repeated testing.
Key Facts
- A sensor threshold separates line from background, so a reading below or above the threshold is classified as line or not line depending on the sensor type.
- For a two-sensor robot, if both sensors see the background, the robot usually drives forward because it is centered near the line.
- For a two-sensor robot, if the left sensor sees the line, the robot should turn left or slow the left motor to move back toward center.
- For a two-sensor robot, if the right sensor sees the line, the robot should turn right or slow the right motor to move back toward center.
- Error can be calculated as error = target position - measured position, where zero error means the robot is centered on the line.
- Proportional correction uses correction = Kp x error, where Kp is the gain that controls how strongly the robot steers.
- Motor commands can be set with left motor = base speed - correction and right motor = base speed + correction for one common steering setup.
- A control loop repeats the steps read sensors, calculate error, choose motor speeds, and update motors many times per second.
Vocabulary
- Line sensor
- A sensor that detects how much light reflects from the floor so the robot can tell the line from the background.
- Threshold
- A cutoff value used to decide whether a sensor reading means line or background.
- Error
- The difference between where the robot should be and where the sensor readings show it is.
- Base speed
- The normal motor speed the robot uses before steering corrections are added.
- Proportional gain
- A number called Kp that controls how large the steering correction is for a given error.
- Control loop
- A repeated sequence where the robot reads sensors, makes a decision, and updates its motors.
Common Mistakes to Avoid
- Using an untested threshold is wrong because lighting and floor color can change sensor readings. Calibrate the robot by measuring the line and background before choosing the cutoff.
- Making the base speed too high is wrong because the robot may pass over curves before it can react. Start slow, then increase speed after the logic works reliably.
- Reversing the motor correction is wrong because the robot steers away from the line instead of toward it. If the robot turns the wrong way, swap the plus and minus correction signs or reverse the turn rule.
- Choosing a Kp value that is too large is wrong because the robot may zigzag sharply across the line. Lower Kp if the robot oscillates or shakes while following.
- Ignoring lost-line behavior is wrong because the robot may keep driving straight after it leaves the path. Add a search rule, such as turning toward the last side that detected the line.
Practice Questions
- 1 A robot uses threshold = 500, where readings below 500 mean black line. If the left sensor reads 430 and the right sensor reads 720, which way should the robot turn?
- 2 A robot has target position = 0 and measured position = -3. Calculate error using error = target position - measured position.
- 3 A robot uses Kp = 8 and error = 5. Calculate correction using correction = Kp x error, then find left motor and right motor if base speed = 60 and left motor = base speed - correction, right motor = base speed + correction.
- 4 Why can a robot with correct line-detection logic still fail to follow a curved line smoothly?