#include <ESP32Servo.h>
#define SERVO_PIN 14
// Connect the PIR sensor on PIN 25
#define PIR_PIN 25
Servo doorLock;
// Declare the flag variable motionDetected
bool motionDetected = false;
void setup() {
Serial.begin(115200);
Serial.println("Hello, ESP32!");
// Set PIR pinMode to INPUT
pinMode(PIR_PIN, INPUT);
doorLock.attach(SERVO_PIN);
doorLock.write(0);
}
void loop() {
// Check for motion
motionDetected = digitalRead(PIR_PIN);
// Check whether motion is detected or not and set the doorLock value to 90 else set to 0
if(motionDetected){
doorLock.write(90);
}else{
doorLock.write(0);
}
// Add delay of 200 milliseconds to avoid continuous detection
delay(200);
}