Read LDR/Photoresistor Sensor using ESP32

Posted on

by

in

Read LDR/Photoresistor Sensor using ESP32

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 get triggered when it is nighttime can make use of this sensor.

How does LDR or Photoresistor work?

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 number of electrons is few but when the semiconductor materials receive 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.

Components/Tools

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

IDE/Software

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 (Analog To Digital Converters) namely ADC1 and ADC2 pins that control 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.

Code

#include <Arduino.h>

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

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!

One response to “Read LDR/Photoresistor Sensor using ESP32”

  1. Using Node.js and React to display chart of real-time sensor readings – donskytech.com

    […] our earlier post, I detailed how we can read the values of an LDR using an ESP32 microcontroller.  This sensor is […]

Leave a Reply

Your email address will not be published.