In this post, we will learn How to Interface IR Sensor with Arduino. It’s is one of the most commonly used sensor and it is used for multiple purposes such as Line following Robot, Obstacle avoiding Robot, Controlling LEDs with IR Sensor, color detection, fire detection, etc.
[adsforwp id=”3715″]
So Today we are going to cover step by step guide on How to Interface Infrared Sensor with Arduino.
Interfacing of IR Sensor with Arduino
What is IR Sensor?
An Infrared sensor or commonly known as IR Sensor is an electronic device, used to sense the heat of an object and motion of an object. Basically, an IR Sensor can detect Infrared radiation that is not visible with naked eyes. A body when heated radiates infrared light which can be detected by IR Sensor.
[adsforwp id=”3715″]
IR Sensor is consist of two circuits, IR transmitter, and IR receiver. In the transmitter section, IR LED is used and in the Reciever section, a photodiode is used. IR LED transmit Infrared light to an object and then that IR light is bounced back and IR receiver, receives that light and convert it to the electric voltage accordingly.
Picture down below shows the IR Sensor and Operation of IR Sensor.
[adsforwp id=”3713″]

IR Sensor Module:
[adsforwp id=”3713″]
IR Sensor module is used with Arduino to detect infrared radiations that are not visible to the human eye. It is mostly used for robot obstacle avoidance, obstacle avoidance car, line count and black and white line tracking.
Also Read: What is Arduino Micro-controller? And How to Learn Arduino
Specification of IR Sensor Module:
-
- It has a receiving range of 2 ~ 30 cm.
- working voltage of 3 .3 V to 5 V.
- working current of 1.5 mA.
- operating temperature range from -25 C to 85C.
IR Sensor Module Pins:
- +5v Source: IR sensor Module require +5v to work perfectly.
- Ground: In order to get operation from IR Sensor module we need to provide ground to the sensor module.
- OUT Pin: OUT pin is important pin it is used to connect sensor with microcontroller‘s input or output port.
Components required:
- Arduino Uno
- IR Sensor Module
- Jumper Wires
- Breadboard (Optional)
Let’s Interface IR Sensor with Arduino:
[adsforwp id=”3725″]
The connection for Interfacing of IR Sensor with Arduino is very easy, connect VCC of a module with Arduino 5v pin, connect the ground of module with Arduino’s Ground and connect OUT pin of a module with a 4th digital pin of Arduino.
Note: do not get confused if you feel any query do comment down below we will respond you as soon as possible.
Source code for Interfacing IR Sensor with Arduino
/* Interfacing of IR Sensor with arduino code Purpose: Turns on an LED when object is detected, else off. copyrights: www.studentsheart.com */ [adsforwp id="3725"] const int IRSensor=4; void setup() { // initialize the digital pin as an output. // Pin 13 has an builtin LED connected on most Arduino boards: pinMode(13, OUTPUT); //Pin 4 is connected to the output of IR sensor pinMode(IRSensor,INPUT); } void loop() { if(digitalRead(IRSensor)==HIGH) //Check the sensor output { digitalWrite(13, HIGH); // set the LED on } else { digitalWrite(13, LOW); // set the LED off } }
Note: by varying sensor’s potentiometer you can ensure that the sensor is correctly working or not!
Just place an object in front of a sensor and observe the change in LED connected to pin 13 (Built-in Arduino’s LED). If the LED is high it means the sensor is working if the LED is low vary the potentiometer till the LED goes high.
Now remove the object and see if LED goes low it means the sensor is working correctly.
Read More: Download any E-Book for Free from Z-Library
Read More: How to Interface Ultrasonic Sensor with Arduino
lelisa
i have used this code in ,y project but,it couldnot tell me that there is an obstacle or not ,so what will be the problem?
Sohailanwar
Hey Lelisa,
Thanks for your comment, after uploading this code Just place an object in front of a sensor and observe the change in LED connected to pin 13 (Built-in Arduino’s LED).
If the LED is glowing it means the sensor is working if the LED is low vary the potentiometer till the LED goes high.
Now remove the object and see if LED goes low it means the sensor is working correctly.
Aqsa
I want to count the drops of water using ir sensor but it is not working .could u plz suggest me code for detection of drops.
Sohailanwar
Hey Lelisa,
Thanks for your comment, and try to vary the potentiometer till the LED goes high.
Jackson Chu
May I post a source code here and have you tell me if I did it correctly or not?
StudentsHeart Team
Hey Jackson Chu,
Thanks for your comment, and sure upload your code here we will rectify or verify it.
~ Keep visiting StudentsHeart.com
Amir Hameed
sir if i have to use two IR sensor so where i have to make connections of 2nd IR sensor and what’s its coding then.
StudentsHeart Team
Hey Amir Hameed,
Thanks for your comment, and in order to use another IR sensor with the Arduino just connect Output pin of IR sensor with any digital pin of Arduino (say 5). and remaining GND and VCC of sensor should be connected with GND pin of Arduino and VCC with Arduino VCC.
and you have add one more line in the data declration section: below I have added for you.
const int IRSensor=4;
const int IR=5;
void setup() {
// initialize the digital pin as an output.
// Pin 13 has an builtin LED connected on most Arduino boards:
pinMode(13, OUTPUT);
//Pin 4 is connected to the output of IR sensor
pinMode(IRSensor,INPUT);
}
void loop() {
if(digitalRead(IRSensor)==HIGH) //Check the sensor output
{
digitalWrite(13, HIGH); // set the LED on
}
else
{
digitalWrite(13, LOW); // set the LED off
}
}
Let me know if you have any queries.
~ Keep visiting StudentsHeart.com
Sam Sanchez
Hello! I have been looking all over the internet for a tutorial, and yours is the most simple and easy to follow. Can I ask if you have a code for an IR that actives a mini motor for a fan?
Joven
Hi. I want to determine the duration of time that there is an obstacle in front of the IR sensor. I used the code blow and I only get the startTime. How can I determine the endTime?
long unsigned startTime = 0;
long unsigned endTime = 0;
long unsigned finalTime;
void setup() {
pinMode(2, INPUT);
Serial.begin(9600);
Serial.print(“obstacle timer”);
Serial.println();
delay(300);
}
void loop() {
int IRSens = digitalRead(2);
if(IRSens == LOW && startTime ==0)
{
startTime = millis();
Serial.print(“startTime =”);
Serial.println(startTime);
}
if (IRSens == HIGH && startTime > 0)
{
endTime = millis();
Serial.print(“endTime =”);
Serial.println(endTime);
}
finalTime = endTime – startTime;
Serial.print(“finalTime = “);
Serial.println(finalTime);
oneTime = 0;
twoTime = 0;
}