Line-following robots use a simple trick: white surfaces reflect infrared light, black surfaces absorb it. The line tracking sensor shines infrared light down and checks whether it bounces back. In this lesson the LED shows when the sensor is over a black line.
What you'll learn
- How reflective infrared sensors work
- How to calibrate a sensor for your surface
- The idea behind line-following robots
What you need
- UNO R3 board and USB cable (UNO R3 Starter Kit)
- Line tracking sensor module (45-in-1 Sensor Kit)
- 1 LED and 1 resistor, 220 Ω (UNO R3 Starter Kit)
- White paper and black electrical tape or a thick black marker
- Breadboard and jumper wires (UNO R3 Starter Kit)
Wiring

| From | To |
|---|---|
| Tracking module S | UNO pin 2 |
| Tracking module + | 5V |
| Tracking module − | GND |
| UNO pin 8 | 220 Ω resistor, then the LED's long leg |
| LED short leg | GND |
Sensor modules from different makers can label and order their pins differently. Always follow the labels printed on your module.
Prepare a track
Stick a strip of black electrical tape on white paper, or draw a thick black line with a marker. Hold the sensor about 1 cm above the paper.
The sketch
const int SENSOR_PIN = 2;
const int LED_PIN = 8;
const int ON_BLACK = HIGH; // check in the Serial Monitor and change to LOW if needed
void setup() {
Serial.begin(9600);
pinMode(SENSOR_PIN, INPUT);
pinMode(LED_PIN, OUTPUT);
}
void loop() {
int value = digitalRead(SENSOR_PIN);
bool onLine = (value == ON_BLACK);
digitalWrite(LED_PIN, onLine ? HIGH : LOW);
Serial.print("Sensor: ");
Serial.print(value);
Serial.println(onLine ? " on the black line" : " on white");
delay(100);
}
Open the Serial Monitor and move the sensor between white paper and the black line. If the messages are the wrong way round, change ON_BLACK to LOW.
How it works
The sensor has an infrared LED and an infrared detector side by side. Over white paper, most of the light bounces back to the detector. Over black, it's absorbed, so little comes back. The module turns that into HIGH or LOW. A line-following robot uses two or three of these: if the left one sees the line, it steers left.
Try this next
- Test other surfaces: wood, a book cover, your hand.
- Make the buzzer from the UNO kit beep when the sensor leaves the line.
- Draw a track and follow it by hand, keeping the LED lit.
Troubleshooting
- Always "on white": hold the sensor closer, about 0.5–1 cm from the surface.
- Always "on the black line": some surfaces, like shiny black plastic, still reflect infrared. Try matte tape.
- Results flicker at the edge of the line: that's normal; the sensor is half on each colour.