const int LDR_PIN = A0; // Analog pin connected to the LDR
const int LED_PIN = 13; // Digital pin connected to the LED
void setup() {
pinMode(LED_PIN, OUTPUT);
Serial.begin(9600); // Initialize serial communication for debugging
}
void loop() {
int lightLevel = analogRead(LDR_PIN); // Read the analog value from the LDR
// Convert the analog value to a percentage (0-100)
int brightness = map(lightLevel, 0, 1023, 0, 100);
// Print the light level (for debugging)
Serial.print("Light level: ");
Serial.print(brightness);
Serial.println("%");
// Adjust the LED brightness based on the light level
analogWrite(LED_PIN, brightness * 2.55); // Convert percentage to PWM value (0-255)
delay(1000); // Delay for stability and to prevent rapid changes
}