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 →
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
- Open this experiment
- 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.