#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <ESP32Servo.h>
const int lightSensorPin = 2; // Analog pin connected to the light sensor
const int servoMotorPin = 5; // PWM pin connected to the servo motor
const int ledPin = 33; // Digital pin connected to the LED
const int lcdAddress = 0x27; // I2C address of the LCD display
LiquidCrystal_I2C lcd(lcdAddress, 16, 2); // Define the LCD object
Servo myservo; // Create a servo object
int lightValue; // Variable to store the light sensor reading
int servoPosition = 0; // Variable to store the servo motor position
const int brightThreshold = 1000; // Threshold value for brightness (adjustable)
const int servoMin = 0; // Minimum servo position (0 degrees)
const int servoMax = 90; // Maximum servo position (180 degrees)
void setup() {
// Initialize serial communication
Serial.begin(9600);
// Set pin modes
pinMode(lightSensorPin, INPUT);
pinMode(servoMotorPin, OUTPUT);
pinMode(ledPin, OUTPUT);
// Initialize LCD display
lcd.init();
lcd.backlight();
// Attach servo object to the pin
myservo.attach(servoMotorPin);
// Print initial message
lcd.print("Initializing...");
delay(1000);
lcd.clear();
}
void loop() {
// Read the light sensor value
lightValue = analogRead(lightSensorPin);
// Determine brightness status
bool isBright = lightValue < brightThreshold;
// Update LCD display
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Light value: ");
lcd.print(lightValue);
lcd.setCursor(0, 1);
lcd.print(isBright ? "Bright" : "Dark");
// Control servo motor based on brightness
if (isBright) {
servoPosition = servoMax; // Turn clockwise
digitalWrite(ledPin, HIGH); // Turn on LED
} else {
servoPosition = servoMin; // Turn anti-clockwise
digitalWrite(ledPin, LOW); // Turn off LED
}
// Move the servo motor to the desired position
myservo.write(servoPosition);
// Print status to serial monitor
Serial.print("Light Value: ");
Serial.print(lightValue);
Serial.print(", Servo Position: ");
Serial.println(servoPosition);
delay(100); // Delay between readings
}