How to Wire an LDR to ESP32: A Step-by-Step MicroPython Guide

Think wiring an LDR to an ESP32 is easy? One wrong resistor value and your sensor reads zero at high noon. Learn the right way with this wiring diagram.

Golden hour, perfect readings—Calista fine-tunes her ESP32 LDR build to track light changes with open-source precision.

Wiring an LDR to an ESP32 sounds simple—until your sensor reads zero at noon.

It was supposed to be a 10-minute build.

Just wire an LDR to my ESP32, write a few lines of MicroPython, and boom—ambient light readings for my indoor greenhouse. But instead of smooth lux values, I got zeros. At high noon. With the LDR pointed directly at the sun.

I rechecked everything: jumper wires, GPIO pin, pull-down resistor—wait. The resistor. That tiny, overlooked piece in the voltage divider circuit. Swapping it out fixed everything, and suddenly my ESP32 was reading light with uncanny accuracy.

That’s when it hit me—most LDR tutorials either skip the wiring diagram entirely or gloss over the one thing that makes or breaks your build. As someone who champions FOSS and low-cost hardware, I believe in understanding every link in the chain. This article breaks down not just how to wire an LDR to the ESP32, but why each connection matters.

If you’re tired of tutorials that leave you guessing, this one’s for you. Read on.

What is an LDR?

An LDR, also known as a photoresistor, is a passive component whose resistance decreases as light intensity increases. This property makes it useful for applications that require automatic brightness control, daylight sensing, and light-activated switches. Since the ESP32 lacks built-in analog pins like the Arduino, we’ll use its ADC (Analog-to-Digital Converter) to read the LDR’s output.

Close-up of a Light Dependent Resistor (LDR), a sensor that changes resistance based on the amount of light it receives.

· · ─ ·𖥸· ─ · ·

Wiring an LDR to ESP32

To connect an LDR to the ESP32, we’ll use a simple voltage divider circuit. This setup allows the ESP32 to measure varying voltages based on light intensity.

Components Needed:

  • ESP32 development board
  • LDR (photoresistor)
  • 10kΩ resistor
  • Breadboard and jumper wires

Circuit Diagram:

  1. Connect one end of the LDR to 3.3V on the ESP32.
  2. Connect the other end of the LDR to one leg of a 10kΩ resistor.
  3. Connect the junction between the LDR and the resistor to GPIO 34 (or any ADC-compatible pin) on the ESP32.
  4. Connect the other end of the resistor to GND.

This voltage divider will output a voltage that varies with light intensity, which the ESP32 can read using its ADC.

· · ─ ·𖥸· ─ · ·

Reading LDR Values in MicroPython

To read the LDR’s output, we’ll write a simple MicroPython script using the ADC module.

MicroPython Code:

from machine import ADC, Pin
import time

# Configure ADC on GPIO 34
ldr = ADC(Pin(34))
ldr.atten(ADC.ATTN_11DB)  # Configure for full range (0-3.3V)

while True:
    light_value = ldr.read()  # Read ADC value (0-4095)
    print("LDR Value:", light_value)
    time.sleep(1)

Explanation of the Code:

  • We initialize the ADC on GPIO 34.
  • The attenuation is set to 11dB, allowing a full range of 0-3.3V.
  • The script continuously reads the LDR value and prints it every second.

· · ─ ·𖥸· ─ · ·

Applications of LDR with ESP32

Once you’ve successfully connected and read LDR values, you can use this setup in various projects:

  • Automatic Night Lights – Turn LEDs on when it gets dark.
  • Smart Blinds – Adjust window blinds based on sunlight.
  • Light Data Logging – Monitor and analyze light levels over time.

· · ─ ·𖥸· ─ · ·

Open Hardware, Accurate Light

Wiring an LDR to an ESP32 isn’t just about matching wires to pins—it’s about understanding how voltage dividers, analog-to-digital conversion, and resistor values all work together. And once you get that right, your ESP32 becomes a powerful, precise light sensor capable of triggering anything from smart blinds to garden lights.

This is what makes open hardware so exciting: it invites us to tinker, to fail forward, and to build smarter with every experiment.

Want more FOSS-powered sensor builds and practical microcontroller how-tos?

Subscribe to the DevDigest newsletter and get real-world hacks, circuit saves, and breadboard wins delivered straight to your inbox.

Let’s build better—one bugfix at a time.

Like what you’re reading?
Help keep DevDigest
free and caffeine-powered
—buy me a coffee on Ko-fi.

Leave a Reply

Your email address will not be published. Required fields are marked *

Comments (

)

  1. Peva

    Hiya, I’m really glad I have found this info. Today bloggers publish just about gossips and net and this is really frustrating. A good blog with interesting content, this is what I need. Thank you for keeping this web-site, I’ll be visiting it. Do you do newsletters? Can not find it.

    1. Sam Galope

      Thanks for the feedback! I appreciate your suggestions and will definitely consider adding more visuals to improve readability and engagement. In the meantime, you might enjoy this article: Making LED Matrix Icons and Graphics with ESP32. Let me know what you think! 😊

  2. Makhija

    Thank you for sharing this article with me. It helped me a lot and I love it.

    1. Sam Galope

      You’re very welcome! I’m really glad to hear that the article was helpful and that you enjoyed it. If you’re interested in more hands-on projects, you might find this one useful too:

      How to Monitor Soil Moisture Levels with an ESP32 and Soil Moisture Sensor using MicroPython

      Thanks for reading, and I appreciate your support! 🚀