An LDR light sensor module lets your ESP32 react to ambient light, going dark-triggered or measuring brightness as a number. It gives you both a simple digital HIGH/LOW signal and a raw analog reading, so you can pick whichever fits your project, a night light trigger or a light level logger.
The full tutorial, with every detail and the line-by-line code explanation, is on our site: ESP32 MicroPython LDR Module
What you need
- 38-pin ESP32 ESP-WROOM-32 Dev Module - Narrow
- 38-pin ESP32 ESP-WROOM-32 Dev Module - Wide
- 30-pin ESP32 ESP-WROOM-32 Dev Module - Wide
- ESP32 Uno-form board
- ESP32 S3 Uno-form board
- USB Cable Type-A to Type-C (for USB-A PC)
- USB Cable Type-C to Type-C (for USB-C PC)
- LDR Light Sensor Module
- Jumper Wires
- Breadboard
- Screw Terminal Expansion Board for ESP32
- Breakout Expansion Board for ESP32
- Power Splitter for ESP32
- DIYables ESP32 Starter Kit (ESP32 included)
- DIYables ESP32 S3 Starter Kit (ESP32 S3 included)
- DIYables Sensor Kit (18 sensors/displays)
Wiring
The module has four pins. Connect VCC to the ESP32's 3.3V to 5V supply and GND to ground. The DO pin is the digital output, and the AO pin is the analog output. You can wire just one of them or both at once, depending on whether you want a simple light/dark signal or a continuous reading. In the example code, DO connects to GPIO13.
How it works
The module has a potentiometer that sets a light threshold. On the DO pin, the output goes LOW when the surrounding light is brighter than that threshold, and HIGH when it's dimmer, so you can read it like a simple switch. A DO-LED on the board mirrors this: it's lit when there's light and off in the dark. The AO pin instead gives you a varying reading, lower when there's more light and higher as it gets darker, which is useful if you want to measure light level rather than just detect its presence.
Code
The MicroPython sketch sets up GPIO13 as a digital input connected to the DO pin. In a loop, it calls value() on that pin: a reading of 1 means no light is present, and 0 means light is present, so the code prints the opposite message for each state. A short sleep keeps the output from flooding the console. A separate version of the code reads from the AO pin instead, giving you a numeric light level rather than a simple present/not-present state.
esp32-micropython-ldr-module.py
"""
This ESP32 MicroPython code was developed by newbiely.com
This ESP32 MicroPython code is made available for public use without any restriction
For comprehensive instructions and wiring diagrams, please visit:
https://newbiely.com/tutorials/esp32-micropython/esp32-micropython-ldr-module
"""
from machine import Pin
import utime # For timing functions
DO_PIN = Pin(13, Pin.IN) # The ESP32 pin GPIO13 connected to the DO pin of the LDR module
while True:
light_state = DO_PIN.value() # Read the digital value from the pin
if light_state == 1:
print("The light is NOT present")
else:
print("The light is present")
utime.sleep(0.5) # Add a small delay to avoid spamming the output
Full tutorial
The complete tutorial - wiring diagram, code explanation and troubleshooting - is here: https://newbiely.com/tutorials/esp32-micropython/esp32-micropython-ldr-module

Top comments (0)