PIR Sensor Interfacing with Arduino UNO
Introduction
PIR Sensor
PIR sensor is used for detecting infrared heat radiations. This makes them useful in applications involving detection of moving living objects that emit infrared heat radiations.
The output (in terms of voltage) of PIR sensor is high when it senses motion; whereas it is low when there is no motion (stationary object or no object).
For more information on PIR sensor and how to use it, refer the topic PIR Sensor in the sensors and modules section.
Interfacing Diagram
Interfacing PIR Sensor with Arduino UNO
Example
Motion detection of living objects using PIR sensor using Arduino.
Upon detection of motion, "Object detected" is printed on serial monitor of Arduino. When there is no motion, "No object in sight" is printed on serial monitor of Arduino.
Sketch For Motion Detection Of Living Object
const int PIR_SENSOR_OUTPUT_PIN = 4; /* PIR sensor O/P pin */
int warm_up;
void setup() {
pinMode(PIR_SENSOR_OUTPUT_PIN, INPUT);
Serial.begin(9600); /* Define baud rate for serial communication */
delay(20000); /* Power On Warm Up Delay */
}
void loop() {
int sensor_output;
sensor_output = digitalRead(PIR_SENSOR_OUTPUT_PIN);
if( sensor_output == LOW )
{
if( warm_up == 1 )
{
Serial.print("Warming Up\n\n");
warm_up = 0;
delay(2000);
}
Serial.print("No object in sight\n\n");
delay(1000);
}
else
{
Serial.print("Object detected\n\n");
warm_up = 1;
delay(1000);
}
}
Using Interrupt
Interfacing Diagram
Sketch
const int PIR_SENSOR_OUTPUT_PIN = 2; /* PIR sensor O/P pin */
void setup() {
pinMode(PIR_SENSOR_OUTPUT_PIN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(2), pir, FALLING); /* Interrupt on rising edge on pin 2 */
Serial.begin(9600); /* Define baud rate for serial communication */
delay(20000); /* Power On Warm Up Delay */
}
void loop() {
}
void pir(){
Serial.println("Object Detected");
}
No comments:
Post a Comment