Introduction
In this tutorial, we will learn how to use or 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.
There are two major parts in the IR sensor, an 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.
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 receive the IR light and gives the output value as 1
. But the object should be brighter because we know a dark color or black color absorbed the light
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
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, to 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. For output devices, we need to declare the type as OUTPUT and for input devices, we need to declare it 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 declaring output devices we need to declare the input devices like the 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.
Final Code
Bellow program shows how to turn led on and of by detecting object using an IR sensor
/*
Tested by robogenesis.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
}
}
Comments