Programming Examples
Write a program for detection of the light using photo resistor with output as light detected and LED state.

Write a program for detection of the light using photo resistor with output as light detected and LED state.
Solution#define LDR_PIN A0 // LDR connected to analog pin A0
#define LED_PIN 13 // LED connected to pin 13
int threshold = 500; // Light level threshold (adjust as needed)
void setup() {
pinMode(LED_PIN, OUTPUT);
Serial.begin(9600);
}
void loop() {
int lightValue = analogRead(LDR_PIN); // Read LDR value
Serial.print("Light Level: ");
Serial.print(lightValue);
if (lightValue > threshold) {
Serial.println(" | Light Detected | LED ON");
digitalWrite(LED_PIN, HIGH);
} else {
Serial.println(" | No Light | LED OFF");
digitalWrite(LED_PIN, LOW);
}
delay(1000);
}
▶ RUN Output/ Explanation: 