Built-in project · Arduino Uno R3

Line tracking sensor on pin 3, on an Arduino Uno

A TCRT5000 line sensor’s digital output on pin 3, printed to the serial monitor: one value over white, the other over a black line. The whole build, an Arduino Uno and 1 more part, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Beginner About 10 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $31. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • How an infrared reflective sensor tells light from dark
  • Reading a digital input with digitalRead
  • How line following robots find their path
  • Printing readings to the Serial Monitor
  • Difficulty: Beginner
  • Time: About 10 minutes
  • Board: Arduino Uno R3
  • Parts: 3

How it works

The TCRT5000 shines an infrared LED down at the surface and watches the reflection with a phototransistor. White reflects a lot of light. Black reflects very little. The module's DO pin turns that into a digital 0 or 1 on pin 3.

The sketch reads pin 3 four times a second and prints the value. Click the sensor in the simulator and move the surface under it to see the number change. Over white it prints one value.

Parts list

3 parts, plus the jumper wires. They retail for about $31; on Mokxi they're free.

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Part and pinConnects to
Line sensor, TCRT5000 pin GNDArduino Uno R3 pin GND
Line sensor, TCRT5000 pin DOArduino Uno R3 pin 3
Line sensor, TCRT5000 pin VCCArduino Uno R3 pin 5V

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// A line tracking sensor: is it over the black line?
//
//   sensor DO -> pin 3   VCC -> 5V   GND -> GND
//
// An infrared LED shines down and a phototransistor watches what comes back.
// White reflects it and black does not, so DO tells the two apart.
//
// Click the sensor and move the surface under it.

void setup() {
  pinMode(3, INPUT);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: line tracking sensor on pin 3");
}

void loop() {
  Serial.println(digitalRead(3));
  delay(250);
}

Code walkthrough

The key functions of the sketch, and what each one does.

setup sketch.ino

Runs once. pinMode(3, INPUT) makes pin 3 a plain input to listen to the sensor. Serial.begin(9600) opens serial output and a title line is printed.

void setup() {
  pinMode(3, INPUT);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: line tracking sensor on pin 3");
}

loop sketch.ino

Prints digitalRead(3), which is 0 or 1 depending on what is under the sensor, then waits 250 milliseconds with delay. That gives four readings per second.

void loop() {
  Serial.println(digitalRead(3));
  delay(250);
}

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • The value never changes This project
    Check that DO goes to pin 3, VCC to 5V and GND to GND, and that you move the surface under the sensor.
  • It cannot tell black from white. Line sensor
    Keep the sensor about 2 mm to 10 mm above the surface. Set the trimmer on the module for the surface you use. Too far gives no change, and too close can saturate the eye.
  • The readings change with room light. Line sensor
    Sunlight and bulbs contain infrared and confuse the sensor. Shade it from outside light. Calibrate on the actual black and white surfaces with analogRead, not just a fixed guess.
  • Upload fails or nothing runs after wiring pins 0 and 1 to a module. Arduino Uno R3
    Pins 0 and 1 are the USB serial port. Move the module to other pins, or unplug it while uploading. Also check the right board is selected.
  • Pins misbehave or the board resets when you power motors or servos from it. Arduino Uno R3
    Each I/O pin gives about 20 mA, 40 mA max. Power motors and servos from their own supply and join its ground to the Uno's GND.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.