const int ldrPin = A0; // LDR sensor connected to analog pin A0
const int ledPin = 13; // LED connected to digital pin 13
void setup() {
pinMode(ledPin, OUTPUT); // Set the LED pin as an output
Serial.begin(9600); // Initialize serial communication for debugging
}
void loop() {
int ldrValue = analogRead(ldrPin); // Read the LDR sensor value
int threshold = 500; // Adjust this threshold based on ambient light conditions
Serial.print("LDR Value: ");
Serial.println(ldrValue); // Print LDR value for debugging
// Check if the LDR value is below the threshold
if (ldrValue < threshold) {
digitalWrite(ledPin, HIGH); // Turn on the LED
Serial.println("Dark"); // Print "Dark" for debugging
} else {
digitalWrite(ledPin, LOW); // Turn off the LED
Serial.println("Light"); // Print "Light" for debugging
}
delay(500); // Add a delay to avoid rapid LED state changes
}