Final Notes For Infrared Sensor
Final Notes For Infrared Sensor
Hardware
(Arduino & IR Sensor)
IR Sensor Pinout
The IR sensor has a 3-pin connector that interfaces it to the outside world. The connections
are as follows:
VCC is the power supply pin for the IR sensor which we connect to the 5V pin on the Arduino.
OUT pin is a 5V TTL logic output. LOW indicates no motion is detected; HIGH means motion
is detected.
The working of the IR sensor module is very simple, it consists of two main components: the
first is the IR transmitter section and the second is the IR receiver section. In the transmitter
section, IR led is used and in the receiver section, a photodiode is used to receive infrared
signal and after some signal processing and conditioning, you will get the output.
An IR proximity sensor works by applying a voltage to the onboard Infrared Light Emitting
Diode which in turn emits infrared light. This light propagates through the air and hits an object,
after that the light gets reflected in the photodiode sensor. If the object is close, the reflected
light will be stronger, if the object is far away, the reflected light will be weaker. If you look
closely toward the module. When the sensor becomes active it sends a corresponding Low
signal through the output pin that can be sensed by an Arduino or any kind of microcontroller
to execute a particular task. The one cool thing about this module is that it has two onboard
LEDs built-in, one of which lights on when power is available and another one turns on when
the circuit gets triggered.
Excellence | Service | Leadership and Good Governance | Innovation | Social Responsibility | Integrity | Professionalism | Sp irituality
For most of the Arduino projects, this sensor is used to detect proximity or to build obstacle
avoidance robots. This Sensor is popular among beginners as these are low power, low cost,
rugged, and feature a wide sensing range that can be trimmed down to adjust the sensitivity.
This sensor has three pins two of which are power pins leveled VCC and GND and the other
one is the sense/data pin which is shown in the diagram above. It has an onboard power LED
and a signal LED the power LED turns on when power is applied to the board the signal LED
turns on when the circuit is triggered. This board also has a comparator Op-amp that is
responsible for converting the incoming analog signal from the photodiode to a digital signal.
We also have a sensitivity adjustment potentiometer; with that, we can adjust the sensitivity of
the device. Last and finally, we have the photodiode and the IR emitting LED pair which all
together make the total IR Proximity Sensor Module.
Excellence | Service | Leadership and Good Governance | Innovation | Social Responsibility | Integrity | Professionalism | Sp irituality
The IR proximity Sensor module can be powered by both 3.3V and 5V supplies. This enables
the module to be used in both 5V systems like Arduino and 3.3V systems like raspberry pi.
This sensor can detect an object if the distance in between the object and the sensor is 2 to
10 cm. Further you can also control the maximum distance of the sensor by adjusting the trim
pot on the module.
Testing your IR sensor module is easy, just power the Vcc and Gnd pin with 5V and Ground
respectively and bring your hand near the IR sensors. You should be able to see the signal
LED on the module turn on if everything is working.
The schematic diagram of the IR Motion sensor is shown below. The schematic itself is very
simple and needs a handful of generic components to build. If you don't have a prebuilt module
on hand but still want to test your project, the schematic below will come in handy.
Excellence | Service | Leadership and Good Governance | Innovation | Social Responsibility | Integrity | Professionalism | Sp irituality
Now that we have a complete understanding of how an IR sensor works, we can connect all
the required wires to Arduino as shown below.
Connecting the IR sensor to any microcontroller is really simple. As we know this sensor
outputs a digital signal and processing this signal is very easy. There exist two methods to do
so first, you can always check the port in an infinite loop to see when the port changes its state
from high to low, or the other way is to do it with an interrupt if you are making a complicated
project the interrupt method is recommended. Power the IR with 5V or 3.3V and connect
ground to ground. Then connect the output to a digital pin D9. We have just used a Male to
Female Jumper wire to connect the IR sensor module with Arduino board as shown below.
Excellence | Service | Leadership and Good Governance | Innovation | Social Responsibility | Integrity | Professionalism | Sp irituality
With that, you’re now ready to upload some code and get the IR Motion Sensor working.
Programming
(Arduino IDE/C++)
The Arduino IR sensor module code is very simple and easy to understand. We are just
basically keeping track of whether the input to pin D9 is HIGH or LOW.
We initialize our code by declaring two global variables, the first one holds the pin value where
the IR sensor is connected and the second one holds the value where the LED is connected
Next, we have our setup function. In the setup function, we initialize the serial with 115200
baud. Next, we print a statement to check if the serial monitor window is properly working or
not, and then we initialize the IRSensor pin as input and the LED pin as output.
void setup(){
Serial.begin(115200); // Init Serial at 115200 Baud Rate.
Serial.println("Serial Working"); // Test to check if serial is
working or not
pinMode(IRSensor, INPUT); // IR Sensor pin INPUT
pinMode(LED, OUTPUT); // LED Pin Output
}
Next, we have our infinite loop. In the infinite loop, we first read the sensor pin with the
digitalRead() function and store the value to sensorStatus variable. Then we check to see if
the output of the sensor is high or low, if the output of the sensor is high that means no
motion is detected, else motion is detected, we also print this status in the serial monitor
window.
void loop(){
int sensorStatus = digitalRead(IRSensor); // Set the GPIO as Input
if (sensorStatus == 1) // Check if the pin high or not
{
// if the pin is high turn off the onboard Led
digitalWrite(LED, LOW); // LED LOW
Serial.println("Motion Detected!"); // print Motion Detected! on
the serial monitor window
}
else {
//else turn on the onboard LED
digitalWrite(LED, HIGH); // LED High
Serial.println("Motion Ended!"); // print Motion Ended! on the
serial monitor window
}
}
Excellence | Service | Leadership and Good Governance | Innovation | Social Responsibility | Integrity | Professionalism | Sp irituality
The GIF shows the IR sensor module in action, you can notice the LED turn on both on the module and on the
Arduino board as pointed by the red arrows. If you are working with an IR Motion Sensor Module for the first time
then you should get a response similar to this.
Reference
• https://circuitdigest.com/microcontroller-projects/interfacing-ir-sensor-module-with-arduino
Excellence | Service | Leadership and Good Governance | Innovation | Social Responsibility | Integrity | Professionalism | Sp irituality
Code:
void setup()
{
Serial.begin(115200); // Init Serial at 115200 Baud
Serial.println("Serial Working"); // Test to check if serial is working or
not
pinMode(IRSensor, INPUT); // IR Sensor pin INPUT
pinMode(LED, OUTPUT); // LED Pin Output
}
void loop()
{
int sensorStatus = digitalRead(IRSensor); // Set the GPIO as Input
else
{
//else turn on the onboard LED
digitalWrite(LED, HIGH); // LED High
Serial.println("Motion Detected!"); // print Motion Ended! on the serial
monitor window
}
}