const int pirPin = 14; // PIR sensor output pin (digital)
const int ledPin = 19; // LED pin (built-in LED on most Arduino boards)
const int buzzer = 18;
void setup() {
pinMode(pirPin, INPUT); // Set PIR pin as input
pinMode(ledPin, OUTPUT); // Set LED pin as output
pinMode(buzzer, OUTPUT);
digitalWrite(ledPin, LOW); // Ensure LED is off initially
Serial.begin(9600); // Start serial communication for debugging
Serial.println("Hello, ESP32!");
}
void loop() {
int pirState = digitalRead(pirPin); // Read PIR sensor state
if (pirState == HIGH) { // Motion detected
digitalWrite(ledPin, HIGH); // Turn on LED
digitalWrite(buzzer, HIGH);
Serial.println("Motion detected!");
delay(2000); // Keep LED on for 2 seconds (adjust as needed)
} else {
digitalWrite(ledPin, LOW); // Turn off LED
digitalWrite(buzzer, LOW);
Serial.println("No motion.");
}
delay(1000); // Small delay to avoid flooding the Serial Monitor
}