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);
}
▶ RUN

Output/ Explanation:


Latest Current Affairs 2026 Online Exam Quiz for One day Exam Online Typing Test CCC Online Test Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Online MBA 2 years Online MCA Online BCA Best Website and Software Company in Allahabad