Read LDR/Photoresistor Sensor using ESP32

ESP32 LDR Wiring

This short post will discuss how we can read sensor values coming from a Light Dependent Resistor (LDR) or a photoresistor using an ESP32 board. The Serial Monitor of our PlatformIO IDE then outputs the values read from our sensor.

A photoresistor or an LDR or a photocell acts like a variable resistor whose value changes depending on the intensity of light that is exposed. If exposed to high light intensity then the resistance is low otherwise it could be high as in megaohms when it is in the dark. This makes this sensor ideal for Internet of Things (IOT) project that needs to act differently depending on the amount of light. Home automation projects that gets triggered when it is night time can make use of this sensor.

How LDR or Photoresistor works?

If you need to detect the presence (or absence) of light then use LDR or Photoresistors (or PhotoCells) in your projects The intensity of light causes the resistance to vary which could be from zero to mega ohms in values.

Semiconductor materials that have high light sensitivity properties are used in creating this component. Semiconductors tend to exhibit high resistance by default. The amount of electrons are few but when the semiconductor materials receives light photons then it triggers the electrons to break free causing conductivity.

These components are widely available and are easy to use in microcontroller projects.


  • LDR or Photoresistor
  • ESP32 module (I used 38Pin NodeMCU ESP32S board here)
  • Breadboard
  • Connecting wires


We will use Visual Studio Code as our IDE in developing this project and install the PlatformIO IDE extension. We will be using the Arduino framework as well to power up our project. If you are using Windows/PC then I have documented steps on how to install visual studio code.

Schematic/Wiring Diagram – ESP32 with LDR/Photoresistor

The following image below is the schematic diagram of our circuit. The Photoresistor/LDR is in series with a resistor to create a voltage divider circuit. The resistance of the Photoresistor/LDR varies depending on the intensity of the light. The GPIO36 pin of the ESP32 reads the amount of voltage drop. Note that the resistance of the Photoresistor/LDR is inversely proportional to the intensity of light captured by the sensor.

Note on choosing GPIO Pins on ESP32 when using WIFI

The ESP32 has 2 ADC’s namely ADC1 and ADC2 pins that controls several gpio pins under it. If your project will use WIFI functionality along with analogRead then only use pins under ADC1. The ADC2 pins are used internally by ESP32 modules for wifi functionality so you might encounter undesired read results.


#include <Arduino.h>

int sensorVal;
const int ANALOG_READ_PIN = 36; // or A0
const int RESOLUTION = 12; // Could be 9-12
void setup()
void loop()
  //Read and print the sensor pin value
  sensorVal = analogRead(ANALOG_READ_PIN);
  Serial.print(" \n"); 
  //sleep for some time before next read

The code for this project is in my github account and you can view it here.

The loop() function contains much of the logic in reading the sensor reads of the LDR/Photoresistor. The analogReadResolution() function will configure the values of the sensor reads and has a default value of 12.

The function analogRead() is what the ESP32 is using to read the Photoresistor or LDR sensor read. It then outputs the value to our serial monitor.

That’s it!

Happy Exploring!

If you like my post then please consider sharing this. Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *