ESP32 S3 - LDR Module

The LDR light sensor module is a simple yet powerful component for detecting and measuring light levels with your ESP32 S3. This beginner-friendly tutorial walks you through connecting and programming the LDR module to sense ambient light in real-world projects.

In this tutorial, we will learn how to use an ESP32 S3 and an LDR light sensor module together to detect and measure the amount of light. What you'll build:

  1. Understand what an LDR light sensor module is and how it works
  2. Wire the LDR light sensor module to your ESP32 S3
  3. Program the ESP32 S3 to detect light using the digital output (DO) pin
  4. Program the ESP32 S3 to measure light levels using the analog output (AO) pin
ESP32 S3 - LDR 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 S3 - Light Sensor.

Hardware Preparation

1×ESP32 S3 WROOM N16R8
1×USB Cable Type-A to Type-C (for USB-A PC)
1×USB Cable Type-C to Type-C (for USB-C PC)
1×LDR Light Sensor Module
1×Breadboard
1×Jumper Wires

Or you can buy the following kits:

1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables .

Overview of LDR Light Sensor Module

The LDR light sensor module is a digital and analog sensor that detects and measures ambient light intensity in your environment. It combines a light-dependent resistor with onboard signal conditioning circuitry, making it straightforward to use with the ESP32 S3 without any additional components. The built-in potentiometer lets you fine-tune the detection threshold to suit your specific lighting conditions.

Key Specifications

The module operates on a supply voltage between 3.3V and 5V, which makes it directly compatible with the ESP32 S3's 3.3V GPIO logic. It provides two output types — a digital output (DO) and an analog output (AO) — so you can choose threshold-based detection or continuous light level measurement depending on your project. A power LED and a status LED are included on the board for at-a-glance monitoring, and the compact form factor fits easily on a standard breadboard.

Pinout

The LDR light sensor module exposes four pins for connecting to your ESP32 S3:

  1. VCC: Power supply pin — connect to 3.3V on the ESP32 S3
  2. GND: Ground pin — connect to GND on the ESP32 S3
  3. DO: Digital output pin — outputs HIGH when dark, LOW when bright (threshold adjustable via the onboard potentiometer)
  4. AO: Analog output pin — outputs a variable voltage proportional to light intensity (lower value = brighter light)
LDR Light Sensor Module Pinout
image source: diyables.io

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

Connect the LDR light sensor module to your ESP32 S3 by matching each module pin to the corresponding ESP32 S3 pin as shown in the diagram below. You can use the digital output (DO), the analog output (AO), or both simultaneously depending on your project requirements.

Safety Notes

Always connect the VCC pin to the 3.3V rail on the ESP32 S3 rather than the 5V rail to stay within the board's GPIO voltage tolerance. Double-check that GND is shared between the module and the board before powering on, as a floating ground will produce erratic readings.

The wiring diagram between ESP32 S3 LDR Light Sensor Module

This image is created using Fritzing. Click to enlarge image

LDR Module Pin ESP32 S3 Pin
VCC 3.3V
GND GND
DO GPIO7
AO GPIO8 (GPIO8 / ADC1_CH7)

ESP32 S3 Code - Read value from DO pin

The following code demonstrates how to read the digital output from the LDR light sensor module using the ESP32 S3. It configures a GPIO pin as a digital input, reads the HIGH or LOW signal produced by the sensor, and prints the current light status to the Serial Monitor. The detection threshold is set by turning the onboard potentiometer on the module itself.

/* * This ESP32 S3 code was developed by newbiely.com * * This ESP32 S3 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-s3/esp32-s3-ldr-module */ #define DO_PIN 7 // The ESP32 S3 pin connected to DO pin of the ldr module void setup() { // Initialize the Serial to communicate with the Serial Monitor. Serial.begin(115200); // initialize the ESP32's pin as an input pinMode(DO_PIN, INPUT); } void loop() { int light_state = digitalRead(DO_PIN); if (light_state == HIGH) Serial.println("It is dark"); else Serial.println("It is light"); }

Detailed Instructions

  1. New to ESP32 S3? Complete our Getting Started with ESP32 S3 guide first.
  2. Wire the components: Connect the LDR light sensor module to the ESP32 S3 according to the wiring diagram shown above.
  3. Connect ESP32 S3: Plug the ESP32 S3 into your computer using a USB Type-C cable.
  4. Select board: Open Arduino IDE and select the ESP32 S3 board and its corresponding COM port.
  5. Upload code: Copy the above code and open it in Arduino IDE, then click the Upload button to upload the code to the ESP32 S3.
  6. Test the sensor: Cover and uncover the LDR light sensor module with your hand or an object to test light detection.
  7. View results: Open the Serial Monitor to see the light detection status in real-time.
  8. Adjust sensitivity: If needed, turn the potentiometer on the module to adjust the light detection threshold.
  9. Pro Tip: Mark the potentiometer position once you find the perfect sensitivity setting for your lighting conditions.
