IR Sensor Arduino Tutorial: Detect Objects and Show Results on an LCD
An IR sensor module detects nearby objects by sending out infrared light and measuring how much of it returns to a photodiode. This tutorial shows how to wire the module to an Arduino UNO, read its digital output, read the analog output on modules that provide one, and show the results on an LCD1602 I2C display. It also covers sensitivity adjustment, an object counter sketch and a troubleshooting table for the most common problems.
The sketches use digitalRead() and analogRead() only, so no sensor library is needed. Readers who are new to these functions can follow the digitalWrite() and digitalRead() tutorial and the Arduino ADC and analogRead() tutorial first. For other detection and distance sensors, see the PIR motion sensor tutorial, the HC-SR04 ultrasonic sensor tutorial and the GP2Y0A41SK0F distance sensor tutorial.
Table of contents
IR Sensor Arduino Working Principle
An IR sensor module combines an infrared LED, a photodiode and a comparator on one small board. This section explains how these parts turn reflected light into a signal that the Arduino can read.
IR LED and Photodiode Detection
The IR LED sends out infrared light continuously. When an object enters the detection area, part of that light reflects back to the photodiode, and the photodiode passes more current as the reflected light increases. The comparator on the board compares the photodiode signal with a reference voltage set by the potentiometer, and the OUT pin changes state when the signal crosses that reference.
| Component | Function |
|---|---|
| IR LED | Emits infrared light towards the object |
| Photodiode | Receives the light reflected by the object |
| Comparator (usually an LM393) | Converts the photodiode signal into a HIGH or LOW output |
| Potentiometer | Sets the reference voltage, which adjusts the detection sensitivity |
| Power LED and output LED | Show that the module is powered and that an object is detected |
IR Sensor Detection Diagram
The diagram below shows the signal path from the IR LED to the Arduino input.
Digital and Analog IR Sensor Output
IR modules provide a digital output, an analog output, or both, depending on the board.
| Type | Output | Behavior | Typical use |
|---|---|---|---|
| Digital IR sensor (3 pins) | HIGH or LOW | On most modules the output is LOW when an object is detected | Obstacle detection, line following, counting |
| Analog IR sensor (AO pin) | ADC value from 0 to 1023 | The value changes with the amount of reflected light | Relative proximity and surface contrast |
| Dual output sensor | DO and AO | Provides both signals | Projects that need a threshold and a raw value |
Digital modules are the simplest choice and suit most projects. The analog value depends on the surface color, the surface finish and the ambient light, so it shows relative proximity and does not give an accurate distance in centimeters. On many modules with an AO pin, such as TCRT5000 boards, the value falls as the object gets closer, so the direction should be confirmed by printing values from the module in use.
IR Sensor Arduino Pinout and Specifications
Most IR obstacle sensor modules have three pins, and boards with an analog output have four. The labels differ slightly between manufacturers, so the text printed on the board should be matched with the table below.
IR Sensor Pinout Table
| Pin | Description |
|---|---|
| VCC | Power supply, 3.3 V to 5 V |
| GND | Ground |
| OUT or DO | Digital output |
| AO (4-pin modules only) | Analog output |
IR Sensor Specifications
| Specification | Typical value |
|---|---|
| Operating voltage | 3.3 V to 5 V |
| Detection range | About 2 cm to 30 cm, adjustable; the maximum distance depends on the surface color, and white surfaces give the longest range |
| Detection angle | About 35° |
| Output type | Digital, active LOW on most modules; some boards add an analog output |
| Output levels | HIGH equals the module supply voltage, and LOW is close to 0 V |
| Sensitivity adjustment | Onboard potentiometer |
| Indicators | Power LED and output LED |
| Supply current | Varies by module; listings range from under 1 mA to about 20 mA, so the seller’s documentation should be checked |
These values are typical for FC-51 style modules, and individual boards can differ.
IR Sensor Logic Levels
The OUT pin swings between about 0 V and the supply voltage of the module. The ATmega328P on the Arduino UNO reads an input as HIGH above 0.6 × VCC and as LOW below 0.3 × VCC, which is 3.0 V and 1.5 V at a 5 V supply. Older datasheet revisions list 0.2 × VCC for the low threshold. A module powered from 5 V therefore works directly with an UNO or Nano, while a 3.3 V board should power the module from its own 3.3 V pin so that the output never exceeds the voltage the board accepts.
The signal wire only drives an input, so it draws almost no current from the Arduino pin. The pin numbers for each board are listed in the Arduino UNO pinout guide and the Arduino Nano pinout guide.
IR Sensor Arduino Wiring
Connecting the module to an Arduino UNO takes three wires. A short test sketch then confirms that the module works before the later examples are added.
IR Sensor Wiring Diagram
| IR sensor pin | Arduino pin |
|---|---|
| VCC | 5V |
| GND | GND |
| OUT | D2 |
Any digital pin can be used for OUT, and this tutorial uses D2. With nothing in front of the sensor the output stays HIGH, and it goes LOW when an object comes close. A few modules invert this behavior, so the output LED on the board should be compared with the serial output during the first test.
IR Sensor Basic Digital Output Test
Upload the sketch below to confirm that the sensor responds. A hand held 2 cm to 10 cm in front of the sensor works well for this test.
// IR sensor basic test: prints the raw digital output of the module
const uint8_t IR_PIN = 2; // OUT pin of the IR sensor module
void setup() {
pinMode(IR_PIN, INPUT); // The module drives the line, so no pull-up is needed
Serial.begin(9600);
}
void loop() {
int state = digitalRead(IR_PIN); // 1 = no object, 0 = object detected
Serial.println(state);
delay(200);
}The Serial Monitor must be set to 9600 baud, as explained in the Arduino UART tutorial. The values have the following meaning.
| Serial output | Meaning |
|---|---|
| 1 | No object detected |
| 0 | Object detected |
If the value does not change, the checks in the troubleshooting section apply.
IR Sensor Arduino Digital Output Code
In digital mode the module reports HIGH or LOW depending on whether an object is detected. This mode suits line-following robots, obstacle detection and counters, and it needs only one input pin.
IR Sensor Object Detection Code with LED Feedback
The sketch below turns on the onboard LED and prints a message whenever the sensor output is LOW.
// IR sensor object detection with LED feedback
const uint8_t IR_PIN = 2; // OUT pin of the IR sensor module
const uint8_t LED_PIN = LED_BUILTIN; // Onboard LED (pin 13 on the UNO and Nano)
void setup() {
pinMode(IR_PIN, INPUT);
pinMode(LED_PIN, OUTPUT);
Serial.begin(9600);
}
void loop() {
int sensorState = digitalRead(IR_PIN);
if (sensorState == LOW) { // LOW means an object is detected
digitalWrite(LED_PIN, HIGH);
Serial.println("Object Detected!");
} else { // HIGH means no object
digitalWrite(LED_PIN, LOW);
Serial.println("No Object");
}
delay(200); // Keeps the serial output readable
}How the Digital Detection Code Works
- IR_PIN is the input that reads the digital output of the sensor.
- LED_BUILTIN maps to pin 13 on the UNO and Nano, so the onboard LED follows the detection state.
- digitalRead() returns LOW when an object is detected, which turns the LED on and prints a message.
- delay(200) limits the print rate so the Serial Monitor stays easy to read.
IR Sensor Serial Monitor and LED Output
In the animation below the onboard LED turns on and the Serial Monitor prints the detection message when a hand enters the sensor range.
IR Sensor Arduino Analog Output Code
Modules with an AO pin, usually four-pin boards such as TCRT5000 modules, provide a voltage that changes with the amount of reflected light. Reading it with analogRead() gives a finer view of proximity than the digital output. The ADC on the UNO has 10 bits, so the readings range from 0 to 1023 for 0 V to 5 V, as the ADC and analogRead() tutorial explains.
IR Sensor Analog Wiring
| IR sensor pin | Arduino pin |
|---|---|
| VCC | 5V |
| GND | GND |
| AO (analog out) | A0 |
| DO (digital out) | Not used |
A 3-pin module has no analog output. Connecting its digital OUT pin to A0 only returns readings close to 0 or close to 1023, so the digital sketches should be used with those modules.
IR Sensor Analog Read Code
// Reads the analog output (AO) of an IR sensor module and prints the ADC value
const uint8_t IR_ANALOG_PIN = A0; // AO pin of the IR sensor module
void setup() {
Serial.begin(9600);
}
void loop() {
int value = analogRead(IR_ANALOG_PIN); // 10-bit ADC: 0 to 1023 for 0 V to 5 V
Serial.print("Analog value: ");
Serial.println(value);
delay(200);
}Reading the IR Sensor Analog Value
Hold a hand or a sheet of paper at different distances and watch the values in the Serial Monitor. On many TCRT5000 style modules the value falls as the object gets closer, because more reflected light lowers the output voltage, but other boards can behave the opposite way. The direction should therefore be tested on the module in use.
The values also change with the surface color and the ambient light, so they suit relative proximity and threshold checks. For distance measurement between 4 cm and 30 cm, the GP2Y0A41SK0F distance sensor tutorial covers a sensor that was designed for that purpose.
IR Sensor Arduino LCD1602 Display
An LCD1602 with an I2C backpack shows the sensor readings without a computer. The display needs only two signal wires, and the basics of the module are covered in the I2C LCD1602 Arduino tutorial.
IR Sensor and LCD1602 Wiring
| IR sensor pin | Arduino pin |
|---|---|
| VCC | 5V |
| GND | GND |
| OUT | D2 |
| LCD1602 I2C pin | Arduino pin |
|---|---|
| GND | GND |
| VCC | 5V |
| SDA | A4 (UNO and Nano) |
| SCL | A5 (UNO and Nano) |
Both modules run from 5 V, so they share the same supply pin. The I2C pins differ on other boards, and the Arduino I2C tutorial lists them together with an address scanner.
LiquidCrystal_I2C Library Setup
- Open the Arduino IDE and go to Sketch, then Include Library, then Manage Libraries.
- Search for LiquidCrystal I2C.
- Install the version by Frank de Brabander.
The sketches in this tutorial call lcd.init() and lcd.backlight(), which this library provides. Other libraries with the same name can use different function names.
IR Sensor LCD1602 I2C Address
The sketches use the address 0x27. Many LCD backpacks use a PCF8574 chip with addresses from 0x20 to 0x27, and boards with the PCF8574A chip use addresses from 0x38 to 0x3F, so 0x3F is the other common value. Each sketch checks the address at startup and prints a message on the Serial Monitor if the display does not answer.
IR Sensor Digital Output on LCD1602
// Shows the digital state of the IR sensor on an I2C LCD1602
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
const uint8_t LCD_ADDRESS = 0x27; // Use 0x3F if your backpack uses that address
const uint8_t IR_PIN = 2; // OUT pin of the IR sensor module
LiquidCrystal_I2C lcd(LCD_ADDRESS, 16, 2); // 16 columns, 2 rows
int lastState = -1; // Forces the first screen update
void setup() {
Serial.begin(9600);
pinMode(IR_PIN, INPUT);
// Check that the LCD backpack answers on the I2C bus
Wire.begin();
Wire.beginTransmission(LCD_ADDRESS);
if (Wire.endTransmission() != 0) {
Serial.println("LCD not found. Check the wiring and the I2C address.");
}
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("IR Digital State");
}
void loop() {
int state = digitalRead(IR_PIN);
if (state != lastState) { // Update the display only when the state changes
lcd.setCursor(0, 1);
// Both strings are 16 characters long, so the old text is fully overwritten
lcd.print(state == LOW ? "Object Detected " : "No Object ");
lastState = state;
}
delay(50);
}The first row holds a fixed title, and the second row shows the detection state. Updating the display only when the state changes removes the flicker that clearing the screen on every loop would cause.
IR Sensor Analog Output on LCD1602
This sketch needs a module with an AO pin and the wiring from the analog section, with the AO pin connected to A0.
// Shows the analog value of the IR sensor on an I2C LCD1602
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
const uint8_t LCD_ADDRESS = 0x27; // Use 0x3F if your backpack uses that address
const uint8_t IR_ANALOG_PIN = A0; // AO pin of the IR sensor module
LiquidCrystal_I2C lcd(LCD_ADDRESS, 16, 2);
void setup() {
Serial.begin(9600);
// Check that the LCD backpack answers on the I2C bus
Wire.begin();
Wire.beginTransmission(LCD_ADDRESS);
if (Wire.endTransmission() != 0) {
Serial.println("LCD not found. Check the wiring and the I2C address.");
}
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("IR Analog Value:");
}
void loop() {
int value = analogRead(IR_ANALOG_PIN); // 0 to 1023
char line[17];
snprintf(line, sizeof(line), "%-16d", value); // Pads with spaces to 16 characters
lcd.setCursor(0, 1);
lcd.print(line);
Serial.println(value); // Optional output for monitoring
delay(300);
}The value is padded to 16 characters with snprintf(), so a shorter number never leaves old digits on the display.
IR Sensor Arduino Object Counter
An IR sensor can count items that pass through its detection area, for example products on a conveyor or people in a doorway. The Arduino increases a counter each time an object enters the beam and shows the total on the LCD1602.
IR Sensor Counter Logic
- The sensor is placed beside the path of the objects so that each object crosses the detection area.
- The sketch counts once when the output changes from HIGH to LOW, which marks the arrival of an object.
- A 50 ms debounce time ignores short glitches, and the loop never blocks, so the display and the sensor keep updating.
IR Sensor Object Counter Code
The wiring is the same as in the LCD1602 section, with OUT connected to D2.
// IR sensor object counter with LCD1602 I2C output
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
const uint8_t LCD_ADDRESS = 0x27; // Use 0x3F if your backpack uses that address
const uint8_t IR_PIN = 2; // OUT pin of the IR sensor module
const unsigned long DEBOUNCE_MS = 50; // Ignore state changes shorter than 50 ms
LiquidCrystal_I2C lcd(LCD_ADDRESS, 16, 2);
unsigned long count = 0; // Number of detected objects
bool lastDetected = false; // Previous sensor state
unsigned long lastChange = 0; // Time of the last accepted state change
void showCount() {
char line[17];
snprintf(line, sizeof(line), "Count: %-9lu", count); // Padded to 16 characters
lcd.setCursor(0, 1);
lcd.print(line);
}
void setup() {
Serial.begin(9600);
pinMode(IR_PIN, INPUT);
// Check that the LCD backpack answers on the I2C bus
Wire.begin();
Wire.beginTransmission(LCD_ADDRESS);
if (Wire.endTransmission() != 0) {
Serial.println("LCD not found. Check the wiring and the I2C address.");
}
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Object Counter");
showCount();
}
void loop() {
bool detected = (digitalRead(IR_PIN) == LOW); // LOW means an object is detected
// Accept a new state only after the debounce time has passed
if (detected != lastDetected && millis() - lastChange > DEBOUNCE_MS) {
lastChange = millis();
lastDetected = detected;
if (detected) { // Count on arrival of the object
count++;
showCount();
}
}
}The sketch reads the sensor continuously and counts on the change of state, so the loop never waits for the object to leave the beam. Objects that move very fast can still be missed by polling, and the Arduino external interrupts tutorial shows how to count them with attachInterrupt().
IR Sensor Object Counter Output
IR Sensor Arduino Sensitivity Adjustment
The potentiometer on the module sets the reference voltage of the comparator, which changes the distance at which the output switches. A correct setting avoids both false triggers and missed detections.
IR Sensor Potentiometer Adjustment
- Power the module and watch the output LED.
- Place the object at the distance where detection should begin.
- Turn the potentiometer slowly with a small screwdriver until the output LED just turns on.
- Move the object away and confirm that the LED turns off at a clear distance.
- Repeat the test with the real surface that the sensor will see.
Module manuals usually state that clockwise rotation increases the detection distance and counterclockwise rotation reduces it. The direction can differ between boards, so the output LED should confirm it on the module in use.
IR Sensor Range and Surface Effects
The detection distance also depends on the target. Light colored surfaces reflect more infrared light and give the longest range, while black or dark surfaces absorb most of it and are detected only at a short distance. At the longest sensitivity setting the output can flicker near the switching point, and a slightly lower setting gives cleaner transitions. Direct sunlight contains infrared light, so indoor use is recommended.
IR Sensor Arduino Applications
The digital output and the low cost make the IR sensor module a common choice for small robots and counting systems. The examples below show where the sketches in this tutorial can be reused.
IR Sensor Line Following Robot
A light surface returns more infrared light than a dark one, so the sensor output changes when it crosses from a light area to a dark area. The digital output tells the Arduino whether the robot is on the line, and several sensors let the robot steer the left and right wheels independently.
IR Sensor Obstacle Avoidance Robot
Sensors mounted at the front of a robot report objects ahead, and the Arduino changes the direction or the speed of the motors when an object is detected. The PWM and analogWrite() tutorial explains how to control the motor speed.
IR Sensor Automatic Object Counting
Objects on a conveyor or in a doorway can be counted with the sketch in the object counter section, and the total can be shown on the LCD1602.
IR Sensor Proximity Alarm
The sensor output can also trigger a sound when an object comes too close. The passive buzzer tutorial provides alarm and siren sketches that can be started from the detection state.
IR Sensor Arduino Troubleshooting
The table lists the most common problems with the module and the sketches in this tutorial.
| Problem | Likely cause | Fix |
|---|---|---|
| The output never changes | No power, a wrong pin, or the potentiometer at an extreme setting | Check that the power LED is on, confirm that OUT goes to D2, and turn the potentiometer slowly until the output LED reacts |
| The output always shows an object | Sensitivity is too high, or a nearby surface reflects the light | Lower the sensitivity, move the sensor away from the surface and shield it from sunlight |
| The reading flickers when nothing is in front | Long-range setting, ambient light or long wires | Lower the sensitivity, shorten the wires and block direct light |
| Dark objects are not detected | Dark surfaces absorb infrared light | Raise the sensitivity or bring the object closer |
| The Serial Monitor shows nothing or unreadable text | Wrong port or baud rate | Select the correct port and set the Serial Monitor to 9600 baud |
| The LCD stays blank | Wrong I2C address, SDA and SCL swapped, or the contrast set too low | Check the address (0x27 or 0x3F), confirm A4 and A5, and adjust the contrast potentiometer on the backpack |
| The analog value moves in the opposite direction | The direction depends on the module | Print values with and without an object, then adapt the threshold in the sketch |
For serial problems, the Arduino UART tutorial explains the Serial Monitor settings. For LCD address problems, the I2C scanner in the Arduino I2C tutorial lists every device on the bus.
Conclusion
The IR sensor module is a simple way to add object detection to an Arduino project, because it needs only three wires and no sensor library. This tutorial covered the digital and analog outputs, sensitivity adjustment, an LCD1602 display and an object counter with debouncing.
The detection signal can be used to drive a relay, a motor or a buzzer, or it can be combined with a PIR motion sensor for presence detection. For longer distances, the HC-SR04 ultrasonic sensor tutorial is the better choice, and the GP2Y0A41SK0F tutorial covers a sensor with a more consistent analog output between 4 cm and 30 cm.
Download Project Here
Download the CubeMX project files and HAL source code — all tested on real hardware.
If you find this useful, you can support us with a Donation as well.
Browse More Arduino Sensor Tutorials

Arun is an embedded systems engineer with 10+ years of experience in STM32, ESP32, and AVR microcontrollers. He created ControllersTech to share practical tutorials on embedded software, HAL drivers, RTOS, and hardware design — grounded in real industrial automation experience.
Recommended Tools
Essential dev tools
Categories
Browse by platform











