const int pirPin = 2; // PIR sensor connected to digital pin 2
const int buzzerPin = 9; // Pin for the buzzer
const int ledPin = 8; // Pin for the LED
void setup() {
pinMode(pirPin, INPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(ledPin, OUTPUT);
}
void loop() {
int pirState = digitalRead(pirPin);
if (pirState == HIGH) {
// Motion detected
tone(buzzerPin, 1000); // Sound the buzzer at 1000 Hz
digitalWrite(ledPin, HIGH); // Turn on the LED
delay(500); // Wait for 0.5 seconds
noTone(buzzerPin); // Turn off the buzzer
digitalWrite(ledPin, LOW); // Turn off the LED
delay(500); // Wait for 0.5 seconds (LED blinks at 1Hz)
} else {
// No motion detected
noTone(buzzerPin); // Turn off the buzzer
digitalWrite(ledPin, LOW); // Turn off the LED
}
}