Light Dependent Resistor (LDR)
Interfacing Light Sensor (LDR) with ESP32
Interfacing Light Sensor (LDR) with ESP32 demonstrates how an LDR sensor can be interfaced with an ESP32 board to measure light intensity and control an LED according to the sensor value.
Objectives
- To study the working of the LDR sensor.
- To interface the LDR sensor with the ESP32 board.
- To write a program to control an LED using the LDR sensor.
Hardware Modules
- ESP32 Board
- PC / Laptop
- LDR Sensor Module
- LED
Theory
LDR stands for Light Dependent Resistor.
An LDR is a light-sensitive resistor whose resistance changes according to the intensity of light falling on its surface.
The LDR works on the principle of Photo-Conductivity.
As the intensity of light falling on the LDR increases, the resistance of the LDR decreases.
Similarly, as the intensity of light decreases, the resistance of the LDR increases.
More Light → Lower Resistance
Less Light → Higher Resistance
This change in resistance occurs because light contains photon energy. Higher light intensity provides more photon energy, which increases the number of charged carriers in the LDR material. As the number of charged carriers increases, the resistance decreases.
LDR Sensor
The LDR consists of a light-sensitive material such as cadmium sulfide.
The LDR has two terminals or pins.
The LDR is an analog sensor because its resistance changes according to the intensity of light.
The ESP32 can receive analog voltage through its analog-capable GPIO pins.
In this practical, the LDR sensor is connected to GPIO 34 of the ESP32.
Voltage Divider Circuit
The LDR produces a change in resistance according to the light intensity. However, the ESP32 analog input measures voltage.
Therefore, a voltage divider circuit is used to convert the resistance change of the LDR into a corresponding voltage.
The voltage divider consists of:
- LDR
- Resistor
- Supply
- Ground
- ESP32 analog input
The LDR sensor module provides the corresponding analog output voltage to the ESP32.
The analog output of the LDR module is connected to GPIO 34.
LDR → Voltage Divider → Analog Voltage → ESP32 GPIO 34
LDR Output
The analog value received by the ESP32 changes according to the intensity of light falling on the LDR.
The ESP32 reads this value using the analogRead() function.
The program compares the LDR value with a predefined threshold value.
In this practical, the threshold value is set to 1000.
LDR Value < 1000 → LED ON
LDR Value ≥ 1000 → LED OFF
ESP32 Analog Input
The LDR sensor is connected to GPIO 34 of the ESP32.
GPIO 34 is used as the analog input for reading the LDR sensor value.
The analog value is obtained using:
analogRead(ldrPin)
The value is stored in the variable ldrValue.
The program continuously reads and displays the LDR value on the Serial Monitor.
Connections
| Device | Pin | ESP32 | Function |
|---|---|---|---|
| LDR Module | Data / Output | GPIO 34 | Analog sensor input |
| LDR Module | VCC | Supply | Power |
| LDR Module | GND | GND | Ground |
| LED | Data | GPIO 2 | LED control |
| LED | GND | GND | Ground |
Steps for Assembling the Circuit
- Connect the analog output/data pin of the LDR module to GPIO 34 of the ESP32.
- Connect the VCC pin of the LDR module to the required supply.
- Connect the GND pin of the LDR module to GND of the ESP32.
- Connect the LED data pin to GPIO 2 of the ESP32.
- Connect the LED ground pin to GND of the ESP32.
- Check all connections before applying power.
Safety Precautions
- First, make all the connections according to the circuit diagram.
- Check the connections between the LDR module and ESP32.
- Check the LED connections.
- Ensure that the LDR output is connected to the correct analog GPIO.
- Ensure that all connections are correct before applying power.
- Then connect the ESP32 board to the PC/Laptop.
Schematic Diagram
The LDR module is connected to the analog input of the ESP32.
The LED is connected to a digital output GPIO of the ESP32.
The ESP32 reads the LDR sensor value and controls the LED according to the programmed threshold.
LDR → ESP32 GPIO 34 → Sensor Value → Threshold Comparison → LED
Interfacing Diagram Using Breadboard
The LDR module and LED can be connected to the ESP32 using a breadboard.
The LDR module provides the analog sensor value to GPIO 34.
The LED is controlled using GPIO 2.
Interfacing Diagram Using the IoT Kit
The LDR module and LED can be connected to the ESP32 through the corresponding connections provided on the IoT kit.
The LDR sensor provides the analog input, while the LED provides the visual output.
Procedure
- Write the program according to the algorithm given below.
- Save and compile the program.
- Connect the ESP32 board to the PC/Laptop using a USB cable.
- Upload the program to the ESP32.
- Open the Serial Monitor.
- Set the Serial Monitor baud rate to 9600.
- Observe the LDR sensor value.
- Observe the LED output according to the sensor value.
Algorithm
The following algorithm is used to interface the LDR sensor with the ESP32 and control the LED:
- Start the Arduino IDE.
- Declare a variable to store the analog GPIO connected to the LDR.
- Declare a variable to store the analog value received from the LDR module.
- Declare a variable to store the threshold value.
- Declare a variable to store the GPIO connected to the LED.
- In the void setup() function, configure the LDR GPIO as INPUT.
- Configure the LED GPIO as OUTPUT.
- Set the baud rate to 9600 using Serial.begin(9600).
- In the void loop() function, read the analog value from the LDR using analogRead(ldrPin).
- Print the LDR sensor value on the Serial Monitor.
- Compare the LDR sensor value with the threshold value.
- If the LDR value is less than the threshold value, make the LED GPIO HIGH.
- Otherwise, make the LED GPIO LOW.
- Give a delay of 100 ms.
- Repeat the process continuously.
The overall operation is:
LDR → ESP32 Analog Input → LDR Value → Threshold Comparison → LED ON/OFF
Working
The ESP32 reads the analog output of the LDR sensor through GPIO 34.
The value obtained from the sensor is stored in the variable ldrValue.
The sensor value is displayed on the Serial Monitor at a baud rate of 9600.
The program compares the LDR value with the threshold value of 1000.
If the LDR value is less than the threshold value, the ESP32 sets GPIO 2 HIGH and turns the LED ON.
LDR Value < 1000 → LED ON
If the LDR value is greater than or equal to the threshold value, the ESP32 sets GPIO 2 LOW and turns the LED OFF.
LDR Value ≥ 1000 → LED OFF
The process repeats continuously with a delay of 100 ms.
Threshold Operation
The threshold value used in the program is:
Threshold = 1000
The LED operation is:
| LDR Value | Condition | LED |
|---|---|---|
| Less than 1000 | LDR value < threshold | ON |
| 1000 or greater | LDR value ≥ threshold | OFF |
Program
/* Interfacing LDR with Arduino board and lighting 13 no. LED */
int ledPin = 2;
int ldrPin = 34;
// int ldrPin1 = 35;
// int ldrPin2 = 36;
int ldrValue;
int thresholdValue = 1000; // set threshold
void setup()
{
pinMode(ldrPin, INPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop()
{
ldrValue = analogRead(ldrPin);
Serial.println(ldrValue);
if (ldrValue < thresholdValue)
{
digitalWrite(ledPin, HIGH);
}
else
{
digitalWrite(ledPin, LOW);
}
delay(100);
}
Observation
Observe the output on the Serial Monitor.
The LDR sensor value is continuously displayed by the ESP32.
As the intensity of light falling on the LDR changes, the analog sensor value also changes.
Observe the LED connected to GPIO 2.
When the LDR value is less than the threshold value of 1000, the LED turns ON.
When the LDR value is equal to or greater than 1000, the LED turns OFF.
Result
The ESP32 successfully interfaces with the LDR sensor and reads the light intensity as an analog value through GPIO 34.
The ESP32 successfully compares the LDR sensor value with the programmed threshold value of 1000.
The LED connected to GPIO 2 is turned ON when the LDR value is less than the threshold and OFF when the value is equal to or greater than the threshold.