Arduino Tutorial: IR Distance / Line Tracing / Line Tracking Sensor (MH Sensor Series/KY-033/TCRT5000)

IR distance sensor (MH Sensor Series, KY-033, TCRT5000).

IR distance sensor (MH Sensor Series, KY-033, TCRT5000).

In this tutorial, it is shown how to use an IR distance sensor with an Arduino Uno. The todays sensor comes in many names: MH Sensor Series, KY-033 (variant with 3 pins), TCRT5000, etc. Moreover, it is often advertised as IR distance sensor, line tracing sensor or line tracking sensor.
In addition to the IR distance sensor, this tutorial makes use of an LCD module called “LCM1602 IIC V1” which is utilized to show sensor values. The main advantage of the LCM1602 IIC V1 is that it is very easy-to-use. For example, it can be controlled by setting up an I2C connection.

List of materials:
– Arduino Uno [Search on Aliexpress | Amazon]
– Jumper wires [Search on Aliexpress | Amazon]
– Mini breadboard [Search on Aliexpress | Amazon]
– MH Sensor Series [Search on Aliexpress | Amazon]
– LCM1602 IIC V1 (LCD) [Search on Aliexpress | Amazon]

Remark: Some variants of the module type, such as the KY-033, have only three pins. Typically, the A0 pin is missing. Moreover, the D0 pin is often labeled as S. If you own such a variant, this tutorial is still of use to you. Just ignore the part related to the A0 pin.

Pin layout:

The scheme shows how to wire the MH Sensor Series and the LCM1602 IIC V1 to an Arduino Uno.

The IR sensor and the LCM1602 module have only four pins. The GND pins of both modules must be connected to the Arduino’s GND pins. The same applies to the VCC pins which must be connected to the Arduino’s 5V pin. As the Arduino Uno has only a single 5V pin, a mini breadboard is used to “split” the 5V pin. Next, the A0 and D0 pin of the IR sensor must be connected to the Arduino. The A0 pin is the raw analog value (0-1023) of the measured distance between the sensor and an obstacle. In this tutorial, A0 is connected to the Arduino’s A0 pin. The D0 pin is a digital pin that goes to HIGH state if the analog value is greater than or equal to a specific threshold. The threshold can be adjusted by the blue trimpot of the IR distance sensor. Here, D0 is connected to the Arduino’s pin 8.
As a last step, the LCM1602 module’s SDA and SCL pins must be connected to the corresponding SDA and SCL pins of the Arduino Uno.

Example source code

// (c) Michael Schoeffler 2017, http://www.mschoeffler.de
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // initializes the LCM1602 IIC V1 (LCD module)
// 0x27 is the I2C address. This address might be different.

const int IN_A0 = A0; // analog input
const int IN_D0 = 8; // digital input

void setup() {
  pinMode (IN_A0, INPUT);
  pinMode (IN_D0, INPUT);
  lcd.begin(16, 2); // begins connection to the LCD module
  lcd.backlight(); // turns on the backlight
}

int value_A0;
bool value_D0;

void loop() {

  value_A0 = analogRead(IN_A0); // reads the analog input from the IR distance sensor
  value_D0 = digitalRead(IN_D0);// reads the digital input from the IR distance sensor

  lcd.setCursor(0, 0); // sets the cursor of the LCD module to the first line
  lcd.print("A0:");
  lcd.setCursor(3, 0); // sets the cursor of the LCD module to the fourth character
  lcd.print(value_A0); // prints analog value on the LCD module

  lcd.setCursor(0, 1); // sets the cursor of the LCD module to the first line
  lcd.print("D0:");
  lcd.setCursor(3, 1); // sets the cursor of the LCD module to the fourth character
  lcd.print(value_D0); // prints digital value on the LCD module

  delay(1000);
}

If the code has been compiled and transmitted to the Arduino Uno, the LCD module should show the distance between the IR distance sensor and an obstacle. Keep in mind that the distance is indicated by a analog value between 0 and 1023. Unfortunately, it is very challenging to convert the analog value to a metric unit of length, such as meter or centimeter. The reason is that the measured analog value is strongly influenced by the obstacle’s material. For example, black surface does reflect far less light than white surface. As a consequence, the measured analog value will differ. Interestingly, this characteristic can be used in order to use the IR distance sensor as a “black or white” detector. This application can often be found in “car assembly kits” where multiple IR sensors are mounted on the undercar. As a result, the car is capable of following a black line that is drawn on white ground.

The following pictures show distance measurements with black and white material. Although the distance is about the same, the measured analog value (A0) differs strongly:

The IR distance sensor measures black material. The analog sensor value is much higher than when measuring white material (A0 = 949).

The IR distance sensor measures white material. The analog sensor value is much lower than when measuring black material (A0 = 525).

Video tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *