Maker Pro
Arduino

How a PIR Sensor Works

February 13, 2018 by DIY Partners
 
Share
banner

Using pir motion sensor module make led blink,alarm system and relay module. This will make better security.

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.

PIR sensors are more complicated than many of the other sensors explained in these tutorials (like photocells, FSRs and tilt switches) because there are multiple variables that affect the sensors input and output. To begin explaining how a basic sensor works, we'll use this rather nice diagram. The PIR sensor itself has two slots in it, each slot is made of a special material that is sensitive to IR. The lens used here is not really doing much and so we see that the two slots can 'see' out past some distance (basically the sensitivity of the sensor). When the sensor is idle, both slots detect the same amount of IR, the ambient amount radiated from the room or walls or outdoors. When a warm body like a human or animal passes by, it first intercepts one half of the PIR sensor, which causes a positive differential change between the two halves. When the warm body leaves the sensing area, the reverse happens, whereby the sensor generates a negative differential change. These change pulses are what is detected.

LED Blink

Now when the PIR detects motion, the output pin will go "high" to 3.3V and light up the LED!

Once you have the breadboard wired up, insert batteries and wait 30-60 seconds for the PIR to 'stabilize'. During that time the LED may blink a little. Wait until the LED is off and then move around in front of it, waving a hand, etc, to see the LED light up!

//the time we give the sensor to calibrate (10-60 secs according to the datasheet)
int calibrationTime = 1;       
 
//the time when the sensor outputs a low impulse
long unsigned int lowIn;        
 
//the amount of milliseconds the sensor has to be low
//before we assume all motion has stopped
long unsigned int pause = 5000; 
 
boolean lockLow = true;
boolean takeLowTime; 
 
int pirPin = 2;    //the digital pin connected to the PIR sensor's output
int ledPin = 13;
 

void setup(){
  Serial.begin(9600);
  pinMode(pirPin, INPUT);
  pinMode(ledPin, OUTPUT);
  digitalWrite(pirPin, LOW);
 

  Serial.print("calibrating sensor ");
    for(int i = 0; i < calibrationTime; i++){
      Serial.print(".");
      delay(1000);
      }
    Serial.println(" done");
    Serial.println("SENSOR ACTIVE");
    delay(50);
  }

void loop(){
 
     if(digitalRead(pirPin) == HIGH){
       digitalWrite(ledPin, HIGH);   //the led visualizes the sensors output pin state
       if(lockLow){ 
         //makes sure we wait for a transition to LOW before any further output is made:
         lockLow = false;           
         Serial.println("---");
         Serial.print("motion detected at ");
         Serial.print(millis()/1000);
         Serial.println(" sec");
         delay(50);
         }        
         takeLowTime = true;
       }
 
     if(digitalRead(pirPin) == LOW){      
       digitalWrite(ledPin, LOW);  //the led visualizes the sensors output pin state
 
       if(takeLowTime){
        lowIn = millis();          //save the time of the transition from high to LOW
        takeLowTime = false;       //make sure this is only done at the start of a LOW phase
        }
       if(!lockLow && millis() - lowIn > pause){ 
           //makes sure this block of code is only executed again after
           //a new motion sequence has been detected
           lockLow = true;                       
           Serial.print("motion ended at ");      //output
           Serial.print((millis() - pause)/1000);
           Serial.println(" sec");
           delay(50);
           }
       }
  }

Alarm System


int buzzerPin = 13;                // choose the pin for the buzzer and 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(buzzerPin, OUTPUT);        // declare buzzer as output
 pinMode(inputPin, INPUT);        // declare sensor as input

 Serial.begin(9600);
}

void loop(){
 val = digitalRead(inputPin);  
 if (val == HIGH) {           
   digitalWrite(buzzerPin, LOW); 
   if (pirState == LOW) {
     Serial.println("Motion detected!");
     pirState = HIGH;
   }
 } else {
   digitalWrite(buzzerPin, HIGH);
   if (pirState == HIGH){
     Serial.println("Motion ended!");
     pirState = LOW;
   }
 }
}

 PIR Sensor Using Controlling RELAY Module

int relay = 4;                   //// choose the pin for the RELAY
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(relay, OUTPUT);         //declare RELAY 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, LOW); // turn LED ON
   digitalWrite(relay, LOW);   // turn RELAY 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, HIGH);// turn LED OFF
   digitalWrite(relay, HIGH);// turn RELAY 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;
   }
 }
}

Related Content

Comments


You May Also Like