// Define constants for LDR pin and LED pin
const int ldrPin = A0; // LDR connected to analog pin A0
const int ledPin = 13; // LED connected to digital pin 13
// Define threshold value for light level
int threshold = 500; // Adjust this value according to ambient light conditions
void setup() {
pinMode(ledPin, OUTPUT); // Set LED pin as output
Serial.begin(9600); // Initialize serial communication for debugging
}
void loop() {
int lightLevel = analogRead(ldrPin); // Read LDR sensor
Serial.print("Light Level: ");
Serial.println(lightLevel); // Print the light level to serial monitor for debugging
// Check if light level is below the threshold
if (lightLevel < threshold) {
digitalWrite(ledPin, HIGH); // Turn on the LED
} else {
digitalWrite(ledPin, LOW); // Turn off the LED
}
delay(100); // Delay for stability, adjust as needed
}