// Pin Definitions
#define MOTION_SENSOR_PIN 2 // Pin connected to motion sensor
#define BUZZER_PIN 3 // Pin connected to buzzer
// Variables
int motionDetected = 0; // Variable to store motion detection status
void setup() {
// Initialize serial communication at 9600 baud
Serial.begin(9600);
// Set motion sensor pin as input
pinMode(MOTION_SENSOR_PIN, INPUT);
// Set buzzer pin as output
pinMode(BUZZER_PIN, OUTPUT);
}
void loop() {
// Read the motion sensor input
motionDetected = digitalRead(MOTION_SENSOR_PIN);
// If motion is detected
if (motionDetected == HIGH) {
Serial.println("Motion detected!");
// Play a tone on the buzzer for 500 milliseconds
tone(BUZZER_PIN, 1000); // Frequency of 1000Hz
delay(500); // Play for 500 milliseconds
// Stop the tone
noTone(BUZZER_PIN);
}
// Delay for stability
delay(100);
}