Esp32

Light Dependent Resistor – LDR Sensor with ESP32 Using Arduino IDE

The ESP32 is a powerful microcontroller equipped with features for IoT. ESP32 with LDR can measure light intensity and trigger response according to it. Using ESP32 and an LDR we can create a remote light sensing-based project and design a variety of innovative IoT solutions for various industries and applications.

In this guide, the basics of LDR and its applications with ESP32 will be covered.

1: Introduction to LDR Sensor

2: Applications of LDR with ESP32

3: Interfacing LDR with ESP32 Using Arduino IDE

Conclusion

1: Introduction to LDR Sensor

A Light Dependent Resistor (LDR) is a type of resistor that changes its resistance based on the intensity of light it is exposed to. In darkness, its resistance is very high, while in bright light its resistance is very low. This change in resistance makes it best for light sensing projects.


The ESP32 analog pins convert the incoming voltages to an integer between 0 and 4095. This integer value is mapped against the analog input voltage from 0V to 3.3V which is by default the ADC reference voltage in ESP32. This value is read using the Arduino analogRead() function from LDR.

For further detailed guide and ADC pinout of ESP32 read the article ESP32 ADC – Read Analog Values with Arduino IDE.


The ESP32 has a built-in analog-to-digital converter (ADC) that can measure the voltage across the LDR and convert it into a digital signal that can be processed by the microcontroller. Using this signal ESP32 determines the resistance of the LDR, which is proportional to the light intensity.

Here we will be using the ESP32 ADC channel 1 pins.


Photons or light particles play a crucial role in the operation of LDRs. When light falls on the surface of an LDR, photons are absorbed by the material, which then frees electrons in the material. The number of free electrons is directly proportional to the intensity of light, and the more electrons that are freed, the lower the resistance of the LDR becomes.

2: Applications of LDR with ESP32

Following is the list of some IoT based applications of LDR with ESP32:

    • Light activated switch
    • Light level indicator
    • Night mode in devices
    • Light-based security systems
    • Smart lighting systems
    • Light-sensitive security systems
    • Plant monitoring
    • Energy-efficient lighting
    • Automated window blinds

3: Interfacing LDR with ESP32 Using Arduino IDE

To use an LDR with the ESP32 we need to connect the LDR with an ESP32 ADC channel pin. After that Arduino code is needed that will read analog values from the LDR output pin. To design this circuit, we need LDR, a resistor, and the ESP32 board.

The LDR and the resistor are connected in series, with the LDR connected to the analog channel 1 input pin of ESP32. A LED will be added to the circuit that can test LDR working.

3.1: Schematic

The circuit diagram for interfacing LDR with ESP32 is quite simple. We need to connect the LDR and a resistor in a voltage divider configuration and connect the output of the voltage divider to the ADC (Analog to Digital Converter) pin of ESP32. ADC channel 1 pin D34 is used as an analog input for ESP32.

Following image is the schematic of ESP32 with LDR sensor.

3.2: Code

Once the circuit has been set up, the next step is to write the code for the ESP32. The code will read the analog input from the LDR and use it to control an LED or other device based on different light levels.

int LDR_Val = 0;     /*Variable to store photoresistor value*/
int sensor =34;      /*Analogue Input for photoresistor*/
int led= 25;         /*LED output Pin*/
void setup() {
    Serial.begin(9600);     /*Baud rate for serial communication*/
    pinMode(led, OUTPUT);   /*LED Pin set as output */
}
void loop() {
    LDR_Val = analogRead(sensor);   /*Analog read LDR value*/
    Serial.print("LDR Output Value: ");
    Serial.println(LDR_Val);   /*Display LDR Output Val on serial monitor*/
    if(LDR_Val > 100) {       /*If light intensity is HIGH*/
        Serial.println(" High intensity ");
        digitalWrite(led,LOW); /*LED Remains OFF*/
    }
    else {
     /*Else if Light intensity is LOW LED will Remain ON*/
        Serial.println("LOW Intensity ");
        digitalWrite(led,HIGH);  /* LED Turn ON LDR value is less than 100*/
    }
    delay(1000);     /*Reads value after every 1 sec*/
}

 
In above code we use a LDR with ESP32 that will control LED using the analog input coming from LDR.

The first three lines of code declare variables to store the photoresistor value, the analog pin for the photoresistor, and the LED output pin.

In the setup() function, the serial communication is initiated with a baud rate of 9600 and LED pin D25 is set as output.

In the loop() function, the photoresistor value is read using the analogRead() function, which is stored in the LDR_Val variable. The photoresistor value is then displayed on the serial monitor using the Serial.println() function.

An if-else statement is used to control the LED based on the light intensity detected by the photoresistor. If the photoresistor value is greater than 100, it means the light intensity is HIGH, and the LED remains OFF. However, if the photoresistor value is less than or equal to 100, it means the light intensity is LOW, and the LED turns ON.

Finally, the program waits for 1 second using the delay() function before reading the photoresistor value again. This cycle repeats indefinitely, making the LED turn ON and OFF based on the light intensity detected by the photoresistor.

3.3: Output under Dim Light

Light intensity is less than 100 so LED will remain ON.

3.4: Output under Bright Light

As light intensity increases the LDR value will increase and LDR resistance will decrease so LED will turn OFF.

Conclusion

The LDR can be interfaced with ESP32 using the ADC channel 1 pin. The LDR output can control light sensing in various applications. With its low cost and compact size, the ESP32 and LDR make an attractive choice for IoT projects that require light sensing capabilities. Using the Arduino analogRead() function we can read values from LDR.

About the author

Kashif

I am an Electrical Engineer. I love to write about electronics. I am passionate about writing and sharing new ideas related to emerging technologies in the field of electronics.