Submit

06 / 16

Light Sensor: Build a Night Light

Compare a light sensor reading with a threshold to switch an LED. Test the boundary and see why sensor direction depends on the circuit.

Run this experiment →
Independently modeled teaching circuit with detailed connectors, pins, chip and component wiring

Why it’s here

Compare a sensor reading with a threshold.

How it works

The threshold is a decision boundary. In this example, a lower reading means less light, so the LED turns on below the threshold. A real voltage divider may produce the opposite direction depending on its wiring.

Try it

  1. Open this experiment
  2. Run with a threshold of 500. Move the sensor reading from 499 to 500 and then 501; record exactly when the LED switches.

Connections

  • A0 — LDR voltage divider
  • D13 — LED through resistor

Example sketch

void setup() {
  pinMode(13, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  int light = analogRead(A0);
  digitalWrite(13, light < 500);
  Serial.println(light);
}

Check your understanding

Is the LED on when the reading equals the threshold?

Show the answer

No. The example tests whether the reading is strictly below the threshold. Equality takes the other branch.

Sources

Arduino Uno R3 ↗ · Language reference ↗

MAKE A THOUGHTFUL CHOICE

Your shortlist, side by side.

Pricing models are indicative. Check each official website for current plans and limits.