How to wire an LDR to an ESP32
To wire an LDR to an ESP32, connect 1 to 3V3 and 2 to GPIO 34 (D34). The LDR and its 10 kilohm resistor form a divider between 3.3 V and GND, so the pin never sees more than 3.3 V. The sketch below uses no library beyond what the board includes. It reads the divider and prints the light level with the word Dark, Dim or Bright.
- 3.3 V logic
- Library: none
- 2 wires, 1 helper parts
LDR to ESP32 wiring table
Every wire in the circuit above, with the part’s pin names and the board’s own labels. The wire colors are the ones the circuit uses; on a real bench any colors work.
| LDR pin | ESP32 pin | Wire color | Why |
|---|---|---|---|
| LDR, one leg | 3V3 | red | The top of the voltage divider. |
| LDR, other leg | GPIO 34 (D34) | amber | The middle of the divider, where the reading is taken. |
| 10 k resistor, one end | The LDR leg on GPIO 34 (D34) | n/a | Joined to the same node as the analog pin. |
| 10 k resistor, other end | GND | n/a | The bottom of the divider. |
Helper parts
The extra parts in the circuit above, and where their wires go:
| From | To | Wire color |
|---|---|---|
| R1 (10 kΩ) pin 1 | 2 | amber |
| R1 (10 kΩ) pin 2 | GND | blue |
Power and logic levels for LDR and ESP32
Logic level
The ESP32’s GPIO pins are 3.3 V logic and are not 5 V tolerant. Nothing wired to a pin may go above 3.3 V.
Supply
The LDR takes 3.3 V from 3V3. That pin is the ESP32’s own regulated 3.3 V output.
This pair
The divider runs between 3V3 and GND, so the pin never sees more than 3.3 V. Do not use VIN (5 V from USB) for it: in bright light the pin would rise toward 5 V. The ESP32’s ADC is twelve bits, 0 to 4095, and at the default attenuation it saturates at about 3.1 V, so a pot across the 3.3 V rail reaches 4095 a little before the end of its travel and reads about 1890 in the middle.
Pins to leave alone
GPIO 34, 35, 36 (VP) and 39 (VN) are input only: they cannot drive a pin or take an internal pull-up. GPIO 0, 2, 5, 12 and 15 are strapping pins, read at power-on, and GPIO 6 to 11 are the flash chip.
The working code for the ESP32
Library: none, the sketch reads the divider with analogRead(). This is the sketch the circuit above runs.
const int LDR_PIN = 34; // the divider's middle, GPIO 34 (D34)
void setup() {
Serial.begin(115200);
}
void loop() {
int raw = analogRead(LDR_PIN); // 0 to 4095
int percent = map(raw, 0, 4095, 0, 100);
Serial.print("Light: ");
Serial.print(raw);
Serial.print(" ");
Serial.print(percent);
Serial.print("% ");
if (percent < 25) {
Serial.println("Dark");
} else if (percent < 60) {
Serial.println("Dim");
} else {
Serial.println("Bright");
}
delay(250);
}
What you should see
Open the serial monitor at 115200 baud. Each line shows the raw reading, the level as a percentage and Dark, Dim or Bright. More light lowers the LDR’s resistance and raises the reading.
The LDR has no polarity, so either leg can go to either side. Swap the LDR and the resistor and the readings invert.
The serial monitor prints this, recorded from the circuit above with the light turned up and then down:
Light: 3312 80% Bright
Light: 4095 100% Bright
Light: 0 0% DarkCommon mistakes wiring an LDR to an ESP32
No fixed resistor
An LDR alone is a resistor between two pins, not a voltage. With nothing from the analog pin to ground the pin floats, and the reading barely follows the light. The 10 k to GND is what makes it a divider.
Reading it backwards
With the LDR on the supply side, as here, bright light reads high. Swap the LDR and the resistor and bright reads low. Both work as long as the code agrees with the wiring.
Running the divider from VIN (5 V from USB)
In bright light the middle of the divider rises toward the supply. From VIN (5 V from USB) that is more than the ESP32 (Xtensa LX6)'s analog input can take. Use 3V3.
Build your own LDR project
Open this circuit in the editor, change the wiring or the code, and keep your version in a free account.