Lesson 6 of 13
Sense light
Objective: Read a light sensor through a voltage divider, calibrate a threshold for your room, and add hysteresis so a lamp, or a rover's headlights, switches cleanly.
Changing the light is a simulated input: the sketch does not change. For scale: about 3 lux as twilight ends, 50 lux living-room lights, 320–500 lux office lighting, 1000 lux an overcast day.
While the sketch runs, a chart draws the light, the reading and the lamp over the last 8 seconds.
Stage 1 of 7 · What am I building?
Explore
A robot that knows how bright it is can switch on its headlights, find a shadow, or notice a door opening. The board measures light as a voltage on A0. Select each part on the circuit, or below.
Pick a part. 0/7 inspected.
Before you start (optional refreshers)
Conditional logic
if (condition) { … } else { … } chooses what to do from a reading. The same test runs on every pass of loop().
Voltage dividers
Two resistances in series split the supply voltage. A potentiometer is a divider whose middle leg (the wiper) slides along it: 0 V at the GND end, 5 V at the 5V end, anything between. Change one resistance (a knob, an LDR) and the middle voltage changes.
Analog-to-digital conversion
analogRead() measures 0–5 V on A0–A5 and reports 0–1023: about 4.9 mV per step.
New ideas in this lesson
Resistive sensors in a divider
Some sensors change their resistance: an LDR's falls as light rises. Paired with a fixed resistor, it makes a divider, so the change becomes a voltage analogRead() can measure. Which way the reading moves depends on which side of A0 the sensor sits.
Thresholds and comparisons
Compare a reading to a chosen number to turn a continuous value into a decision. Whether the action happens below or above it depends on how the sensor is wired.
Hysteresis
Use two switching points with a gap between them: turn on below one, off above the other, and leave things as they are in between. Small wobbles in a reading then cannot flip the output back and forth.
Sensor readings and calibration
Real readings vary with the room, the part and noise. Measure first (often with Serial) in the conditions that matter, then choose a threshold between them. Compare with a trusted reference such as a ruler: a difference that stays the same is an offset to subtract; one that grows with distance is a scale error.
Build it for real · Arduino Uno R3
You need
- Arduino Uno R3 (or compatible) and its USB cable
- Breadboard and jumper wires
- Light-dependent resistor (LDR)
- 10 kΩ resistor
- LED
- 220 Ω resistor
Wiring
- LDR from 5V to A0.
- 10 kΩ resistor from A0 to GND.
- LED: D13 → 220 Ω → LED long leg; short leg → GND.
Before you power up: Keep the 10 kΩ resistor: without it A0 is pulled up through the LDR and reads near 1023 whatever the light.
Tip: Print analogRead(A0) with Serial in your room's light and in shadow, then pick a threshold between the two.
The lab models ideal parts. Real circuits add tolerances, noise and loose wires, so check your wiring against this list before you plug in.
// Turn a lamp on when it gets dark
// LDR from 5V to A0, 10k resistor from A0 to GND
// Lamp LED: D13 through a 220 ohm resistor to GND
// Brighter light gives a HIGHER reading
const int sensorPin = A0; // the middle of the divider
const int lampPin = 13;
const int threshold = 400; // tune for your room
void setup() {
pinMode(lampPin, OUTPUT);
}
void loop() {
int light = analogRead(sensorPin); // 0 to 1023
if (light < threshold) {
digitalWrite(lampPin, HIGH); // dark: lamp on
} else {
digitalWrite(lampPin, LOW); // bright: lamp off
}
}Press ▶ Run, then change the light.