HomeArduino TutorialsArduino SensorsIR Sensor Arduino Tutorial: Interfacing, Calibration, Detection Modes, Codes & LCD1602 Display

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.

IR Sensor Module with Arduino: Complete Interfacing and Working 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.

IR (Infra-Red) Sensor module features, showing IR LEDs, potentiometer, Power and Object detect LEDs and Pinout.

Here is a quick summary:

ComponentFunction
IR LEDEmits infrared light towards the object
PhotodiodeDetects reflected IR light
Op-Amp ComparatorConverts light intensity into HIGH/LOW
PotentiometerAdjusts detection sensitivity
Status LEDShows 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.

TypeOutputBehaviorCommon Use
Digital IR SensorHIGH/LOWLOW = object detectedLine follower, obstacle detection
Analog IR Sensor0–1023 valueValue increases as object comes closerDistance measurement
Dual Output SensorDO + AOSupports both modesFlexible 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.

IR (Infra-Red) Sensor module features, showing IR LEDs, potentiometer, Power and Object detect LEDs and Pinout.

The table blow explains the function of each pin

PinDescription
VCCPower supply (3.3V – 5V)
GNDGround
OUT / DODigital Output
AO (optional)Analog Output

Technical Specifications

SpecificationTypical Value
Operating Voltage3.3V to 5V
Detection Range2 cm – 30 cm
Output TypeDigital / Analog
Sensitivity AdjustmentYes, using a potentiometer
Indicator LEDYes, 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.

Wiring connection between IR sensor module and Arduino Uno.

The table below explains the wiring diagram.

IR Sensor PinArduino Pin
VCC5V
GNDGND
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.

Image showing the output on the Arduino serial monitor when the object is detected by the IR sensor.

Here is what these values mean:

Serial OutputMeaning
1No object detected
0Object 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

  • irPin reads the sensor’s digital output.
  • ledPin turns 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.

gif showing the object is detected by the IR sensor, and the respective output is printed on the serial console of Arduino IDE.

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 PinArduino Pin
VCC5V
GNDGND
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

  • irPin is 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.

Image showing the wiring connection between IR sensor, LCD1602 and Arduino Uno. The LCD is connected via I2C.

Here is the complete wiring table for both, the sensor and the LCD:

IR Sensor PinArduino Pin
VCC5V
GNDGND
OUTD2
LCD PinArduino Pin
GNDGND
VCC5V
SDAA4 (for Arduino UNO)
SCLA5 (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:

  1. Open Arduino IDE.
  2. Go to Sketch → Include Library → Manage Libraries.
  3. Search for “LiquidCrystal_I2C”.
  4. Install the version by Frank de Brabander or Marco Schwartz.
Add the I2C LCD1602 liquidcrystal_I2C library to Arduino IDE.

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.

 gif showing the object is detected by the IR sensor, and the respective output is printed on the LCD1602, which is connected to Arduino via I2C.

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

  1. Locate the small screw on the IR sensor module. This is the potentiometer.
  2. Turn it clockwise to increase sensitivity (sensor detects objects from farther distance).
  3. Turn it counterclockwise to decrease sensitivity (sensor detects objects only when closer).
  4. Test the sensor by moving your hand in front of it and observing the LED indicator or Serial Monitor.
Image showing the potentiometer on the IR sensor module can be used to change the sensitivity.

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.

gif showing the white lines being detected by the IR sensor.

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.

gif showing the object is detected by the IR sensor, and the counter is incremented on the LCD1602, which is connected to the Arduino via I2C.

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

1 2

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

Subscribe
Notify of

0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments