PIR Sensor Using Arduino

PIR Sensor

PIR sensors allow you to sense motion, almost always used to detect whether a human has moved in or out of the sensors range. They are small, inexpensive, low-power, easy to use and don’t wear out. For that reason they are commonly found in appliances and gadgets used in homes or businesses. They are often referred to as PIR, “Passive Infrared”, “Pyroelectric”, or “IR motion” sensors.

How It WorksFirst let’s explain the working principle. The module actually consists of a Pyroelectric sensor which generates energy when exposed to heat.

 Image result for pir sensorImage result for pir sensor

That means when a human or animal body will get in the range of the sensor it will detect a movement because the human or animal body emits heat energy in a form of infrared radiation. That’s where the name of the sensor comes from, a Passive Infra-Red sensor. And the term “passive” means that sensor is not using any energy for detecting purposes, it just works by detecting the energy given off by the other objects.

The module also consists a specially designed cover named Fresnel lens, which focuses the infrared signals onto the pyroelectric sensor.

The module has just three pins, a Ground and a VCC for powering the module and an output pin which gives high logic level if an object is detected. Also it has two potentiometers. One for adjusting the sensitivity of the sensor and the other for adjusting the time the output signal stays high when object is detected. This time can be adjusted from 0.3 seconds up to 5 minutes.

Image result for pir sensor

The module has three more pins with a jumper between two of them. These pins are for selecting the trigger modes. The first one is called “non-repeatable trigger” and works like this: when the sensor output is high and the delay time is over, the output will automatically change from high to low level. The other mode called “repeatable trigger” will keep the output high all the time until the detected object is present in sensor’s range.

PIR MOTION DETECTOR WITH SERIAL MONITOR

Components Required

  • HC-SR501 PIR Sensor Module
  • Arduino Board
  • Breadboard and Jump Wires

Circuit Description

Connecting PIR sensors to a microcontroller is really simple. The PIR acts as a digital output so all you need to do is listen for the pin to flip high (detected) or low (not detected).Its likely that you’ll want reriggering, so be sure to put the jumper in the H position!

Power the PIR with 5V and connect ground to ground. Then connect the output to a digital pin. In this example we’ll use pin 2.

proximity_pirardbb.gifThe code is very simple, and is basically just keeps track of whether the input to pin 2 is high or low. It also tracks the state of the pin, so that it prints out a message when motion has started and stopped.

Code

/*  PIR sensor tester Tutorial
 *  www.genbays.com 
 */

int ledPin = 13;                // choose the pin for the LED
int inputPin = 2;               // choose the input pin (for PIR sensor)
int pirState = LOW;             // we start, assuming no motion detected
int val = 0;                    // variable for reading the pin status
 
void setup() {
  pinMode(ledPin, OUTPUT);      // declare LED as output
  pinMode(inputPin, INPUT);     // declare sensor as input
 
  Serial.begin(9600);
}
 
void loop(){
  val = digitalRead(inputPin);  // read input value
  if (val == HIGH) {            // check if the input is HIGH
    digitalWrite(ledPin, HIGH);  // turn LED ON
    if (pirState == LOW) {
      // we have just turned on
      Serial.println("Motion detected!");
      // We only want to print on the output change, not state
      pirState = HIGH;
    }
  } else {
    digitalWrite(ledPin, LOW); // turn LED OFF
    if (pirState == HIGH){
      // we have just turned of
      Serial.println("Motion ended!");
      // We only want to print on the output change, not state
      pirState = LOW;
    }
  }
}

PIR MOTION DETECTOR WITH LCD OUTPUT

Image result for pir sensor using LCD

/*  PIR sensor tester using LCD Tutorial
 *  www.genbays.com 
 */
/*
 * You can find the LCD Library from Arduino official website
 * https://github.com/arduino-libraries/LiquidCrystal
 */
#include <LiquidCrystal.h>

int ledPin = 13;                // choose the pin for the LED
int inputPin = 7;               // choose the input pin (for PIR sensor)
int pirState = LOW;             // we start, assuming no motion detected
int val = 0;                    // variable for reading the pin status

LiquidCrystal lcd (12, 11, 5, 4, 3, 2) ;

void setup() 
{
  pinMode(ledPin, OUTPUT);      // declare LED as output
  pinMode(inputPin, INPUT);     // declare sensor as input
  }

void loop()
{
  val = digitalRead(inputPin);  // read input value
  if (val == HIGH) 
  {         
    // check if the input is HIGH
    digitalWrite(ledPin, HIGH);  // turn LED ON
    if (pirState == LOW) 
    {
      // we have just turned on
      lcd.clear();
      lcd.setCursor(0,0);
      Serial.println("Motion detected!");
      lcd.print("Motion detected!");  
      pirState = HIGH;
    }
  } 
  else 
  {
    digitalWrite(ledPin, LOW); // turn LED OFF
    if (pirState == HIGH)
    {
      // we have just turned of
      lcd.clear();
      lcd.setCursor(0,0);
      Serial.println("Motion ended!");
      lcd.print("Motion ended!");
      pirState = LOW;
    }
  }
}