int sensorPin = 35; // Pin connected to the PIR sensor signal
int ledPin = 16; // Pin connected to the LED
void setup() {
// Initialize the serial communication
Serial.begin(115200);
Serial.println("Starting ESP32 PIR and LED setup");
// Configure the sensorPin as an input
pinMode(sensorPin, INPUT);
// Configure the ledPin as an output
pinMode(ledPin, OUTPUT);
// Ensure the LED is off at startup
digitalWrite(ledPin, LOW);
}
void loop() {
// Read the value from the PIR sensor
int sensorValue = digitalRead(sensorPin);
// Print the sensor value to the Serial Monitor
Serial.print("PIR Sensor State: ");
Serial.println(sensorValue);
// Check if motion is detected
if (sensorValue == HIGH) {
// Turn on the LED when motion is detected
digitalWrite(ledPin, HIGH);
Serial.println("Motion detected! LED ON");
} else {
// Turn off the LED when no motion is detected
digitalWrite(ledPin, LOW);
Serial.println("No motion detected. LED OFF");
}
// Small delay to avoid rapid triggering
delay(1000);
}