In this tutorial, I am going to show you how to use KY-033 line tracking sensors. These kinds of devices are widely used, for instance, to guide remote-controlled vehicles along a dark-coloured line on the ground.
Parts Required
- ESP8266 Node MCU;
- Micro-USB to USB cable;
- KY-033 line tracking sensor;
- 1 red LED;
- 1 green LED;
- 2 220Ω resistor;
- Male-to-male jumper wires;
- Male-to-female jumper wires;
- 1 Breadboard;
Note: Since only 3 digital pins are required for you to follow this tutorial, an Arduino Nano, ESP32 NodeMCU board or any similar board can be used instead. Just be sure to change the pins in the code if necessary.
The KY-033 Line Tracking Sensor
The working principle of these kinds of sensors is based on the use of infrared (IR) light to detect when the sensor is positioned over a dark or light-coloured surface, and there are numerous types of different tracking sensors on the market. As seen in Figure 2, the KY-033 sensor module’s main components are:
- Reflective Optical Sensor – a TRRT5000 reflective sensor which includes an IR emitter and IR receiver;
- Potentiometer – defines a threshold for the digital output pin;
- Comparator – an LM393 comparator, which compares the signal created by the IR receiver with the predefined value through the potentiometer to control the status of the digital output.
Regarding the reflective optical sensor’s internal circuit (Figure 3), it is a very simple component, containing an IR LED and a phototransistor in a leaded package which blocks visible light. For more information about the TRRT5000 reflective optical sensor, take a look at its datasheet.
As previously mentioned, the sensor uses IR light to detect if it is positioned over a dark or light-coloured surface. This is because light-coloured surfaces tend to reflect IR light, while darker surfaces tend to absorb it. Thus, when the tracking sensor is placed over a light surface, the IR light is reflected from the surface and then detected by the phototransistor, allowing current to flow through it. Over a dark surface, however, the IR light is absorbed and so the phototransistor will not detect any light, blocking the flow of current. The higher the IR light intensity detected from the phototransistor, the higher the current flowing from the collector (C) to the emitter (E) of the phototransistor and therefore the higher the voltage drop across the analogue output of the sensor.
One very important aspect to be taken into consideration when 4using these sensors is their detectable area, as exemplified in Figure 4. The IR emitter and receiver have an irradiated and receivable area, respectively. The sensor is only operational when these two areas intercept each other. Typically, the KY-033 module has a detectable area between 0.2 and 1.5 cm, thus it is recommended to use these sensors close to the ground surface.
Circuit
As seen in Figure 1, the KY-033 module has 3 pins:
- VCC – Power supply (5V);
- OUT – Sensor output;
- GND – Ground (0V).
We can start by connecting the VCC and GND pins of the sensor to the Vin and GND pins of our ESP8266, respectively. Then, we can connect the OUT pin of the sensor to pin D4 of the ESP8266. To visually perceive the correct line tracking from the sensor, 2 LEDs (one green and one red) are added. Thus, the green LED will be on if the sensor is tracking a dark surface, (like a black line), while the red LED will be on otherwise, indicating that the system to which our sensor is attached is out of its pretended trajectory. To do so, connect the anode of the green and red LEDs to pins D1 and D2 of the ESP8266, respectively, the cathode of the LEDs to their respective 220 Ω resistor and its remaining terminals to ground. In the end, your circuit should be set as shown in Figure 5.
Code
Finally, it is time for some code. We can start by defining the desired port numbers and the system’s inputs (KY-033 sensor) and outputs (LEDs). Optionally, we can also initialize the serial communication with the PC, in order to check the output of the sensor over time. Then, according to what was previously mentioned, the green LED is set on if a dark surface (black line) is detected, while the red LED will be on otherwise, possibly indicating that the system to which our sensor is attached is out of its pretended trajectory.
#define sensorPin 2 #define greenLED 5 #define redLED 4 void setup() { // Definition of the sensor as input and the LEDs as outputs pinMode(sensorPin, INPUT); pinMode(greenLED, OUTPUT); pinMode(redLED, OUTPUT); // Initialize serial communications with the PC Serial.begin (9600); } void loop() { int value = digitalRead(sensorPin); // Prints sensor value to the Serial Monitor Serial.println(value); // If a dark surface is detected: if (value == HIGH) { digitalWrite(greenLED, HIGH); digitalWrite(redLED, LOW); } // If a light surface is detected: else { digitalWrite(greenLED, LOW); digitalWrite(redLED, HIGH); } }
That’s it! If you enjoyed this tutorial, you can visit our YouTube channel and watch our many tutorials. Thanks for following us and be sure to rate, comment and share our content.
References
[1] https://www.vishay.com/docs/83760/tcrt5000.pdf
Towards the Future !!! 😉