DEV Community

Cover image for ESP32 MicroPython LDR Module
Arduino ESP32 Raspberry Pi Tutorials
Arduino ESP32 Raspberry Pi Tutorials

Posted on Originally published at newbiely.com

ESP32 MicroPython LDR Module

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

Wiring

ESP32 MicroPython LDR Module wiring diagram

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
Enter fullscreen mode Exit fullscreen mode

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)