About The Project
This project involves creating a simple and effective burglar detection system using a PIR sensor and a buzzer. The PIR sensor detects any motion within its range, which could indicate the presence of an intruder. When motion is detected, the system activates a buzzer, providing an audible alert to warn occupants of potential unauthorized entry.
Scope of The Project
The project provides a cost-effective solution for basic security needs, suitable for residential and small business applications. It can be easily expanded to include additional sensors or integrated into a larger security system for enhanced protection.
PIR Sensor
A PIR (Passive Infrared) sensor is a type of motion sensor that detects infrared (IR) radiation emitted by objects within its field of view, particularly living beings like humans and animals.
How PIR Sensor Output Works:
- Motion Detected (Moving Into or Within the Range):
– Output: The sensor’s output goes HIGH when it detects motion, regardless of whether the motion is towards or away from the sensor. This HIGH state indicates that some movement has been detected within the sensor’s field of view.
- Continued Motion:
– Output: If the object continues to move within the sensor’s detection range, the output will remain HIGH as long as the motion is detected.
- Motion Stops or Object Leaves the Range:
– Output: Once the motion stops or the object moves out of the sensor’s range, the sensor’s output will stay HIGH for a brief period (based on the sensor’s time delay setting) and then return to LOW. The transition from HIGH to LOW does not occur specifically because the object moved away; it happens because the motion has stopped being detected.
Circuit Wiring
Circuit Wiring
// www.matthewtechub.com
// PIR motion detection with Buzzer indication
const int pirPin = 2; // PIR sensor output pin connected to digital pin 3
const int buzzerPin = 3; // Buzzer connected to digital pin 2
int pinStateCurrent = LOW; // Current state of the PIR sensor output
int pinStatePrevious = LOW; // Previous state of the PIR sensor output
void setup() {
Serial.begin(9600); // Initialize serial communication at 9600 bps
pinMode(pirPin, INPUT); // Set the PIR sensor pin as an input
pinMode(buzzerPin, OUTPUT); // Set the Buzzer pin as an output
}
void loop() {
pinStatePrevious = pinStateCurrent; // Save the previous state of the PIR sensor
pinStateCurrent = digitalRead(pirPin); // Read the current state of the PIR sensor
// If motion is detected (LOW -> HIGH transition)
if (pinStatePrevious == LOW && pinStateCurrent == HIGH) {
Serial.println("Motion detected!"); // Print message to Serial Monitor
digitalWrite(buzzerPin, HIGH); // Turn on the Buzzer
}
// If motion stops (HIGH -> LOW transition)
else if (pinStatePrevious == HIGH && pinStateCurrent == LOW) {
Serial.println("Motion stopped!"); // Print message to Serial Monitor
digitalWrite(buzzerPin, LOW); // Turn off the Buzzer
}
}
This code effectively detects motion using the PIR sensor and provides an audible alert via the buzzer when motion is detected, with feedback printed to the Serial Monitor.
Code Explanation
const int pirPin = 2; // Pin connected to the PIR sensor
const int buzzerPin = 3; // Pin connected to the buzzer
int pinStateCurrent = LOW; // Current state of the PIR sensor
int pinStatePrevious = LOW; // Previous state of the PIR sensor
- `const int pirPin = 2;` – Assigns pin 2 to the PIR sensor.
- `const int buzzerPin = 3;` – Assigns pin 3 to the buzzer.
- `int pinStateCurrent = LOW;` – Tracks the current state of the PIR sensor.
- `int pinStatePrevious = LOW;` – Tracks the previous state of the PIR sensor.
void setup() {
Serial.begin(9600); // Begin serial communication at 9600 bps
pinMode(pirPin, INPUT); // Set PIR sensor pin as input
pinMode(buzzerPin, OUTPUT); // Set buzzer pin as output
}
- `Serial.begin(9600);` – Initializes serial communication for debugging.
- `pinMode(pirPin, INPUT);` – Configures the PIR sensor pin as an input.
- `pinMode(buzzerPin, OUTPUT);` – Configures the buzzer pin as an output.
void loop() {
pinStatePrevious = pinStateCurrent; // Store the last state of the PIR sensor
pinStateCurrent = digitalRead(pirPin); // Read the current state from the PIR sensor
// Motion detected: Transition from LOW to HIGH
if (pinStatePrevious == LOW && pinStateCurrent == HIGH) {
Serial.println("Motion detected!"); // Output message to Serial Monitor
digitalWrite(buzzerPin, HIGH); // Turn on the buzzer
}
// Motion stopped: Transition from HIGH to LOW
else if (pinStatePrevious == HIGH && pinStateCurrent == LOW) {
Serial.println("Motion stopped!"); // Output message to Serial Monitor
digitalWrite(buzzerPin, LOW); // Turn off the buzzer
}
}
- Updates `pinStatePrevious` with the last sensor state and reads the new sensor state into `pinStateCurrent`.
- Motion Detection:
- Checks if the sensor state changed from LOW to HIGH, indicating detected motion.
- Prints “Motion detected!” to the Serial Monitor and activates the buzzer.
- Motion Stopping:**
- Checks if the sensor state changed from HIGH to LOW, indicating that motion has stopped.
- Prints “Motion stopped!” to the Serial Monitor and deactivates the buzzer.