int LDRPin = 13; // ldr connected to analog pin a0
int LEDPin = 2; // led connected to digital pin 13
int threshold = 500; // threshold value for light level(adjust as needed)
void setup(){
pinMode(LDRPin, INPUT); //set ldr pin as input
pinMode(LEDPin, OUTPUT); // set led pin as output
Serial.begin(9600); // initialize serial communication for debugging
}
void loop(){
int LDRValue = analogRead(LDRPin); // read the value from the ldr
Serial.println(LDRValue); // print the ldr value to the serial monitor
if(LDRValue < threshold){
// if the light level is below the threshold, turn on the led
digitalWrite(LEDPin, HIGH);
}else{
// if the light level is above the threshold, turn off the led
digitalWrite(LEDPin, LOW);
}
delay(100); // small delay for stability
}