Introduction
In this tutorial, we are going to learn how to use or how to interface an IR sensor to an Arduino UNO. We can use IR as both analog and digital sensors. Here I have taken a digital sensor.
IR Sensor Theory
We can count the no of an object or detect the object using an IR sensor. Generally We can directly connect the microcontroller ( Arduino, NodeMCU, and Raspberry Pi) to the digital IR sensor. Therefore I am taking the Arduino UNO.
![Digital IR sensor](https://techforfun.in/wp-content/uploads/2020/07/Copy-of-ir-sensor-1.png)
There are two major parts in the IR sensor, IR transmitter known as IR led, and an IR receiver known as the photodiode. The main objective of IR led is to transmit infrared waves, and the photodiode is to receive infrared waves.
![ir sensor image 1](https://techforfun.in/wp-content/uploads/2020/07/Copy-of-ir-sensor-2.png)
Therefore IR led always transmit infrared waves, whenever there is an object in front of the IR sensor the infrared waves reflect, and the photodiode receives the IR light and gives the output value as 0
. whenever there is no object in front of the IR sensor the infrared waves will not reflect, and the photodiode will not receives the IR light and gives the output value as 1
. But the object should be brighter because we know a dark colour or black colour absorbed the light
![if object is present or not present](https://techforfun.in/wp-content/uploads/2020/07/ir-object.png)
Parts Required
Parts | Quantity |
---|---|
Arduino UNO | 1 |
A-B cable | 1 |
IR Sensor Module | 1 |
Led | 1 |
Bread Board | 1 |
220 ohm | 1 |
Connecting Wire | As per required |
Circuit Diagram
![circuit diagram of arduino uno and IR sensor](https://techforfun.in/wp-content/uploads/2020/07/ir-sensor.png)
![lay out diagram IR remote and Arduino UNO](https://techforfun.in/wp-content/uploads/2020/07/ir-layout.png)
Program
For programming this concept we need to understand some basic syntax of Arduino IDE. The first thing is we need to install Arduino . After that, we need to know how to interface led to Arduino.
For control led we need to declare led as output
pinMode(PIN Number, Device TYPE)
PIN Number– The pin, in which we have connected the device. In this case, I have attached multiple Led to 12 no pin of Arduino UNO.
Device Type– What is the device type. Like, For output devices we need to declare the type as OUTPUT and for input devices, we need to declare as INPUT. In this case, we have connected Led. As we know, the led is an output device. so we have to program
//declar all led as output pinMode(12, OUTPUT)
After declare output devices we need to declare the input devices like push button.
//declar all IR sensor as input pinMode(5, INPUT)
After declare we need to turn on/ off led. so, to do that we need to understand about digitalWrite()
digitalWrite(PIN Number, Status)
PIN Number– in which pin we have connected the device. In this case i have attached to 7 no pin of Arduino UNO
Status– status means we need to turn the led on/off. For turning led on we can write HIGH or 1 and for turning off led we can write LOW or 0.
Code
Code 1
Bellow program shows how to turn led on and of by detecting object using IR sensor
/* Tested by techforfun.in object detection using IR sensor */ //declare all devices int led = 12; int sensorPin = 5; int sensorValue =0; void setup(){ //declare all pin for output pinMode(led, OUTPUT); // led as output pinMode(sensorValue, INPUT); //sensor as input } void loop() { sensorValue=digitalRead(sensorPin); // store the sensor value in the variable that is HIGH or LOW if(sensorValue==LOW){ //sensor detected digitalWrite(led, HIGH);// led on } else{ digitalWrite(led, LOW);// led off } }