TL;DR:
An emitter shines infrared light and a detector measures how much returns. Dark surfaces reflect less.
Pick a threshold halfway between the black and white readings you measure on your own track.
Sunlight and room lights add infrared, so shield, calibrate and re-check the threshold.
A line-following robot needs one simple fact: is the sensor over the line, or over the floor? An infrared reflectance sensor answers this with a pair of tiny parts and a few lines of code. Getting it to work reliably in a real room is a different challenge, and it is where the details matter.
How the sensor works
The sensor has two parts side by side. The emitter is an infrared LED, which gives off light you cannot see. The detector is a phototransistor or photodiode, which produces a signal that depends on how much infrared light reaches it. When the sensor is held a few millimetres above a surface, light from the emitter bounces off the surface and some of it reaches the detector.
A white or shiny surface reflects a lot of the light. Black tape absorbs most of it, so less returns. The robot reads the difference as a number.
Worked example: readings on black tape vs white floor
Say you read the sensor with a 10-bit analogue input, which gives values from 0 to 1023. On many modules a brighter return gives a lower number, but this depends on the circuit, so test yours. Here is a set of imagined readings from one module held 5 mm above the surface:
Surface | Reading (0 to 1023) | Note |
|---|---|---|
White floor | 120 | Lots of light returns |
Black tape | 850 | Little light returns |
Edge between them | about 480 | Half on tape, half on floor |
Choose the threshold halfway between the two: (120 + 850) ÷ 2 = 485. In code: if the reading is above 485, the sensor is over the line, otherwise it is on the floor. Check that the edge reading of about 480 falls near the threshold, which is what you want, because that is where the decision should flip.
The gap between the black and white readings, 850 − 120 = 730, is your safety margin. A wide gap means noise or small changes in height will not flip the decision. If your gap is small, the sensor is probably too far from the surface or the tape is shiny.
From one sensor to following a line
One sensor can follow the edge of a line: if it sees black, steer one way, otherwise steer the other, and the robot weaves along the edge. Two sensors, one on each side of the line, let the robot steer left when the left sensor sees black and right when the right one does. An array of five or more sensors lets you compute a position estimate across the line and steer in proportion, which is smoother.
Thresholds you can trust
Place the robot on the actual track, under the lighting where it will run.
Read each sensor over white and over black, several times, and record the lowest and highest values.
Set each sensor's threshold halfway between its own black and white readings.
Re-check the thresholds when the lighting or the floor changes.
Sensors on the same robot rarely give exactly the same numbers, because of tiny differences between parts and heights. A separate threshold for each sensor, found by this routine, works better than one shared value.
Ambient light
Sunlight and many lamps give off infrared light too. That extra light reaches the detector and shifts all your readings, and it can change as a cloud passes or a person walks by. There are three common fixes.
Shield the sensor. Mount it low, close to the surface, with a small skirt around it to block stray light.
Measure with the emitter off, then on. Subtract the "off" reading from the "on" reading. What is left is the light from your own emitter.
Recalibrate at the start of each run, in the conditions where you are racing.
Height and surface
Distance to the surface changes the reading a lot. If the robot rocks or bounces, the numbers move. Mount the sensors on a rigid part and at the height the data sheet recommends. Glossy tape can reflect like a mirror, so use matte tape where possible.
Tip: Print the raw sensor values to the screen as you move the robot slowly across a line. You can see the transition, the noise and the right threshold for yourself.
Common mistakes
Copying a threshold from a tutorial instead of measuring your own.
Calibrating indoors and racing under bright lights.
Using a sensor whose output goes down for black when your code expects it to go up.
Mounting the sensors too high, so the difference between black and white nearly vanishes.
Your plan for this week
Tape a black line on a pale floor, read one sensor over each surface ten times and compute the threshold from the lowest and highest values. Then test in different lighting. If you want help checking your threshold arithmetic, DuckyHelper can work through the numbers with you.
Frequently asked questions
Why does my sensor work at home but not at the competition?
The lighting and floor are different. Recalibrate on site, and shield the sensor to reduce the effect of stray infrared light.
Do I need an analogue input?
Not always. Some modules have a built-in comparator that gives a simple on or off output, but you lose the ability to tune the threshold in code.
