Lesson 1 · The Reflectance Sensor
Lesson 1 · The Reflectance Sensor
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()andget_right() - Interpret sensor values (roughly
0.0on white to1.0on 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.
- 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.
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)
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
Your 'off line' (white) values average 0.2 and your 'on line' (black) values average 0.8. What threshold should you choose?
Why does the XRP have two reflectance sensors instead of one?
Real-world connections
Reflectance sensing shows up all over the automated world:
Barcode scanners
Scanners read the pattern of light and dark bars exactly the way your sensor reads tape.
Line-following AGVs
Automated guided vehicles follow lines taped or painted on warehouse floors.
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.)