const int pirPin = 2; // PIR sensor connected to D2
const int relayPin = 3; // Relay connected to D3
const int buzzerPin = A1; // Buzzer connected to D4 (as per your connections)
const int buzzerFrequency = 1000; // 1 kHz tone for the buzzer
void setup() {
pinMode(pirPin, INPUT);
pinMode(relayPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
// Start serial communication
Serial.begin(9600);
}
void loop() {
int pirState = digitalRead(pirPin);
if (pirState == HIGH) {
digitalWrite(relayPin, HIGH); // Turn on relay (activate connected load)
tone(buzzerPin, buzzerFrequency); // Start buzzing with a tone
Serial.println("Motion detected! Relay and Buzzer ON.");
} else { // If no motion detected
digitalWrite(relayPin, LOW); // Turn off relay
noTone(buzzerPin); // Turn off the buzzer
Serial.println("No motion detected.");
}
delay(500); // Wait for a moment before checking again
}