Home › Lessons › Embedded (ESP32) › Light sensor with an ESP32

Light sensor with an ESP32

Embedded (ESP32) · Beginner · about 20 minutes · part of Zen’s Electronics Design Mastery

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.

Light sensor with an ESP32: the circuit on the simulator's bench

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

  1. Select the LDR and lower the light to 5 lux: the LED comes on.
  2. Change R_10k to 47 kΩ: it now switches at a darker level.

Parts on the bench

In the course

Zen’s Electronics Design Mastery, module “Microcontrollers & Embedded”.

More on embedded (esp32)