An LDR (Light Dependent Resistor) is a sensor that changes its resistance based on light intensity:
By connecting the LDR to an analog input pin of ESP32, we can measure the light intensity and use it in IoT projects. In this tutorial, we’ll connect the LDR to GPIO 35 of ESP32 DOIT DevKit v1 and display the sensor values in the Serial Monitor.
⚠️ This forms a voltage divider circuit, allowing ESP32 to read varying light levels as an analog value.
// LDR with ESP32 DOIT DevKit v1
// LDR connected to GPIO 35
int ldrPin = 35; // LDR connected to ADC pin
int ldrValue = 0; // Variable to store LDR reading
void setup() {
Serial.begin(115200); // Start Serial Monitor
}
void loop() {
ldrValue = analogRead(ldrPin); // Read LDR value (0-4095)
Serial.print("LDR Value: ");
Serial.println(ldrValue);
delay(500); // Small delay for stability
}
Define Variables
int ldrPin = 35;
int ldrValue = 0;
ldrValue
to store sensor readings.Setup Serial Monitor
Serial.begin(115200);
Starts serial communication to display sensor values.
Loop Function
ldrValue = analogRead(ldrPin);
Serial.print("LDR Value: ");
Serial.println(ldrValue);
Problem | Cause | Solution |
---|---|---|
Constant value (no change) | Wrong wiring | Check LDR + resistor connections |
Values always HIGH | No voltage divider | Ensure 10kΩ resistor connected to GND |
Flickering values | Noise in sensor | Add small capacitor (0.1µF) across LDR & resistor |
Serial Monitor not showing | Wrong baud rate | Use 115200 baud in Arduino IDE |
ESP32’s ADC gives values from 0 to 4095 (12-bit resolution).
No, you need a voltage divider with a resistor to read proper values.
Yes, ESP32 has multiple ADC pins, so you can connect several LDRs.
No, ESP32 is 3.3V logic. Using 5V may damage it.
You can add a condition in code: if light < threshold → turn ON LED/relay.