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
- int PIR=8;
- int led=13;
- int minsec=60;
- long last= -minsec*1000;
- void setup()
- {
- pinMode(PIR,INPUT);
- pinMode(led,OUTPUT);
- Serial.begin(9600);
- }
- void loop()
- {
- long now=millis();
- if(digitalRead(PIR)==HIGH)
- {
- if(now > (last+ minsec*1000))
- {
- digitalWrite(led,HIGH);
- Serial.println("Movement Occured");
- last=now;
- }
- else {
- Serial.println("Not Occured");
- }
- delay(500);
- }
- }
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.
- if(digitalRead(PIR)==HIGH)
- {
- if(now > (last+ minsec*1000))
- {
- digitalWrite(led,HIGH); //Led On
- Serial.println("Movement Occured");
- last=now;
- }
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: