Index
Introduction
In this project, we will build an automatic night lamp using an LDR Sensor and an ESP32-S3. When the surrounding light level drops, the LED will turn ON, and when it’s bright, the LED will turn OFF.
Required Components
- ESP32-S3 Board
- LDR Sensor
- LED
- 220Ω Resistor
- Breadboard
- Jumper Wires
Pinout
- DHT11 Sensor
- VCC: Power supply pin (3.3V to 5V)
- GND: Ground pin
- DO: Digital data output pin

- LED
- LED Anode (+)
- LED Cathode (-)

Circuit Diagram
- LDR Sensor
- LDR Sensor VCC → ESP32-S3 3.3V
- LDR Sensor GND → ESP32-S3 GND
- LDR Sensor DO → ESP32-S3 GPIO 18
- LED
- Anode (Long Leg) → ESP32-S3 GPIO 2 (via 220Ω Resistor)
- Cathode (Short Leg) → GND
Code / Programming
/*
Filename: ol_ldr_auto_led.ino
Description: Reads LDR sensor and turns LED ON in dark and OFF in light
Author: www.oceanlabz.in
Modification: 1/7/2025
*/
#define LDR_PIN 18 // LDR connected to GPIO 34
#define LED_PIN 2 // LED connected to GPIO 26
void setup() {
pinMode(LED_PIN, OUTPUT); // Set LED pin as output
Serial.begin(115200); // Start serial communication
}
void loop() {
int ldrValue = analogRead(LDR_PIN); // Read LDR value
Serial.println(ldrValue); // Print value to Serial Monitor
if (ldrValue > 1000) {
digitalWrite(LED_PIN, HIGH); // Turn ON LED in darkness
} else {
digitalWrite(LED_PIN, LOW); // Turn OFF LED in light
}
delay(500); // Delay for half a second
}
Explanation
- LDR detects light intensity, and the ESP32-S3 reads its analog value.
- If LDR value is low (dark environment), the LED turns ON.
- If LDR value is high (bright environment), the LED turns OFF.
Troubleshooting
- LED not turning ON? Try adjusting the threshold value (
1000
). - LDR value not changing? Check LDR connections.
- ESP32-S3 not reading LDR values? Use a different ADC pin.