Auto Night Light using LDR (ESP32 + MicroPython) is a runnable microcontroller simulation example on SensBench — open the wired circuit, inspect the code, and compile real firmware in your browser, no install needed. Last updated Aug 14, 2026.

FREE SIMULATOR EXAMPLE · BEGINNER

Auto Night Light using LDR (ESP32 + MicroPython)

This example runs an automatic night light on the ESP32 — an LDR senses ambient light through the ADC, and an LED switches on when the room darkens. MicroPython, one threshold, five lines of logic.

Board ESP32 DevKit (Xtensa LX6, 240 MHz)
Difficulty beginner
Category sensors
Libraries None — just the Arduino core
Runs in Sensbench browser simulator — free, no account
ESP32 auto night light reading an LDR and switching an LED in the Sensbench simulator

The exact circuit you get when you open this example — already wired, ready to run.

The sketch

# main.py
from machine import Pin, ADC
from time import sleep

# Initialize LDR on ADC pin 34
ldr = ADC(Pin(34))
ldr.atten(ADC.ATTN_11DB)  # full range 0-3.3V

# Initialize LED on GPIO2
led = Pin(2, Pin.OUT)

# Threshold for turning LED on/off
threshold = 500

while True:
    light = ldr.read()       # Read LDR value
    print(light)
    
    if light < threshold:    # It's dark
        led.on()             # Turn LED ON
    else:                    # It's bright
        led.off()            # Turn LED OFF
    
    sleep(1)

How it works

The script reads the LDR through the ESP32’s ADC and compares the value to a threshold. Below it (dark), the LED switches on; above it (bright), off. The loop repeats every few hundred milliseconds, making the light follow the room with visible lag-free response.

The ADC reading is the whole sensor interface: the LDR is one half of a voltage divider, and the script’s threshold number is the divider’s tipping point. The comment-worthy detail is the hysteresis-free comparison — the light flips cleanly at one value, which real night lights improve with a small dead zone.

Before you build it for real

The LDR’s response is slow and nonlinear — it takes tens of milliseconds to settle after a light change, so the threshold must be tuned in the actual mounting location. The example’s comparison has no hysteresis: near the threshold, a flickering lamp or passing shadows make the LED chatter, which real night lights prevent with a dead zone. And the LDR’s resistance varies ±50% between units of the same model — the threshold number that works on one build is only a starting point on the next.

Get the parts

Related examples

FAQ

How does the LDR detect darkness?

The LDR’s resistance rises in the dark, shifting the voltage divider the ADC reads. The script compares the reading against a threshold and switches the LED — brighter than the threshold means daytime, dimmer means night. The threshold is the one number worth tuning.

Why MicroPython instead of C++ for this?

A night light is a five-line program — reading an ADC and writing a pin. MicroPython expresses that in a screenful, with the interactive REPL as a bonus for tuning the threshold live. The trade-off (speed, memory) is irrelevant at this scale.