What is IR Sensor?
An IR sensor is a device that measures the Infrared radiation in its surroundings and gives an electric signal as an output. An IR sensor can measure the heat of an object as well as can detect the motion of the objects.
IR technology is used in our day-to-day life and also in industries for different purposes. For example, TVs use an IR sensor to understand the signals which are transmitted from a remote control. The main benefits of IR sensors are low power usage, their simple design & their convenient features. IR signals are not noticeable by the human eye. The IR radiation in the electromagnetic spectrum can be found in the regions of the visible & microwave. Usually, the wavelengths of these waves range from 0.7 µm to 1000µm. The IR spectrum can be divided into three regions near-infrared, mid, and far-infrared. The near IR region’s wavelength ranges from 0.75 – 3µm, the mid-infrared region’s wavelength ranges from 3 to 6µm & the far IR region’s infrared radiation’s wavelength is higher than 6µm.
As the Infrared Radiation is invisible to our eyes, it can be detected by the Infrared Sensor. An IR sensor is a photodiode that is sensitive to IR light. When IR light falls on the photodiode, the resistances and the output voltages will change in proportion to the magnitude of the IR light received.
Â
read more :Â Top 10 Arduino Projects to Get Started With
Types of IR Sensors
- Active IR Sensor.
- Passive IR Sensor.
1. Active IR Sensor
Active infrared sensors both emit and detect infrared radiation. Active IR sensors have two parts: a light-emitting diode (LED) as an Infrared Radiation transmitter and a Photodiode as an Infrared Radiation receiver. When an object comes close to the sensor, the infrared light from the LED reflects off of the object and is detected by the receiver. Active IR sensors act as proximity sensors, and they are commonly used in obstacle detection systems (such as in robots).
2. Passive IR Sensor
Passive infrared (PIR) sensors only detect infrared radiation and do not emit it from an LED. PIR sensors are most commonly used in motion-based detection, such as in-home security systems. When a moving object that generates infrared radiation enters the sensing range of the detector, the difference in IR levels between the two pyroelectric elements is measured. The sensor then sends an electronic signal to an embedded computer, which in turn triggers an alarm.
How Does an IR Sensor Module Work
Â
Â
As we know till now that the active IR Sensors emit Infrared waves and as-well-as detect the Infrared ways. The IR sensor module exactly works on the same phenomenon. The IR Sensor module contains an Infrared LED and an infrared photodiode.Â
Â
read more :Â Which Arduino Board to Buy
Â
Infrared LED
The Infrared LED looks the same as a normal LED. But the Infrared LED emits light that is invisible to the naked eyes. Whenever the electricity is given to the Infrared LED. it emits infrared light.
Infrared Photodiode
The IR photodiode will be black in color as shown in the picture above. Whenever Infrared waves are applied to the Infrared photodiode, in result the Infrared photodiode changes its resistance, which causes a change in the output voltages.
Interfacing IR Sensor Module with Arduino
Hardware Required
We need the following components:
Â
Â
read more :Â What is Arduino UNO
Building Circuit IR Sensor and Arduino
5V pin of the Arduino |
VCC pin of IR Sensor module |
GND pin of Arduino |
GND pin of IR sensor module |
Pin 10 of Arduino |
OUT pin of IR sensor module |
Pin 13 of Arduino |
One of the pins of Resistor |
Empty pin of Resistor |
Positive pin of the LED |
Negative pin of the LEDÂ |
GND pin of Arduino |
Once, the connection are made as per the given steps above, let’s write a code and upload it to the Arduino board.
Arduino Code for Interfacing IR Sensor Module with Arduino
int LEDpin = 13; |
Uploading the code to the Arduino board:
Step 1: Connect the Arduino board with the computer using a USB cable.
Step 2: Next type the code mentioned above.
Step 3: Select the right board and port.
Step 4: Upload the code to the Arduino.
Step 5: The LED will glow when any obstacle is detected by the IR sensor.
Code Explanation:Â
Firstly, the pins are declared using an int datatype variable LED and assigned pin 13 to it. And created one more int variable obstaclePin and assigned pin 10 to it, And by using an integer type variable hasObstacle we are declaring the state of an obstacle as LOW by default.
int LEDpin = 13;
int obstaclePin = 10;
int hasObstacle = LOW;Â // LOW MEANS NO OBSTACLE
Â
Next we have a void setup() function. The void setup function runs only once in the program. In the void setup() function, we will declare the pins as output or input to the Arduino by using the pinMode function. In the pinMode function, the devices connected to Arduino is either input or output device. In the code, we have 2 pinMode functions in which we are declaring LED as OUTPUT and IR pin i.e., obstaclePin as INPUT. And we have Serial.begin function, this is used to tell Arduino that to start data transmission between Arduino and the computer.
void setup() {
  pinMode(LEDpin, OUTPUT);
  pinMode(obstaclePin, INPUT);
  Serial.begin(9600);
}
Â
Next, we have void loop() function, which runs infinitely in the program. In the void loop() function, first, we are reading the data from the output pin of the IR sensor by using the digitalRead function, the digitalRead function checks the obstaclePin for whether it is HIGH or LOW (1 or 0).
Â
Next, we have an if-else statement. In the if statement we are telling Arduino to turn ON the LED when the IR sensor detects any obstacle and display “Stop something is ahead” on the serial monitor. The obstaclePin becomes HIGH when an obstacle is detected by the IR sensor. In the else statement, we are telling Arduino to keep LED OFF when there is no obstacle and to display “path is clear” on the Serial monitor.
void loop() {
  hasObstacle = digitalRead(obstaclePin);
  if (hasObstacle == HIGH) {
    Serial.println("Stop something is ahead!!");
    digitalWrite(LEDpin, HIGH);
  }
  else {
    Serial.println("Path is clear");
    digitalWrite(LEDpin, LOW);
  }
  delay(200);
}
Â
read more :Â Top 10 Arduino Projects for Beginners
Conclusion:
In this blog we have learned, IR sensors are incredibly versatile and can be used for a variety of applications, from detecting motion to measuring temperature. With different types of IR sensors available, it's essential to choose the right one for your project. By understanding how an IR sensor module works and learning how to interface it with an Arduino board, you can easily incorporate this technology into your next project. By following the code explanation and uploading it to your Arduino board, you'll be up and running in no time! Whether you're a seasoned electronics enthusiast or just starting, the possibilities are endless with IR sensors. So why not get started today and see what you can create with this exciting technology!
Â
Â
If you appreciate our work don't forget to share this post and leave your opinion in the comment box.
Â
Please do check out other blog posts about Arduino Interfacing ACS712 with Arduino , Arduino Interfacing with Ultrasonic Sensor , LED Interfacing with Arduino , Interfacing GSM Module with Arduino , Interfacing MAX30100 Pulse Oximeter with Arduino , How to connect ZMPT101B to Arduino and  How to use Buzzer with Arduino.
Â
Make sure you check out our wide range of products and collections (we offer some exciting deals!)