// Include the required libraries
#include <Arduino.h>
// Define the pin connections
#define PIR_PIN 13 // Pin connected to the output of the PIR motion sensor
#define BUZZER_PIN 12 // Pin connected to the buzzer
// Variables to store the state of the alarm
bool motionDetected = false;
void setup() {
// Initialize the serial monitor
Serial.begin(115200);
// Set the GPIO pins as input and output
pinMode(PIR_PIN, INPUT);
pinMode(BUZZER_PIN, OUTPUT);
}
void loop() {
// Read the state of the PIR sensor
motionDetected = digitalRead(PIR_PIN);
// If motion is detected
if (motionDetected == HIGH) {
Serial.println("Motion detected!");
activateAlarm(); // Call the function to activate the alarm
delay(5000); // Delay for 5 seconds to avoid continuous triggering
}
}
void activateAlarm() {
// Sound the buzzer
digitalWrite(BUZZER_PIN, HIGH);
delay(1000); // Sound the buzzer for 1 second
digitalWrite(BUZZER_PIN, LOW);
}