IR Sensor Module with Arduino: Complete Interfacing and Working Tutorial
Infrared sensors are one of the easiest modules to use with Arduino. They help you detect objects, measure short distances, and sense movement. Because they are cheap and simple, IR sensors are used in line-following robots, obstacle-avoidance robots, counters, and many other projects.
In this tutorial, you will learn how to interface an IR sensor module with Arduino step by step. We will test every function of the module. You will read both digital output and analog distance values. Then we will display the results on the Serial Monitor and later on the LCD1602 I2C display.
The tutorial follows a clear and easy-to-read format. Every feature has complete code and a short explanation. You can also follow my detailed guide on the LCD display here: I2C LCD1602 Arduino Tutorial.

Understanding the IR Sensor Module
In this section, we learn how an IR sensor works, what components it uses, and why it is popular in Arduino projects. Knowing the basics will help you understand the readings and fix issues later.
How the IR Sensor Works (IR LED + Photodiode)
An IR sensor uses an IR LED and a Photodiode to detect objects.
- The IR LED emits infrared light.
- When an object comes close, this light reflects back.
- The Photodiode receives the reflected light.
- The module processes this change and gives an output to Arduino.
Most modules also include a comparator (Op-Amp), a sensitivity knob, and indicator LEDs.
Here is a quick summary:
| Component | Function |
|---|---|
| IR LED | Emits infrared light towards the object |
| Photodiode | Detects reflected IR light |
| Op-Amp Comparator | Converts light intensity into HIGH/LOW |
| Potentiometer | Adjusts detection sensitivity |
| Status LED | Shows when an object is detected |
Types of IR Sensor Modules (Digital and Analog Output)
IR modules generally provide either digital output, analog output, or both.
| Type | Output | Behavior | Common Use |
|---|---|---|---|
| Digital IR Sensor | HIGH/LOW | LOW = object detected | Line follower, obstacle detection |
| Analog IR Sensor | 0–1023 value | Value increases as object comes closer | Distance measurement |
| Dual Output Sensor | DO + AO | Supports both modes | Flexible projects |
Digital sensors are simple and great for beginners. Analog sensors offer more accuracy for short-range distance.
Pinout and Technical Specs
Most IR sensor modules have 3 pins, while some advanced ones have 4 pins.
The image below shows the pinout of IR sensor with 3 pin module.
The table blow explains the function of each pin
| Pin | Description |
|---|---|
| VCC | Power supply (3.3V – 5V) |
| GND | Ground |
| OUT / DO | Digital Output |
| AO (optional) | Analog Output |
Technical Specifications
| Specification | Typical Value |
|---|---|
| Operating Voltage | 3.3V to 5V |
| Detection Range | 2 cm – 30 cm |
| Output Type | Digital / Analog |
| Sensitivity Adjustment | Yes, using a potentiometer |
| Indicator LED | Yes, detection status |
These features make the IR sensor easy to use in Arduino projects.
Connecting the IR Sensor to Arduino
This section shows the simple wiring between the IR sensor module and Arduino. You will connect the sensor, upload a small test sketch, and check the output on the Serial Monitor. This helps you confirm that your IR module is working correctly before moving to advanced features.
Circuit Diagram and Wiring Explained
Connecting the IR sensor to an Arduino is very simple. The module usually has three pins: VCC, GND, and OUT.
The image below shows the connection between Arduino Uno and IR sensor module.
The table below explains the wiring diagram.
| IR Sensor Pin | Arduino Pin |
|---|---|
| VCC | 5V |
| GND | GND |
| OUT (Digital) | D2 (any digital pin) |
This wiring uses digital mode because most basic IR sensors provide a HIGH/LOW output. When nothing is in front of the sensor, the output stays HIGH. When an object comes close, the output becomes LOW.
Make sure:
- The sensor faces forward
- The sensitivity potentiometer is not turned all the way up or down
- You keep your hand 2–10 cm in front of the sensor for testing
Basic Digital Output Test (with Complete Code)
Upload this simple sketch to check if your IR sensor is working:
int irPin = 2; // Digital pin connected to IR sensor
void setup() {
pinMode(irPin, INPUT);
Serial.begin(9600);
}
void loop() {
int state = digitalRead(irPin);
Serial.println(state);
delay(200);
}This code reads the digital signal from the IR sensor and prints it on the Serial Monitor.
Understanding Serial Output Results
When you open the Serial Monitor, you will see 0 or 1 values.
Here is what these values mean:
| Serial Output | Meaning |
|---|---|
| 1 | No object detected |
| 0 | Object detected |
If the output is not changing:
- Adjust the sensitivity potentiometer
- Bring your hand closer to the sensor
- Check wiring again
Using the IR Sensor in Digital Detection Mode
In digital mode, the IR sensor sends HIGH or LOW depending on whether an object is detected. This mode is simple and ideal for projects like line-following robots, obstacle detection, or counters. In this section, we will test the sensor with Arduino and understand how to read its digital output.
Code for Object Detection (Digital Read)
Here’s a complete Arduino sketch to read the digital output of the IR sensor:
int irPin = 2; // Connect IR sensor digital output to Arduino pin 2
int ledPin = 13; // Onboard LED for visual feedback
void setup() {
pinMode(irPin, INPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
int sensorState = digitalRead(irPin);
if(sensorState == LOW) { // Object detected
digitalWrite(ledPin, HIGH);
Serial.println("Object Detected!");
} else { // No object
digitalWrite(ledPin, LOW);
Serial.println("No Object");
}
delay(200);
}Explanation of the Code
irPinreads the sensor’s digital output.ledPinturns on the Arduino onboard LED when an object is detected.- In the
loop(),digitalRead()checks the sensor. - If the output is LOW, it means an object is detected. The LED turns ON, and a message prints to Serial Monitor.
- If the output is HIGH, no object is detected. The LED turns OFF.
delay(200)slows down the printing for easier reading.
This setup helps you visually and digitally confirm the sensor’s detection.
Viewing Results on Serial Monitor
The gif below shows the working in motion. When the object is detected, the LED on Arduino turns ON and the respective message is printed in the serial console as well.
Using the IR Sensor in Analog Distance Mode
Some IR sensor modules provide an analog output, giving a varying voltage based on how close an object is. This allows you to measure distances more precisely compared to digital mode. In this section, we will connect the analog output to Arduino, read the values, and understand what they mean.
Wiring for Analog Output
For analog reading, connect the IR sensor module as follows:
| IR Sensor Pin | Arduino Pin |
|---|---|
| VCC | 5V |
| GND | GND |
| AO (Analog Out) | A0 |
| DO (optional) | Not used |
Note: If your module has both digital (DO) and analog (AO) pins, you can leave the digital pin disconnected when reading analog values.
Make sure the sensor faces the object for accurate distance readings.
Code to Read Analog Distance
Upload this sketch to your Arduino:
int irPin = A0; // Connect IR sensor analog output to Arduino A0
void setup() {
Serial.begin(9600);
}
void loop() {
int sensorValue = analogRead(irPin);
Serial.print("Analog Value: ");
Serial.println(sensorValue);
delay(200);
}
Explanation of the Code
irPinis connected to the sensor’s analog output (AO).analogRead(irPin)reads values from 0 to 1023, depending on the distance of the object.- Higher values indicate that the object is closer, lower values mean the object is farther away.
Serial.println()prints the value on the Serial Monitor for observation.delay(200)slows down readings for easier monitoring.
By using analog mode, you can measure small changes in distance and use them in more advanced Arduino projects, like obstacle avoidance or automatic counters.
Displaying IR Sensor Readings on LCD1602 I2C
In this section, we will display the IR sensor readings on an LCD1602 I2C display. This allows you to see the output without opening the Serial Monitor. If you are new to I2C LCDs, you can follow my I2C LCD1602 Arduino Tutorial for setup and basics.
Wiring IR Sensor + I2C LCD1602 with Arduino
The image below shows the wiring connection between IR Sensor, LCD1602 and Arduino Uno.
Here is the complete wiring table for both, the sensor and the LCD:
| IR Sensor Pin | Arduino Pin |
|---|---|
| VCC | 5V |
| GND | GND |
| OUT | D2 |
| LCD Pin | Arduino Pin |
|---|---|
| GND | GND |
| VCC | 5V |
| SDA | A4 (for Arduino UNO) |
| SCL | A5 (for Arduino UNO) |
Both modules work on 5V, so wiring is very simple.
Installing the LiquidCrystal_I2C Library
Before writing the code, install the LiquidCrystal_I2C library:
- Open Arduino IDE.
- Go to Sketch → Include Library → Manage Libraries.
- Search for “LiquidCrystal_I2C”.
- Install the version by Frank de Brabander or Marco Schwartz.
Printing Digital Output on LCD1602
Here is an example to show digital detection on the LCD:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16x2 LCD
int irPin = 2; // Digital pin connected to IR sensor
void setup() {
pinMode(irPin, INPUT);
lcd.init();
lcd.backlight();
}
void loop() {
int sensorState = digitalRead(irPin);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("IR Digital State:");
lcd.setCursor(0, 1);
if(sensorState == LOW) {
lcd.print("Object Detected");
} else {
lcd.print("No Object ");
}
delay(500);
}Explanation:
lcd.clear()refreshes the display each loop.lcd.setCursor()positions text on the screen.- The first row shows the title, the second row shows object detection status.
- The LCD updates every 500ms.
Output on the LCD1602
The gif below shows the output of the code on the LCD. When the object is detected by the sensor, the “Object Detected” is printed on the display.
Printing Analog Distance on LCD1602
For analog reading from the IR sensor:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16x2 LCD
int irPin = A0; // Analog output pin of IR sensor
void setup() {
lcd.init();
lcd.backlight();
Serial.begin(9600);
}
void loop() {
int sensorValue = analogRead(irPin);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("IR Analog Value:");
lcd.setCursor(0, 1);
lcd.print(sensorValue);
Serial.println(sensorValue); // Optional, for monitoring
delay(500);
}Explanation:
analogRead()reads the distance value from the sensor (0–1023).- The value is displayed on the second row of the LCD.
- The first row shows a descriptive title.
Serial.println()is optional for real-time monitoring on the Serial Monitor.
This setup makes it easy to see both digital and analog IR sensor readings without needing a computer.
Adjusting the IR Sensor Sensitivity
The IR sensor has a potentiometer that allows you to adjust its detection sensitivity. Proper adjustment ensures the sensor can detect objects accurately without false triggers. In this section, we will learn how to fine-tune the sensor and optionally use Arduino to calibrate it.
How to Use the Potentiometer
- Locate the small screw on the IR sensor module. This is the potentiometer.
- Turn it clockwise to increase sensitivity (sensor detects objects from farther distance).
- Turn it counterclockwise to decrease sensitivity (sensor detects objects only when closer).
- Test the sensor by moving your hand in front of it and observing the LED indicator or Serial Monitor.
Tip: Adjust slowly in small increments and check the output each time.
IR Sensor Applications in Arduino Projects
Infra-Red sensors are simple, reliable, and widely used in Arduino projects. They can detect objects, measure distances, or sense motion. In this section, we will look at some of the most common and practical applications where IR sensors shine.
Line Following Robot
A line following robot uses IR sensors to detect lines on the ground, usually black lines on white surfaces.
- The IR sensor detects contrast between the line and the surface.
- Digital output helps the Arduino determine if the robot is on the line or needs to adjust its direction.
- Multiple IR sensors can be used to control left and right wheels independently.
the gif below shows how the IR sensor can detect white lines when tuned to proper sensitivity.
Obstacle Avoidance Robot
IR sensors are often used in obstacle avoidance robots.
- The sensor detects objects in front of the robot.
- When an object is detected, the Arduino changes the robot’s path to avoid collision.
- Digital or analog readings can control turning angles or speed adjustments.
Automatic Counter or Object Detection
IR sensors can be used for automatic counting or object detection systems.
- Place the sensor near a path where objects pass.
- Each time an object crosses, the sensor triggers a digital signal.
- Arduino can count the objects and display the count on an LCD or Serial Monitor.
Below is the code to use IR Sensor as automatic counter.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16x2 LCD
int irPin = 2; // Digital pin connected to IR sensor
int count = 0;
void setup() {
pinMode(irPin, INPUT);
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Object Counter");
lcd.setCursor(0, 1);
lcd.print("count: ");
}
void loop() {
if(digitalRead(irPin) == LOW) {
while (digitalRead(irPin) == LOW); // prevent debouncing
count++;
}
lcd.setCursor(7, 1);
lcd.print(count);
delay(200);
}The gif below shows the IR sensor with Arduino working as object counter.
Conclusion
In this tutorial, we explored how to interface an IR sensor module with Arduino in a clear, step-by-step manner. You learned how to use both digital and analog outputs, connect the sensor to Arduino, and display readings on the Serial Monitor and LCD1602 I2C. We also covered adjusting sensitivity and discussed practical applications like line-following robots, obstacle avoidance, and automatic counters.
IR sensors are easy to use, low-cost, and versatile, making them ideal for beginners and advanced Arduino projects alike. By mastering their use, you can build smart, interactive, and precise systems for robotics, automation, and IoT projects.
With this foundation, you can now combine IR sensors with other components to create more complex and real-world Arduino applications.
Browse More Arduino Sensors Tutorials
DS1307 Arduino Tutorial: Interfacing RTC Module with LCD1602 I2C Display
Arduino BME280 Tutorial: Wiring, Pinout, Code and LCD1602 Display Output
HC-SR04 Arduino Tutorial: Measure Distance and Display on Serial Monitor & LCD1602 I2C
Interface SHT21 Temperature and Humidity Sensor with Arduino | Display on Serial Monitor and I2C LCD
Interface AHT20 Sensor with Arduino | Measure Temperature and Humidity with OLED Display
MPU6050 Arduino I2C Tutorial: Pinout, Wiring, Calibration, Raw Data, and Code Examples
Arduino PIR Motion Sensor Tutorial: Wiring, Modes, Sensitivity & Full Codes
Arduino IR Sensor Project Download
Info
You can help with the development by DONATING Below.
To download the project, click the DOWNLOAD button.
Arduino IR Sensor FAQs
This usually happens due to ambient light interference or electrical noise. Slightly reduce sensitivity using the potentiometer and keep wires short.
Yes, but you must keep a small physical gap between them and reduce sensitivity to prevent overlap of IR reflections.
White or shiny surfaces reflect IR differently. Adjust the threshold or use an IR sensor designed for reflective detection.
They work, but sunlight can affect accuracy. Add a small shade or use modulated IR sensors (like TCRT5000 modules).
If your module provides AO (Analog Output), you can read varying levels to fine-tune detection instead of simple HIGH/LOW signals.










