Index

Introduction
In this project, we will build an automatic night lamp using an LDR Sensor and an ESP32. When the surrounding light level drops, the LED will turn ON, and when it’s bright, the LED will turn OFF.
Required Components
- ESP32 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 → ESP32 3.3V
- LDR Sensor → ESP32 GND
- LDR Sensor → ESP32 GPIO 34
- LED
- Anode (Long Leg) → ESP32 GPIO 26 (via 220Ω Resistor)
- Cathode (Short Leg) → GND

Arduino Code
#define LDR_PIN 34 // LDR connected to GPIO 34
#define LED_PIN 26 // LED connected to GPIO 26
void setup() {
pinMode(LED_PIN, OUTPUT);
Serial.begin(115200);
}
void loop() {
int ldrValue = analogRead(LDR_PIN); // Read LDR value
Serial.println(ldrValue);
if (ldrValue < 1000) { // Adjust threshold based on ambient light
digitalWrite(LED_PIN, HIGH); // Turn ON LED in dark
} else {
digitalWrite(LED_PIN, LOW); // Turn OFF LED in bright light
}
delay(500);
}
Explanation
- LDR detects light intensity, and the ESP32 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 not reading LDR values? Use a different ADC pin (like GPIO 35).