Newbiely | Arduino IDE 2.3.8
──
File
Edit
Sketch
Tools
Help
ESP32S3 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32S3 Dev Module' on 'COM15')
New Line
9600 baud
[14:02:11.324] It is light [14:02:11.826] It is light [14:02:12.331] It is dark [14:02:12.836] It is dark [14:02:13.341] It is dark [14:02:13.846] It is light [14:02:14.351] It is light [14:02:14.856] It is light
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2

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 S3 Code - Read value from AO pin

The following code shows how to read analog values from the LDR light sensor module for continuous, precise light level measurements. The ESP32 S3's 12-bit ADC produces readings from 0 to 4095, where lower values correspond to brighter light and higher values correspond to darker conditions. This approach is useful when you need to track gradual changes in illumination rather than a simple light/dark threshold.

/* * This ESP32 S3 code was developed by newbiely.com * * This ESP32 S3 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-s3/esp32-s3-ldr-module */ #define AO_PIN 8 // The ESP32 S3 pin connected to AO pin of the ldr module void setup() { // Initialize the Serial to communicate with the Serial Monitor. Serial.begin(115200); // set the ADC attenuation to 11 dB (up to ~3.3V input) analogSetAttenuation(ADC_11db); } void loop() { int light_value = analogRead(AO_PIN); Serial.print("The AO value: "); Serial.println(light_value); }

Detailed Instructions

  1. New to ESP32 S3? Complete our Getting Started with ESP32 S3 guide first.
  2. Upload code: Copy the above code and open it in Arduino IDE, then click the Upload button.
  3. Test the sensor: Cover and uncover the LDR light sensor module with your hand or move it to different lighting conditions.
  4. Monitor values: Open the Serial Monitor to see the analog light level readings.
  5. Observe changes: Notice how values increase when you block light and decrease when you expose the sensor to more light.
  6. Pro Tip: Record the minimum and maximum values in your environment to map them to meaningful ranges like percentages or lux levels.
Newbiely | Arduino IDE 2.3.8
──
File
Edit
Sketch
Tools
Help
ESP32S3 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32S3 Dev Module' on 'COM15')
New Line
9600 baud
[14:05:03.112] The AO value: 145 [14:05:03.614] The AO value: 146 [14:05:04.117] The AO value: 146 [14:05:04.619] The AO value: 572 [14:05:05.122] The AO value: 1678 [14:05:05.625] The AO value: 1945 [14:05:06.128] The AO value: 2956 [14:05:06.631] The AO value: 3001 [14:05:07.134] The AO value: 3098 [14:05:07.637] The AO value: 4005 [14:05:08.140] The AO value: 4005 [14:05:08.643] The AO value: 1645 [14:05:09.146] The AO value: 1546 [14:05:09.649] The AO value: 346 [14:05:10.152] The AO value: 172
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2

※ NOTE THAT:

This tutorial uses the analogRead() function to read values from an ADC (Analog-to-Digital Converter) connected to a sensor or component. The ESP32 S3's ADC is suitable for projects that do not require high accuracy. However, for projects needing precise measurements, keep the following in mind:

  • The ESP32 S3's ADC is not perfectly accurate and might require calibration for correct results. Each ESP32 S3 board can vary slightly, so calibration is necessary for each individual board.
  • Calibration can be challenging, especially for beginners, and might not always yield the exact results you want.

For projects requiring high precision, consider using an external ADC (e.g ADS1115) with the ESP32 S3 or using another Arduino, such as the Arduino Uno R4 WiFi, which has a more reliable ADC. If you still want to calibrate the ESP32 S3's ADC, refer to the ESP32 ADC Calibration Driver.

Application Ideas

The LDR light sensor module pairs well with the ESP32 S3 for a wide range of real-world projects that respond intelligently to ambient light conditions.

  1. Automatic Night Light: Turn on lights automatically when it gets dark in your room.
  2. Smart Curtain Controller: Open or close curtains based on sunlight intensity.
  3. Solar Panel Tracker: Optimize solar panel position to face the brightest light source.
  4. Energy-Saving System: Automatically turn off unnecessary lights when natural light is sufficient.
  5. Plant Growth Monitor: Track light exposure for indoor plants and gardens.
  6. Security System: Detect when lights are turned on or off in monitored areas.
  7. Photography Light Meter: Measure ambient light for camera settings.

Video Tutorial

Watch the step-by-step video walkthrough for this ESP32 S3 project below.

Challenge Yourself

These challenges are designed to help you push further with the ESP32 S3 and LDR light sensor module, progressing from quick wins to more involved builds.

  1. Beginner: Add an LED that turns on when the room is dark and turns off when it is bright.
  2. Beginner: Display "Bright", "Medium", or "Dark" on the Serial Monitor based on different analog value ranges.
  3. Intermediate: Control a servo motor to rotate based on light intensity levels.
  4. Intermediate: Create a light-activated alarm that triggers a buzzer when light is detected.
  5. Advanced: Build a data logger that records light levels with timestamps to an SD card.
  6. Advanced: Create an automatic plant watering system that only waters when light levels are optimal.

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!