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:

LDR Light Sensor Module
image source: diyables.io

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×USB Cable Type-C
1×LDR Light Sensor Module
1×Breadboard
1×Jumper Wires
1×(Recommended) ESP32 Screw Terminal Adapter

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: some of these links are affiliate links. We may earn a commission on your purchase at no extra cost to you. We appreciate it.

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.
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

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.
ESP32 LDR Light Sensor Module wiring diagram

This image is created using Fritzing. Click to enlarge image

If you're unfamiliar with how to supply power to the ESP32 and other components, you can find guidance in the following tutorial: How to Power ESP32.

  • The wiring diagram between ESP32 and the LDR light sensor module when using AO only.
ESP32 LDR Module wiring diagram

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.
ESP32 Light Sensor Module wiring diagram

This image is created using Fritzing. Click to enlarge image

ESP32 Code - Read value from DO pin

/* * This ESP32 code is created by esp32io.com * * This ESP32 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-ldr-module */ #define DO_PIN 13 // ESP32's pin GPIO13 connected to DO pin of the ldr module void setup() { // initialize serial communication Serial.begin(9600); // initialize the ESP32's pin as an input pinMode(DO_PIN, INPUT); } void loop() { int lightState = digitalRead(DO_PIN); if (lightState == HIGH) Serial.println("It is dark"); else Serial.println("It is light"); }

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.
COM6
Send
It is light It is light It is dark It is dark It is dark It is light It is light It is light
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

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

/* * This ESP32 code is created by esp32io.com * * This ESP32 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-ldr-module */ #define AO_PIN 36 // ESP32's pin GPIO36 connected to AO pin of the ldr module void setup() { // initialize serial communication Serial.begin(9600); } void loop() { int lightValue = analogRead(AO_PIN); Serial.print("The AO value: "); Serial.println(lightValue); }

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.
COM6
Send
The AO value: 145 The AO value: 146 The AO value: 146 The AO value: 572 The AO value: 1678 The AO value: 1945 The AO value: 2956 The AO value: 3001 The AO value: 3098 The AO value: 4005 The AO value: 4005 The AO value: 1645 The AO value: 1546 The AO value: 346 The AO value: 172
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

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.

※ OUR MESSAGES