LDR + ESP32: Pinout, Wiring Diagram and Working Code
Share
LDR + ESP32: Pinout, Wiring Diagram and Working Code
Quick answer: an LDR has two identical leads. Connect one lead to the ESP32's 3V3 and the other to GPIO34, then add a 10 kΩ resistor from GPIO34 to GND. More light lowers the LDR's resistance, so the reading rises. Use an ADC1 pin such as GPIO34 so the reading still works when Wi-Fi is on.
What is the LDR pinout?
A photoresistor is a two-terminal resistor whose value falls as light increases, as the GL55-series manual explains. It has no polarity, so either lead can go to either side of the circuit. The ESP32 cannot measure resistance directly, which is why the LDR goes into a voltage divider.
| Lead | Function | Source |
|---|---|---|
| Lead 1 | Resistor terminal (no polarity) | GL55-series manual + photo |
| Lead 2 | Resistor terminal (no polarity) | GL55-series manual + photo |
How do you wire the LDR to an ESP32?
| LDR pin | ESP32 DevKit pin | Why this pin |
|---|---|---|
| LDR lead 1 | 3V3 | Top of the divider |
| LDR lead 2 | GPIO34 | Input-only ADC1 pin: works with Wi-Fi on, cannot be driven by mistake |
| 10 kΩ resistor | GPIO34 to GND | Bottom of the divider; sets the useful range |
Swap the LDR and the resistor if you want the reading to rise in darkness instead. The listing pairs this LDR with a 10 kΩ resistor, which suits indoor light levels.
Which ESP32 pins should you avoid?
Every GPIO choice on this page follows the same rules for the 30-pin ESP32-WROOM-32 DevKit (the board Compoden stocks as the ESP32 30-pin CP2102 development board). Espressif's ESP32 GPIO reference is the source:
- Never use GPIO6–11. They connect to the module's SPI flash.
- Avoid the strapping pins GPIO0, 2, 5, 12 and 15 for sensors. Their level at reset decides boot mode and flash voltage, so a sensor holding one of them can stop the board booting. GPIO0 is not even on the 30-pin header; it only goes to the BOOT button.
- GPIO34, 35, 36 and 39 are input-only and have no internal pull-up or pull-down. They are ideal for analog inputs and useless for outputs or clocks.
- Use ADC1 (GPIO32–39) for analog readings. ADC2 pins (GPIO0, 2, 4, 12–15, 25–27) cannot be read while Wi-Fi is running.
- Leave GPIO1 and GPIO3 alone. They carry the USB serial link used for uploads and the Serial Monitor.
- ESP32 GPIOs are 3.3 V only. They are not 5 V tolerant.
What voltage and resistance should you expect?
The GL55-series manual rates the 5 mm parts for up to 150 V and 100 mW (90 mW for the GL5516), far above anything a 3.3 V divider applies. It lists the GL5528 at 10–20 kΩ at 10 lux, at least 1 MΩ in darkness and about 20 ms rise and 30 ms decay time. Treat those as typical figures and measure your own part with a multimeter if the exact value matters.
The ESP32 reads 0–4095 by default with 12-bit resolution. Its ADC is least accurate near both ends of the range, so choose a resistor that puts your normal light level near the middle. analogReadMilliVolts() applies the chip's calibration and is more useful than the raw count when you compare boards.
Working Arduino code for the LDR on ESP32
No library is needed. The sketch prints the raw value, the calibrated millivolts and a bright/dark label with a threshold you can tune.
// 5 mm LDR (photoresistor) + ESP32 DevKit (ESP32-WROOM-32)
// Wiring: 3V3 -> LDR -> GPIO34 -> 10 kOhm resistor -> GND (brighter light = higher reading)
// GPIO34 is an input-only ADC1 pin, so it keeps working when Wi-Fi is on.
const int LDR_PIN = 34;
const int BRIGHT_THRESHOLD = 2000; // tune for your room
void setup() {
Serial.begin(115200);
analogReadResolution(12); // 0-4095
}
void loop() {
int raw = analogRead(LDR_PIN);
int mv = analogReadMilliVolts(LDR_PIN);
Serial.printf("LDR raw=%d %d mV %s\n", raw, mv, raw > BRIGHT_THRESHOLD ? "bright" : "dark");
delay(500);
}
Compile check: this exact sketch compiled for the esp32:esp32:esp32 board (ESP32 Dev Module, ESP32 Arduino core 3.3.10) on 5 October 2026. A clean compile proves the code and libraries agree. It does not prove your wiring, so test it on the bench with the Serial Monitor at 115200 baud.
Common mistakes with the LDR and ESP32
- Using an ADC2 pin (GPIO0, 2, 4, 12–15, 25–27). Its readings stop when Wi-Fi starts.
- No fixed resistor. Without the divider the pin floats and the readings are noise.
- Wiring the top of the divider to 5 V. The pin could then see more than 3.3 V in bright light.
- Expecting lux. An LDR gives relative light levels. For calibrated lux, use a digital light sensor.
- Too fast a response expected. The manual lists tens of milliseconds to respond, so it is not a data receiver.
Where can you get the parts?
| Item | Role | Link |
|---|---|---|
| 5 mm LDR photoresistor | Light-dependent resistor | View LDR |
| ESP32 30-pin CP2102 development board | Controller with ADC, Wi-Fi and Bluetooth | View ESP32 board |
Want this sensor in a bigger build? Describe the whole device in Soldr, for example “I want to build an automatic night lamp with an ESP32 and an LDR”. Soldr returns the exact parts, a wiring map and code that compiles, all from one parts list, so the pins in the code match the pins in the wiring. Compoden stocks the parts and delivers across India.
Sources
Photoresistor behaviour, ratings and GL5528 figures: Senba Optical & Electronic GL55-series CdS photoresistor manual (retailer-hosted copy). ESP32 ADC and pin rules: Espressif ESP32 GPIO reference. Model and divider value: Compoden LDR listing.
Related ESP32 wiring guides
- Vibe coding hardware: build an ESP32 project from one sentence
- TCS34725 colour sensor + ESP32
- DHT22 temperature and humidity + ESP32
- XKC-Y25-V non-contact liquid level + ESP32
- INMP441 I2S microphone + ESP32
- Capacitive soil moisture sensor v2.0 + ESP32
- HX711 and 5 kg load cell + ESP32
- Water level sensor + ESP32
- AS5600 magnetic angle sensor + ESP32
- AHT21B temperature and humidity + ESP32
Frequently asked questions
Which ESP32 pin should I use for an LDR?
Use an ADC1 pin, GPIO32 to GPIO39. GPIO34 is a good choice because it is input-only and keeps working while Wi-Fi is on.
What resistor goes with an LDR on an ESP32?
A 10 kΩ resistor from the analog pin to GND, with the LDR from 3V3 to the pin, works for typical indoor light. Pick a value close to the LDR's resistance at your normal light level.
Does an LDR have polarity?
No. It is a resistor, so either lead can go either way.