Skip to main content

Lesson 1 · The Reflectance Sensor

Module 2 · Line Tracking

Lesson 1 · The Reflectance Sensor

50–60 minSensorsFirst look at sensing
👩‍🏫

Teacher mode is on — the gray boxes are yours only. Flip the switch (bottom-right) to preview the student view.

In Module 1 the robot only ever did what you told it, step by step. Now it gets to sense the world. This lesson introduces the XRP's reflectance sensors — the "eyes" it uses to tell a white surface from a black line — and you'll record the readings you'll rely on for the rest of the module.

Learning Objectives

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

  • Explain how a reflectance sensor tells light surfaces from dark ones
  • Read the left and right sensors with get_left() and get_right()
  • Interpret sensor values (roughly 0.0 on white to 1.0 on black)
  • Pick a threshold that separates "on the line" from "off the line"
  • Record calibration data for your own robot and tape

How the robot "sees" a line

Flip an XRP over and you'll find two small reflectance sensors underneath. Each one shines invisible infrared light down at the surface and measures how much bounces back.

🖼️
Diagram — IR sensor shining light: white reflects a lot (low value), black absorbs it (high value)
Add a src to drop in your image
A white surface reflects most of the light back (a low reading); a black line absorbs it (a high reading).
  • White surface → most light bounces back → a low value (around 0.1–0.3).
  • Black line → light is absorbed → a high value (around 0.7–0.9).

The XRP has a left and a right sensor, each with its own reading — which is how the robot can tell which side of a line it's on.

Knowledge Check

Your sensor reads 0.8. What is it most likely over?

Read the sensors in Python

Reading a sensor is just importing its class and calling a method:

from XRPLib.reflectance import Reflectance
from XRPLib.board import Board

reflectance = Reflectance.get_default_reflectance()
board = Board.get_default_board()

board.wait_for_button()

left = reflectance.get_left()
right = reflectance.get_right()
print("Left sensor:", left)
print("Right sensor:", right)

To watch the values change as you slide the robot across the line, put the read in a loop:

import time

for i in range(50):
left = reflectance.get_left()
right = reflectance.get_right()
print("Left:", left, " Right:", right)
time.sleep(0.1)
🖼️
Screen recording — sliding the robot across the tape while sensor values print
Add a src to drop in your image
Slide the robot slowly across the line and watch the readings climb from ~0.2 to ~0.8.

Activity · Calibrate and pick a threshold

Every surface and every roll of tape is a little different, so you calibrate — measure your own values — instead of trusting a textbook number. Record readings in three positions: both sensors on white, both on the black line, and one on the edge.

Then pick a threshold halfway between your highest white reading and your lowest black reading. That's the number your programs will use to decide "on the line" vs. "off."

threshold = (highest white value + lowest black value) ÷ 2

Knowledge Check

Your 'off line' (white) values average 0.2 and your 'on line' (black) values average 0.8. What threshold should you choose?

Knowledge Check

Why does the XRP have two reflectance sensors instead of one?

Real-world connections

Reflectance sensing shows up all over the automated world:

Retail

Barcode scanners

Scanners read the pattern of light and dark bars exactly the way your sensor reads tape.

Warehouses

Line-following AGVs

Automated guided vehicles follow lines taped or painted on warehouse floors.

Phones

Ambient light sensors

Your phone dims its screen using the same idea — measuring reflected/available light.

Wrap-up

  • Does a higher reading mean lighter or darker? (Darker — less light bounced back.)
  • What is a threshold, and how do you pick one? (The cutoff between line and white; halfway between your calibrated values.)
  • Why two sensors? (To tell which side of the line the robot is on.)

Resources