Movement Detector Using The PIR Sensor

In this article you will learn how to make a movement detector using the PIR Sensor.

Introduction
 
In this article I'll show you how to use and work with a PIR sensor and to sense whether movement is detected or NOT.
 
Requirements
 
To build this project you need the following:
  • Arduino
  • PIR Sensor
  • Bread Board
  • led
  • Jumper Wires
 
                                             Figure:  PIR Sensor
 
Connection
  • PIR Sensor to Digital Pin 8
  • Vcc to 5c
  • Gnd to Gnd 
Led
  • Anode Pin to the digital Pin13
  • Cathode pin to the Gnd  
Programming 
  1. int PIR=8;  
  2. int led=13;  
  3. int minsec=60;  
  4. long last= -minsec*1000;  
  5. void setup()  
  6. {  
  7.   pinMode(PIR,INPUT);  
  8.   pinMode(led,OUTPUT);  
  9.   Serial.begin(9600);  
  10. }  
  11. void loop()  
  12. {  
  13.   long now=millis();  
  14.   if(digitalRead(PIR)==HIGH)  
  15.   {  
  16.     if(now > (last+ minsec*1000))  
  17.     {  
  18.       digitalWrite(led,HIGH);  
  19.       Serial.println("Movement Occured");  
  20.       last=now;  
  21.     }  
  22.     else {  
  23.       Serial.println("Not Occured");  
  24.     }  
  25.     delay(500);  
  26.   }  
  27. }  
Explanation 
 
First we want to initialize the value to the PIR sensor and led. Set the minimum sec value as 60 seconds. In the setup function we want to set the PIR sensor as INPUT and led as OUTPUT. Set the baud rate as 9600 for data transfer between Arduino and PC in the loop function we want to set the condition for PIR. 
  1. if(digitalRead(PIR)==HIGH)    
  2. {    
  3.     if(now > (last+ minsec*1000))    
  4.     {    
  5.       digitalWrite(led,HIGH);    //Led On
  6.       Serial.println("Movement Occured");    
  7.       last=now;    
  8.     }   
If any movement occured ,the PIR sensor value is HIGH and the led is turned ON, else it is in the off condition and prints the value in the serial monitor as movement occurred 
 
Output
 
 
                                                      Figure: Movement Detector
 
Read more articles on Arduino: