// Pin definitions
const int pirPin = 2;
const int ledPin = 4;
// Variables for timing
unsigned long startTime;
unsigned long endTime;
unsigned long responseTime;
void setup() {
Serial.begin(115200);
pinMode(pirPin, INPUT);
pinMode(ledPin, OUTPUT);
Serial.println("Motion Detection Response Time Test - ESP32");
}
void loop() {
// Start timing when motion is detected
if (digitalRead(pirPin) == HIGH) {
startTime = micros();
digitalWrite(ledPin, HIGH);
// Calculate response time
endTime = micros();
responseTime = endTime - startTime;
// Print results
Serial.print("Response Time (microseconds): ");
Serial.println(responseTime);
delay(1000); // Delay to avoid multiple readings
} else {
digitalWrite(ledPin, LOW);
}
}