Skip to main content

Getting Started with Arduino and PIR Sensors for Home Security: An Easy Guide to Implementing Motion Detection with Code

Getting Started with Arduino and PIR Sensors for Home Security: An Easy Guide to Implementing Motion Detection with Code

Arduino Motion Sensor/Detector Project using PIR Sensor
Arduino Motion Sensor/Detector Project using PIR Sensor

Detecting motions or movements has always been important in most projects. With the help of the PIR Sensor it has become very easy to detect human/animal movements. In this project we will learn how we can interface a PIR Sensor with a microcontroller like Arduino. We will interface an Arduino with PIR module and blink a LED and beep a Buzzer whenever a movement is detected. The following components will be needed to build this project.

Materials Required:

  1. PIR Sensor Module
  2. Arduino UNO (any version)
  3. LED
  4. Buzzer
  5. Breadboard
  6. Connecting Wires
  7. 330 ohm resistor

PIR sensor:  

The PIR sensor stands for Passive Infrared sensor. It is a low cost sensor which can detect the presence of Human beings or animals. There are two important materials present in the sensor one is the pyroelectric crystal which can detect the heat signatures from a living organism (humans/animals)   and the other is a Fresnel lenses which can widen the range of the sensor. Also the PIR sensor modules provide us some options to adjust the working of the sensor as shown in below image.

PIR Sensor Pinout

PIR Sensor from Inside

The two potentiometers (orange color) are used to control the sensitivity and trigger on time of the sensor. Basically the Dout pin of the sensor is present in between the Vcc and Gnd pins. The module works on 3.3V but can be powered with 5V as well. On the top left corner it also has a trigger pin setup which can be used to make the module work in two different modes. One is the “H” mode and the other is the “I” mode.

In “H” mode the output pin dout will go high (3.3V) when a person is detected within range and goes low after a particular time (time is set by potentiometer). In this mode the output pin will go high irrespective of whether the person is still present inside the range or has left the area. We are using our module in “H” mode in our project.

In “I” mode the output pin dout will go high (3.3V) when  a person is detected within range and will stay high as long as he/she stays within the limit of the Sensors range. Once the person has left the area the pin will go low after the particular time which can be set using the potentiometer.

Note: The position of potentiometers or pins may vary based on your PIR sensor vendor. Follow the Silk screen to determine you pinouts

Circuit Diagram and Explanation:

The circuit Diagram for Arduino motion detector project by interfacing Arduino with PIR module and blinking an LED/Buzzer is shown in the below image.

Arduino Motion Sensor Circuit Diagram

We have powered the PIR sensor using he 5V Rail of the Arduino. The output pin of the PIR Sensor is connected to the 2nd digital pin of Arduino. This pin will be the INPUT pin for Arduino. Then the 3rd pin of Arduino is connected to the LED and Buzzer. This pin will act as the output pin of the Arduino. We will program the Arduino to trigger an Output on 3rd pin if an Input has been detected at 2nd pin. The complete Program is explained below.

Programming the Arduino:

The program for Arduino is pretty simple and straight forward. To connect Arduino PIR Sensor, we have to assign the pin number 2 as input and pin number 3 as output. Then we have to produce a discontinuous trigger whenever the pin 2 goes high. Each line is explained below.

In the void setup function shown below, we have to declare that the pin 2 connected to PIR output will be used as input and the pin 3 connected to LED/Buzzer will be used as input.

void setup() {

  pinMode(2, INPUT); //Pin 2 as INPUT

  pinMode(3, OUTPUT); //PIN 3 as OUTPUT

}

Then we proceed to the loop() function. As we know the code in here gets executed as long as the MCU is powered on. So we always check if the Pin 2 has gone high by using the below line inside the loop() function.

if (digitalRead(2) == HIGH)

If we find that the particular pin has gone high, it means that the PIR module has be triggered. So, now we has make our output pin (pin 3) to go high. We turn this pin on and off with a delay of 100 milli second so that we can attain the flashing or buzzing output. The code to do the same is shown below.

void setup() {

  pinMode(2, INPUT); //Pin 2 as INPUT

  pinMode(3, OUTPUT); //PIN 3 as OUTPUT

}


void loop() {

  if (digitalRead(2) == HIGH) // check if PIR is triggered.

  {

  digitalWrite(3, HIGH);   // turn the LED/Buzz ON

  delay(100);                       // wait for 100 msecond

  digitalWrite(3, LOW);   // turn the LED/Buzz OFF

  delay(100);                       // wait for 100 msecond

  }

}

Working:

The circuit and program for this Arduino motion detector project is already discussed above. Now, you can build this circuit on a breadboard by following the schematics given above and upload the program which could found at the end of this tutorial. Once your connections are done your set-up should look like something shown below.

Arduino Motion Sensor Project

Now, power on the Arduino and wait for about 50-60 seconds for your PIR sensor to calibrate. Do not be frustrated by the output that you get during this period. After that, try moving in front of the PIR sensor and you LED/Buzzer should be triggered as shown in the video below.

The beeping/flashing should stop after some time; you can now toy around the output by varying the potentiometer to change the sensitivity or the low time of the module. Hope you understood the project and got it working, if you have any trouble in getting this thing work, you can search out through the comment section or on our forums. 


Getting Started with Arduino and PIR Sensors for Home Security: An Easy Guide to Implementing Motion Detection with Code

Code

void setup() {
  pinMode(2, INPUT); //Pin 2 as INPUT
  pinMode(3, OUTPUT); //PIN 3 as OUTPUT
}

void loop() {
  if (digitalRead(2) == HIGH)
  {
  digitalWrite(3, HIGH);   // turn the LED/Buzz ON
  delay(100);                       // wait for 100 msecond 
  digitalWrite(3, LOW);   // turn the LED/Buzz OFF
  delay(100);                       // wait for 100 msecond 
  }
}



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

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

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