Skip to main content

Getting Started with Arduino and IR Modules: An Easy Guide to Implementing Home Automation and Object Detection with Code

Getting Started with Arduino and IR Modules: An Easy Guide to Implementing Home Automation and Object Detection with Code:

An infrared proximity sensor or IR Sensor is an electronic device that emits infrared lights to sense some aspect of the surroundings and can be employed to detect the motion of an object. As this is a passive sensor, it can only measure infrared radiation. This sensor is very common in the electronic industry and if you’ve ever tried to design an obstacle avoidance robot or any other proximity detection-based system, chances are you already know about this module, and if you don’t, then follow this article as here we will discuss everything about it.

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.

GND Should be connected to the ground of the Arduino.

How Does an IR Motion Sensor Module Work?

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.

IR Motion Sensor Module – Parts

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.

Circuit Diagram for IR Motion Sensor Module

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.


In the schematic, we have an IR LED as Transmitter and the Photodiode as a Receiver. If an object is in front of the sensor, the reflected light from the object is received by the photodiode, and depending upon the intensity we can determine how far or how close the object is. In the schematic, you can also find an LM358 Op-Amp which is doing all the comparison work and generating the output. Other than that, there is a potentiometer that can be used to adjust the sensitivity of the IR Sensor module or the triggering distance for this module.

IR Sensor with Arduino UNO – Connection Diagram

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. 

Arduino IR Sensor Module Circuit Connections

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.

Connecting IR Sensor Module with Arduino Uno

With that, you’re now ready to upload some code and get the IR Motion Sensor working. You can also check out the above module in action at the bottom of this article.

Arduino Code for Interfacing IR Motion Sensor Module with Arduino

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

int IRSensor = 9; // connect IR sensor module to Arduino pin D9
int LED = 13; // connect LED to Arduino pin 13

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
  }
}

That is the end of our simple Arduino based IR sensor code.

Working of the IR Motion Sensor Module

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 buy 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.



Code

// Arduino IR Sensor Code

int IRSensor = 9; // connect ir sensor module to Arduino pin 9

int LED = 13; // conect LED to Arduino pin 13

void setup()

{

  Serial.begin(115200); // Init Serila 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

 

  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 Ended!"); // print Motion Detected! on the serial monitor window

  }

  else

  {

    //else turn on the onboard LED

    digitalWrite(LED, HIGH); // LED High

    Serial.println("Motion Detected!"); // print Motion Ended! on the serial monitor window

  }

}


Comments

popular posts

AX2358 Homemade 5.1 Remote Kit with 16x2 LCD

AX2358 Homemade 5.1 Remote Kit with 16x2 LCD                                               AX2358 is I used for this 5.1 remote kit project. It is Built-in 2-channel to 6-channel converter and 6-channel volume controller. It has 4 stereo inputs and one 6 channel input. Any stereo input terminals of AX2358 are selected, it will be directly converted to 6 channels and then output through volume adjustment but when the signal from the 6-channel input terminal is selected, it directly enters the volume adjustment and then outputs it without any processing. It has 6-channel individual volume control in this IC (0 to -79dB, 1dB/step). This IC working by I2C communication. The data are transmitted to the AX2358 via the SDA and SCL. So I need a microcontroller. I choose the microcontroller is Atmega328 for this. This microcontroller programmed with Arduino UNO.      ...

DIY Tutorial Resetting Toner Chip on Ricoh SP C250DN Printer in Simple Steps

  DIY Tutorial Resetting Toner Chip on Ricoh SP C250DN Printer in Simple Steps The RICOH SP 250DN printer toners have a chip that keeps track of the number of pages that have been printed. This is anoying because it will render a refill useless. In order to reuse this kind of toner, there are two things you need to do: refill the toner with ink (if needed) reset the toner chip (or replace it) There is plenty of information explaining how to refill the toner but little information on how to erase the toner chip. This document deals with the second point: how to dump the chip and reset it. It took me a while to get everything setup and to have my toner chip reset so I would like to share this process in order to help other to do the same with their printer toner cartridges. I will step through the process of understanding the problem, analysing the chip circuit, dumping the chip contents and writing back a pattern so the printer will be able to initialize the chip and set t...

Wireless Joystick controlled Robot Car using Arduino, 433Mhz RF and L298N Motor Driver

    Wireless Joystick controlled Robot Car using Arduino – In this tutorial, you will learn how to control a Robot Car wirelessly using Arduino, L298N Motor driver, and  433 Mhz RF transmitter and Receiver . The robot control system can be activated and de-activated using the Built-in Joystick push Button. Arduino Uno Arduino Nano Mega 2560: Robot Car chassis kit: L298N motor driver: 2-Axis Analog Joystick 433 MHz Transmitter and Receiver Modules: DISCLAIMER: Please Note: these are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way! wireless joystick Interfacing: Watch this tutorial for the robot parts assembling and connections. All the connections are exactly the same as explained in my previous tutorial. The only modification that I did is the addition of the 433 Mhz RF receiver module…the VCC pin of the receiver module is connected with the 5 volts…the ground pin of the ...

DIY Metal Detector using Arduino A Simple Guide to Building Your Own at Home

  DIY Metal Detector using Arduino A Simple Guide to Building Your Own at Home How does a metal detector work? As we can see there are three things that are using to complete the whole project. Electronic circuit, Arduino, and a copper coil. here actually we are making a proximity sensor that detects the metal with a  metal detector  using Arduino . Here are the steps to build a DIY metal detector using an Arduino: Materials required: Arduino UNO Copper coil Diode 10nf capacitor Buzzer Led 220 ohm &  330 Ohm Resistor Jumper wires and a breadboard USB cable for uploading the code Circuit Diagram Arduino metal detector:-  Arduino UNO 10 nf Capacitor A2 Pin Terminal 1 GND Terminal 2 Arduino UNO Buzzer D6 Pin Positive GND  Negative Arduino LED 220 Ohm Resistor D5 Pin   Terminal 1   Anode Pin Terminal 2 GND Cathode Pin   Arduino UNO Copper coil Diode 330-ohm res   Terminal 1 Terminal 1 Terminal 1 A1 Pin     Terminal 2 A2 Pin ...

How to Calculate Inverter Battery Backup Time

  How to Calculate Inverter Battery Backup Time? Backup Time (in hours) = Battery Capacity (in Ah) X Input voltage (V) / Total Load (in Watts) Whenever you plan to buy an inverter battery, there is a desire to know some important information in your mind. Like how much will be the backup of the inverter battery, how many hours of backup will be available for how many ah batteries, etc. In this informative information, here you will know the backup time calculation formula.   Taking your inputs, you can calculate your total load, like: 3 tube led lights = 40 x 3 = 120 Watts 2 fans = 75 x 2 = 150 Watts 1 Bulb router = 1×20 Watts = 20 Watts So, the total load in your case is 120 + 150 + 20 = 290 Watts. Now, let us apply all these values in the above-said battery backup time formula.  Backup Time (in hours) = Battery Capacity (in Ah) X Input voltage (V) / Total Load (in Watts) Backup Time (in hours) = 150 x 12 / 290 = 6.2 So, your inverter battery will last around 6.2 hours ....