#include <ESP32Servo.h>
// Define pins
#define PIR_PIN 15 // PIR sensor connected to digital pin 2
#define SERVO_PIN 2 // Servo motor connected to digital pin 9
Servo servo; // Create a Servo object
void setup() {
pinMode(PIR_PIN, INPUT); // Set PIR sensor pin as input
servo.attach(SERVO_PIN); // Attach the servo to the pin
servo.write(0); // Initialize the servo position to 0 degrees
Serial.begin(9600); // Start the serial communication for debugging
}
void loop() {
int pirState = digitalRead(PIR_PIN); // Read the PIR sensor state
if (pirState == HIGH) {
servo.write(90); // Move servo to 90 degrees
Serial.println("Motion detected!");
delay(2000); // Wait for 2 seconds
} else {
servo.write(0); // Move servo back to 0 degrees
Serial.println("No motion.");
delay(100); // Short delay
}
}