Read the world · 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.
Compare a sensor reading with a threshold.
Understand it
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.
Your 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
Sketch
This sketch illustrates the hardware logic. The workbench runs its corresponding teaching model, not a C++ compiler.
void setup() {
pinMode(13, OUTPUT);
Serial.begin(9600);
}
void loop() {
int light = analogRead(A0);
digitalWrite(13, light < 500);
Serial.println(light);
}Quick check
Is the LED on when the reading equals the threshold?
Show explanation
No. The example tests whether the reading is strictly below the threshold. Equality takes the other branch.