Light sensor with an ESP32
An LDR and a fixed resistor make a voltage divider whose output falls as it gets darker. The sketch reads it on GPIO34 and turns on an LED below a threshold: an automatic night light.

Open this lesson in the labFree, in your browser, no account needed.
How it works
V_GPIO34 = 3.3 V × 10 k ÷ (R_LDR + 10 k). Bright light: R_LDR is small, the voltage is high. Dark: R_LDR is large, the voltage falls.
The sketch calls it dark below 1.65 V, which is when R_LDR rises above 10 kΩ.
Try it on the bench
- Select the LDR and lower the light to 5 lux: the LED comes on.
- Change R_10k to 47 kΩ: it now switches at a darker level.
Parts on the bench
- Bench power supply
- ESP32 DevKit
- USB charger / port
- Photoresistor (LDR)
- 2 × Resistor
- LED
In the course
Zen’s Electronics Design Mastery, module “Microcontrollers & Embedded”.