What is a TCRT5000L?
A TCRT5000L is a type of reflective optical sensor that detects color differences in an object by reflecting light off of it.
The TCRT5000L is a simple sensor. It has an infrared LED emitter, and a phototransistor that receives the light reflected by a possible obstacle. The amount of light received depends on the color and reflectivity of the object, so we can distinguish between light and dark areas of an object.
These sensors are usually provided with a standard measurement board with the LM393 comparator, which allows to obtain the reading as a digital value when a certain threshold is exceeded, which is regulated through a potentiometer located on the board. We can capture this signal with the digital inputs of Arduino.
The sensor’s measuring range varies from 0.2 to 15mm, with the optimal distance being 2.5mm. Therefore, it is a very short distance sensor.
The amount of infrared light has a strong dependence on the color, material, shape, and position of the obstacle, so they do not have enough precision to provide an estimate of the distance to an object, they are simply capable of detecting it.
TCRT5000L sensors are widely used to make line-following robots, although they can also be used to detect any other type of object. For example, they are used in printers to know when the paper has run out.
Price
TCRT5000L sensors are really cheap. We can find them assembled with the measurement board for €0.65 from international sellers on eBay or AliExpress.
Since it is a simple sensor, we can also assemble it ourselves. In general, it is not worth it since only the components would cost us more, not to mention the time required and the quality we could obtain, so it is normal for us to use a commercial model.
Assembling our own sensor only really makes sense when, due to the location where the sensor has to be mounted, we do not have space to accommodate a commercial board and we are forced to integrate the component.
Assembly diagram
If you use a commercial board, which as we said in general is recommended, assembling a TCRT5000L to Arduino is really simple. We power the module through Vcc and GND by connecting them, respectively, to the 5V and GND output on Arduino.
On the other hand, we connect the digital output of the sensor (DO) to a digital input to read the state of the sensor.
If you decide to do all the assembly yourself, the electrical diagram is also not complicated. We simply need to power the module correctly through the appropriate resistors, respecting the component’s diagram.
Code examples
To detect when the TCRT5000L passes over a dark area, we simply read the state of the digital input, as we saw in the post Digital Inputs in Arduino.
When the sensor is triggered, we will take appropriate actions, such as stopping or changing the direction of a robot.
const int sensorPin = 9;
void setup() {
Serial.begin(9600); //start serial port
pinMode(sensorPin, INPUT); //define pin as input
}
void loop(){
int value = 0;
value = digitalRead(sensorPin ); //digital pin read
if (value == LOW) {
Serial.println("TCRT5000L activated"); //dark area
}
delay(1000);
}
Another way to handle the TCRT5000L is to use interrupts, which will simplify our code. However, in a line-following robot, we will frequently use three to five line detectors, while Arduino UNO and Nano only have two external interrupts.
For more information on interrupts, see the post What are and how to use interrupts in Arduino and Read a pushbutton with interrupts in Arduino
Download the code
All the code from this post is available for download on Github.