Programming Examples

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


O level July 2025 practical Exam question

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);
}
Output


Online Exam Quiz for One day Exam Online Typing Test CCC Online Test 2026 Best Computer Training Institute in Prayagraj (Allahabad) Best Java Training Institute in Prayagraj (Allahabad) Best Python Training Institute in Prayagraj (Allahabad) O Level Online Test in Hindi Best Website and Software Company in Allahabad