#include <ESP32Servo.h>
int ledPin = 2; // LED connected to GPIO pin 2
int pirPin = 4; // PIR sensor connected to GPIO pin 4
int servoPin = 14; // Servo motor connected to GPIO pin 14
int buzzerPin = 5; // Buzzer connected to GPIO pin 5
Servo myservo; // create a Servo object
void setup() {
pinMode(ledPin, OUTPUT); // set the LED pin as output
pinMode(pirPin, INPUT); // set the PIR sensor pin as input
pinMode(buzzerPin, OUTPUT); // set the buzzer pin as output
myservo.attach(servoPin); // attach the servo to GPIO pin 14
Serial.begin(9600); // initialize serial communication at 9600 bits per second
}
void loop() {
digitalWrite(ledPin, LOW); // turn the LED off
int pirState = digitalRead(pirPin); // read the state of the PIR sensor
if (pirState == HIGH) { // if motion is detected
Serial.println("Motion detected!"); // print a message to the serial monitor
myservo.write(90); // move the servo to 90 degrees
digitalWrite(buzzerPin, HIGH); // turn on the buzzer
digitalWrite(ledPin, HIGH); // turn on the LED
delay(5000); // wait for 5 seconds
myservo.write(0); // move the servo to 0 degrees
delay(500); // wait for a short period of time
digitalWrite(buzzerPin, LOW); // turn off the buzzer
digitalWrite(ledPin, LOW); // turn off the LED
}
}