ESP32 - LDR Module
The LDR light sensor module can sense and measure light around it. It has two outputs: a digital output that can be either LOW or HIGH, and an analog output.
In this tutorial, we will learn how to use an ESP32 and an LDR light sensor module together to detect and measure the amount of light. Here's what we'll cover:
- How to connect the LDR light sensor module to an ESP32.
- How to program the ESP32 to detect light by reading the digital signal from the LDR light sensor module.
- How to program the ESP32 to measure the light level by reading the analog signal from the LDR light sensor module.

Afterward, you can change the code to make an LED or a light bulb turn on (using a relay) when it senses light.
If you're interested in a light sensor in its raw form, I suggest exploring the tutorial for the ESP32 - Light Sensor.
Hardware Used In This Tutorial
1 | × | ESP-WROOM-32 Dev Module | |
1 | × | Micro USB Cable | |
1 | × | LDR Light Sensor Module | |
1 | × | Breadboard | |
1 | × | Jumper Wires | |
1 | × | (Optional) ESP32 Screw Terminal Adapter |
Or you can buy the following sensor kit:
1 | × | DIYables Sensor Kit 30 types, 69 units |
Introduction to LDR Light Sensor Module
The LDR light sensor module can be used to find out if there is light or how much light there is in the area around it. It has a digital output pin and an analog output pin for different options.
Pinout
The LDR light sensor module has four pins:
- VCC pin: Connect this pin to the power source (between 3.3V to 5V).
- GND pin: Connect this pin to the ground (0V).
- DO pin: This is a digital output pin. It gives a HIGH signal when it's dark and LOW when it's light. You can adjust the threshold between dark and light using a built-in potentiometer.
- AO pin: This is an analog output pin. The value decreases as the light gets brighter and increases as the light gets darker.

Additionally, the LDR light sensor module is equipped with two LED indicators:
- The PWR-LED indicator shows the power status.
- The DO-LED indicator reflects the state of light on the DO pin: it illuminates when there is light and remains off when it is dark.
How It Works
Regarding the DO pin:
- The LDR light sensor module has a potentiometer that allows you to adjust the sensitivity or threshold for detecting light.
- When the light intensity in the surrounding environment is above the set threshold (considered as light), the output of the sensor on the DO pin becomes LOW, and the DO-LED turns on.
- When the light intensity in the surrounding environment is below the set threshold (considered as dark), the output of the sensor on the DO pin becomes HIGH, and the DO-LED turns off.
Regarding the AO pin:
- The value read from the AO pin is inversely proportional to the light intensity in the surrounding environment. In other words, as the light intensity increases (brighter), the value on the AO pin decreases.
- Similarly, as the light intensity decreases (darker), the value on the AO pin increases.
It's important to note that adjusting the potentiometer does not affect the value on the AO pin.
Wiring Diagram
Since the light sensor module has two outputs, you can choose to use one or both of them, depending on what you need.
- The wiring diagram between ESP32 and the LDR light sensor module when using DO only.

This image is created using Fritzing. Click to enlarge image
- The wiring diagram between ESP32 and the LDR light sensor module when using AO only.

This image is created using Fritzing. Click to enlarge image
- The wiring diagram between ESP32 and the LDR light sensor module when using both AO an DO.

This image is created using Fritzing. Click to enlarge image
ESP32 Code - Read value from DO pin
Quick Instructions
- If this is the first time you use ESP32, see how to setup environment for ESP32 on Arduino IDE.
- Do the wiring as above image.
- Connect the ESP32 board to your PC via a micro USB cable
- Open Arduino IDE on your PC.
- Select the right ESP32 board (e.g. ESP32 Dev Module) and COM port.
- Copy the above code and open with Arduino IDE
- Click Upload button on Arduino IDE to upload code to ESP32
- Cover and uncover the LDR light sensor module by your hand or something
- See the result on Serial Monitor.
If you observe that the LED status remains constantly on or off, regardless of the presence of light, you have the option to adjust the potentiometer. This adjustment allows you to finely tune the light sensitivity of the sensor.
Furthermore, the code can be modified according to your requirements. For instance, you can program the LED to activate or the light to turn on when light is detected. Additionally, you have the flexibility to make a servo motor rotate. Detailed instructions and tutorials on these customization options can be found at the end of this guide.
ESP32 Code - Read value from AO pin
Quick Instructions
- Copy the above code and open with Arduino IDE
- Click Upload button on Arduino IDE to upload code to ESP32
- Cover and uncover the LDR light sensor module by your hand or something
- See the result on Serial Monitor.
Video Tutorial
Making video is a time-consuming work. If the video tutorial is necessary for your learning, please let us know by subscribing to our YouTube channel , If the demand for video is high, we will make the video tutorial.