
Introduction
Light-dependent resistors (LDRs) are simple and cost-effective sensors used to measure light intensity in various applications, from automatic lighting systems to smart agriculture. They work by varying their resistance based on the amount of light they receive, making them an essential component in light-sensitive electronics. When paired with an ESP32 microcontroller, an LDR can be used to create responsive projects that adapt to changing ambient light conditions.
With the ESP32’s built-in ADC (Analog-to-Digital Converter), we can easily read the LDR’s output and use it for decision-making in IoT and automation applications. Whether you’re designing an energy-efficient lighting system, a security device, or a data logging application, understanding how to interface an LDR with the ESP32 is a valuable skill. In this guide, we’ll explore how LDRs work, how to wire them to an ESP32, and how to write a MicroPython script to read light levels.
Table of Contents
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.

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:
- Connect one end of the LDR to 3.3V on the ESP32.
- Connect the other end of the LDR to one leg of a 10kΩ resistor.
- Connect the junction between the LDR and the resistor to GPIO 34 (or any ADC-compatible pin) on the ESP32.
- 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.
Conclusion
Using an LDR with an ESP32 provides a simple yet powerful way to measure and respond to light intensity. By integrating an LDR with the ESP32’s ADC, we can create projects that dynamically react to ambient lighting, improving automation and energy efficiency in various applications. Whether you’re working on smart home projects, outdoor lighting systems, or scientific experiments, the ability to read and process light levels is a useful capability.
With a basic voltage divider circuit and MicroPython, you can seamlessly add light-sensing functionality to your IoT devices. By experimenting with different applications and refining your code, you can unlock new possibilities for automation and data collection, making your ESP32 projects even more versatile and intelligent.
Leave a Reply