int ledPin = 8; // LED is connected to pin 9
int ldrPin = A0; // LDR is connected to analog pin A0
int ldrValue = 0; // Variable to store LDR value
void setup() {
pinMode(ledPin, OUTPUT); // Set LED pin as output
Serial.begin(9600); // Begin serial communication
}
void loop() {
ldrValue = analogRead(ldrPin); // Read light level
Serial.println(ldrValue); // Display the value for reference
if (ldrValue < 500) { // If it’s dark
digitalWrite(ledPin, HIGH); // Turn on the LED
} else {
digitalWrite(ledPin, LOW); // Turn off the LED
}
delay(500); // Pause for half a second
}