// Define the pins
const int ledPin = 9; // LED connected to digital pin 9
const int ldrPin = A0; // LDR connected to analog pin A0
void setup() {
pinMode(ledPin, OUTPUT); // LED pin as output
Serial.begin(9600); // Initialize serial communication
}
void loop() {
int ldrValue = analogRead(ldrPin); // Read the LDR value (0-1023)
// Map the LDR value (0-1023) to the LED brightness (0-255)
int brightness = map(ldrValue, 0, 1023, 0, 255);
analogWrite(ledPin, brightness); // Set the LED brightness
Serial.print("LDR Value: ");
Serial.print(ldrValue);
Serial.print(" Brightness: ");
Serial.println(brightness);
delay(100); // Delay for stability
}