#include <ESP32Servo.h>
#define SERVO_PIN 15 // ESP32 pin GPIO15 connected to servo motor
#define LDR_PIN 0 // LDR pin connected to analog input A0
#define LED_PIN 13 // LED pin connected to GPIO13
Servo myServo;
void setup() {
myServo.attach(SERVO_PIN); // attaches the servo on ESP32 pin
pinMode(LDR_PIN, INPUT); // set LDR pin as input
pinMode(LED_PIN, OUTPUT); // set LED pin as output
}
void loop() {
int ldrValue = analogRead(LDR_PIN);
// Check if it's dark (adjust the threshold as needed)
if (ldrValue < 500) {
rotateServo(90); // rotate servo to 90 degrees
digitalWrite(LED_PIN, LOW); // turn on LED
} else {
rotateServo(0); // rotate servo to 0 degrees
digitalWrite(LED_PIN, HIGH); // turn off LED
}
}
void rotateServo(int angle) {
myServo.write(angle);
delay(1000); // Give the servo some time to reach the desired position
}