Utility Bar

ESP32 Reading a Photosensitive Resistor (LDR) - Lesson 5

ESP32 Reading a Photosensitive Resistor (LDR)

Ethan Zaitchik |

Table of Contents

    In this lesson you will learn how to use a photosensitive resistor module (LDR module) with your ESP32. You will read analog values to measure light levels and also monitor the built-in digital output LED on the module. We’ll be using the LM393 LDR version.

    This lesson is part of our ESP32 tutorials series, where each lesson builds on the previous one.

    What you will learn

    • How a photosensitive resistor module works
    • How to connect the module to the ESP32
    • How to read analog values using analogRead()
    • How to read the digital output from the module
    • How to display readings on the Serial Monitor

    Components needed

    Get All the Parts You Need

    This tutorial is part of our comprehensive ESP32 learning series. Instead of buying components individually, save time and money with our ESP32 Basic Starter Kit. It includes everything you need for this lesson and 20+ other projects.

    What's Included: ESP32 board, OLED display, sensors (DHT11, PIR, LDR), relay module, buzzers, LEDs, buttons, breadboard, resistors, and all cables, plus access to our complete lesson plans.

    View ESP32 Starter Kit →

    Step 1 - Understanding the Photosensitive Resistor Module

    The module has a built-in LDR and a voltage divider circuit. It provides:

    • AO (analog output): a voltage that changes continuously depending on light levels. On most modules with AO connected through a pull-down resistor, AO decreases as light increases.
    • DO (digital output): switches HIGH or LOW when the light level passes a set threshold. The module has a built-in LED that lights up when DO is HIGH.
    ESP32 microcontroller connected to photosensitive resistor (LDR) circuit diagram

    Step 2 - Wiring the LDR

    1. Place the module on the breadboard
    2. Connect VCC to 3.3 V on the ESP32
    3. Connect GND to GND on the ESP32
    4. Connect AO to GPIO 34 on the ESP32
    5. Connect DO to GPIO 32 on the ESP32

    Step 3 - Writing the Code to Read the LDR

    Open the Arduino IDE and enter the following code. This reads the light level and prints it to the Serial Monitor. It also shows the digital output from the module.

    int ldrAnalogPin = 34; // AO pin connected to ESP32
    int ldrDigitalPin = 32; // DO pin connected to ESP32
    int ldrValue = 0;
    int ldrDigital = 0;
    void setup() {
      Serial.begin(115200);
      pinMode(ldrDigitalPin, INPUT); // Set DO as input
    }
    void loop() {
      ldrValue = analogRead(ldrAnalogPin); // Read analog light level
      ldrDigital = digitalRead(ldrDigitalPin); // Read digital threshold
      Serial.print("Analog light level (AO): ");
      Serial.println(ldrValue);
      Serial.print("Digital output (DO): ");
      Serial.println(ldrDigital);
      delay(500); // Update every half second
    }
    

    How the Code Works

    This program reads light levels using a photosensitive resistor (LDR) module and shows the results in the Serial Monitor. It reads both the analog light level and the digital output threshold from the module.

    int ldrAnalogPin = 34;
    This stores the GPIO pin connected to the module's analog output (AO). GPIO 34 is used in this lesson.
    int ldrDigitalPin = 32;
    This stores the GPIO pin connected to the module's digital output (DO). GPIO 32 is used here.
    void setup()
    This function runs once when the ESP32 starts.
    Serial.begin(115200);
    Starts serial communication at 115200 baud so we can see readings in the Serial Monitor.
    pinMode(ldrDigitalPin, INPUT);
    Tells the ESP32 to use the digital pin as an input to read the module's HIGH/LOW signal.
    void loop()
    This function runs continuously.
    • ldrValue = analogRead(ldrAnalogPin); Reads the voltage from AO and stores it. Returns 0–4095. Bright light usually gives lower values (~100–800), darkness gives higher (~3000–4095).
    • ldrDigital = digitalRead(ldrDigitalPin); Reads the digital output (DO) and stores 0 (LOW) or 1 (HIGH). The module's built-in LED usually turns on when HIGH.
    • Serial.print() and Serial.println() send the readings to the Serial Monitor in real time.
    • delay(500); Pauses for half a second before reading again to prevent flooding the Serial Monitor.

    Step 4 - Uploading the Code

    1. Connect your ESP32 to the computer via USB
    2. Ensure correct board and port are selected in Arduino IDE
    3. Click Upload and wait until finished

    Step 5 - Viewing Sensor Readings

    1. Open the Serial Monitor in Arduino IDE
    2. Set baud rate to 115200
    3. You should see light level values updating every 0.5 seconds
    4. Cover the LDR or shine a light to see the digital output LED respond

    Optional Challenge

    Try blinking the internal LED on GPIO 2 when the LDR's light level reaches a certain threshold.

    ESP32 Lessons

    Lesson complete

    You have successfully read light levels with an LDR and monitored the digital output LED. Next lesson: How to use Push Buttons with a ESP32

    Leave a comment