int sensorPin = 2; // PIR motion sensor input pin
int speakerPin = 8; // Speaker or buzzer output pin
int state = LOW; // Current state of the motion sensor
void setup() {
Serial.begin(9600); // Initialize serial communication
pinMode(sensorPin, INPUT);
pinMode(speakerPin, OUTPUT);
}
void loop() {
state = digitalRead(sensorPin);
if (state == HIGH) {
Serial.println("Motion detected!");
tone(speakerPin, 1000, 500);
} else {
Serial.println("No motion detected");
noTone(speakerPin);
}
delay(100);
}