Skip to content

Light · ESP32 guide

Interfacing LDR Photoresistor (GL5528 Light-Dependent Resistor) with ESP32

The simplest light sensor: a resistor that drops from about 1 MΩ in the dark to a few hundred ohms in bright light.

Updated November 16, 2026 Arduino C++Also forArduino Uno

What is the LDR (Photoresistor)?

An LDR (light-dependent resistor), also called a photoresistor or photocell, is a resistor whose value depends on how much light falls on it. In bright light it drops to a few hundred ohms; in the dark it climbs past a million. It has two legs, no polarity and no electronics inside, which makes it the simplest and cheapest light sensor you can buy.

LDRs turn up in automatic night lights, street lamps that switch on at dusk, light-seeking robots, curtain controllers and "is the lid open?" detectors. This page explains how an LDR works, how to turn its changing resistance into a voltage your board can read, how to choose the fixed resistor, and where an LDR is the wrong choice. For complete builds, follow the Arduino Uno guide or the ESP32 guide. Both include a night-light circuit you can run in the SolderHub Simulator.

Specifications

Type
Cadmium sulphide (CdS) photoresistor, 5 mm (GL5528 class)
Resistance at 10 lux
8–20 kΩ (typical part about 10–15 kΩ)
Resistance at 100 lux
About 2–4 kΩ
Dark resistance
1 MΩ or more (measured 10 s after the light goes off)
Gamma (slope, 10–100 lux)
About 0.7
Peak spectral response
540 nm (green, close to the human eye)
Response time
About 20 ms to rise, 30 ms to decay
Maximum voltage
150 V DC
Maximum power
100 mW at 25 °C
Operating temperature
−30 °C to +70 °C
Polarity
None: either leg can go either way
Lead spacing
About 3.4 mm

Figures for the common 5 mm GL5528 from the Senba GL55 series datasheet. Other GL55xx types (GL5516, GL5537, GL5539) have higher or lower resistance ranges.

Pinout

PinNameConnection
Leg 1TerminalTo the supply (5 V on an Uno, 3.3 V on an ESP32) in the usual divider
Leg 2TerminalTo the analog pin and, through the fixed resistor, to GND
—Divider midpointNot an LDR leg: the junction of the LDR and the fixed resistor goes to the ADC pin

An LDR has no polarity. Either leg can go to either side.

Wiring & code

Setting up LDR (Photoresistor) with ESP32

In this guide you will read an LDR with an ESP32, measure the light as a calibrated voltage in millivolts, and build an automatic night light that switches an LED on at dusk. You'll also learn the two ESP32 details that catch people out with analog sensors: which pins still work when Wi-Fi is on, and why raw ADC numbers aren't quite linear.

It takes about 15 minutes and needs no extra libraries. The finished circuit is also ready to run in the SolderHub Simulator.

What you need

  • ESP32 DevKit (38-pin or 30-pin)

    Qty
    1
    Notes
    Classic ESP32-WROOM-32 board
  • LDR (GL5528 or similar, 5 mm)

    Qty
    1
  • 10 kΩ resistor

    Qty
    1
    Notes
    Brown-black-orange. The fixed half of the divider
  • LED (any colour)

    Qty
    1
    Notes
    The night light
  • 220 Ω resistor

    Qty
    1
    Notes
    Red-red-brown
  • Breadboard and jumper wires

    Qty
    1 + 6
    Notes
    A full-size breadboard suits the wide ESP32
  • USB cable

    Qty
    1
    Notes
    A data cable, not charge-only

Danger

Power the divider from 3V3, never 5V. The ESP32's analog pins accept at most 3.3 V. Fed from 5 V, the divider could put more than 3.3 V on GPIO34 in bright light.

How the circuit works

The divider is the same as on an Arduino, scaled to 3.3 V:

  • 3V3 → LDR → GPIO34 → 10 kΩ → GND

Bright light means a low LDR resistance and a voltage close to 3.3 V; darkness means a high resistance and a low voltage.

Why GPIO34? The ESP32 has two analog converters. ADC2 (GPIO0, 2, 4, 12–15, 25–27) stops working whenever Wi-Fi is on, because the radio uses it. ADC1 (GPIO32–39) always works. GPIO34 is on ADC1 and is input-only, which suits a sensor perfectly. The LED goes on GPIO26, an ordinary output pin.

Animated: bright light gives about 3.02 V at GPIO34; near-darkness gives about 0.81 V.

Wiring: LDR to ESP32 DevKit

FromToWire colourWhy
ESP32 3V3LDR leg 1OrangeTop of the divider; 3.3 V keeps GPIO34 safe
LDR leg 2ESP32 GPIO34GreenDivider midpoint on an ADC1 pin
10 kΩ resistorBetween GPIO34 row and GNDBlack (to GND)Bottom of the divider
ESP32 GPIO26220 Ω → LED long legBlueNight-light output
LED short legESP32 GNDBlackCompletes the LED circuit
The ESP32 night-light circuit in the SolderHub Simulator: 3V3 to the LDR (orange), LDR to GPIO34 with the 10 kΩ to GND (green and grey), GPIO26 through 220 Ω to the LED (blue).

Wire it up, step by step

  1. 1Unplug the ESP32 and place it across the centre gap of the breadboard.
  2. 2Push the LDR into the breadboard with its legs in different rows.
  3. 3Run an orange wire from 3V3 to the first LDR leg. Double-check it isn't the 5V pin.
  4. 4Put the 10 kΩ from the second LDR leg's row to a row connected to GND.
  5. 5Run a green wire from the second LDR leg's row (the midpoint) to GPIO34 (labelled 34 or D34).
  6. 6Connect GPIO26 to the 220 Ω resistor, then to the LED's long leg; the LED's short leg goes to GND.
  7. 7Check that GPIO34 is the pin labelled 34, not VP (GPIO36) or VN (GPIO39) next to it.

Set up the Arduino IDE for ESP32

  1. In Arduino IDE 2, open File → Preferences and add https://espressif.github.io/arduino-esp32/package_esp32_index.json to Additional boards manager URLs.
  2. In Tools → Board → Boards Manager, install esp32 by Espressif Systems.
  3. Choose Tools → Board → esp32 → ESP32 Dev Module and select your port.
  4. Paste the sketch below and click Upload. If it stalls at Connecting…, hold the BOOT button until the upload starts.
sketch.ino
// LDR (GL5528) + ESP32: light meter and automatic night light
// Divider: 3V3 -> LDR -> GPIO34 -> 10 kΩ -> GND  (brighter = higher voltage)

const int LDR_PIN = 34;          // ADC1 pin: keeps working when Wi-Fi is on
const int LED_PIN = 26;          // GPIO26 -> 220 Ω -> LED -> GND

const int ON_BELOW_MV  = 1650;   // about 10 lux (dusk)
const int OFF_ABOVE_MV = 1950;   // a little brighter

bool lightOn = false;

void setup() {
  Serial.begin(115200);
  pinMode(LED_PIN, OUTPUT);
  analogReadResolution(12);      // 0-4095
}

void loop() {
  // analogReadMilliVolts() uses the chip's calibration: more accurate than raw counts
  uint32_t sum = 0;
  for (int i = 0; i < 16; i++) {
    sum += analogReadMilliVolts(LDR_PIN);
    delay(2);
  }
  int mv = sum / 16;

  if (!lightOn && mv < ON_BELOW_MV)  lightOn = true;
  if (lightOn  && mv > OFF_ABOVE_MV) lightOn = false;
  digitalWrite(LED_PIN, lightOn ? HIGH : LOW);

  // Work backwards from the divider to the LDR's resistance
  float rLdr = (mv > 0) ? 10000.0f * (3300.0f - mv) / mv : 1e6f;

  Serial.printf("%4d mV   LDR ≈ %6.0f Ω   Night light: %s\n", mv, rLdr, lightOn ? "ON" : "OFF");
  delay(250);
}

How the code works

  • analogReadMilliVolts() returns the voltage in millivolts using the calibration stored in each ESP32 chip at the factory. Raw analogRead() counts aren't perfectly linear on the ESP32, especially near 0 V and 3.3 V, so millivolts are the better choice.
  • Averaging 16 readings removes lamp flicker and the ESP32 ADC's own noise.
  • Two thresholds, on below 1650 mV and off above 1950 mV, add hysteresis so the LED doesn't flicker at dusk. With a typical GL5528 and 10 kΩ, 1650 mV is about 10 lux.
  • Back to resistance. Rearranging the divider gives R_LDR = 10 kΩ × (3300 − mV) ÷ mV, so the Serial Monitor shows the LDR's resistance too. That's handy for choosing thresholds and fixed resistors.

What you should see

Open the Serial Monitor at 115200 baud, then cover and uncover the LDR:

Output
2980 mV   LDR ≈   1074 Ω   Night light: OFF
2965 mV   LDR ≈   1130 Ω   Night light: OFF
 812 mV   LDR ≈  30640 Ω   Night light: ON
 798 mV   LDR ≈  31353 Ω   Night light: ON
2941 mV   LDR ≈   1221 Ω   Night light: OFF

Tip

Try it right here. The circuit below is the one from this guide, running in the SolderHub Simulator. Use the Try it panel to change the light, press Run, and open the Serial Monitor to watch the millivolts and the night light. The ESP32 simulator is newer than the Uno one, so if anything differs from your real board, trust the hardware.

Try this circuit in the SolderHub Simulator

Run this exact circuit in your browserReady-wired, code loaded

Confirm simulator behavior against the real wiring and the sensor datasheet before connecting hardware.

The ESP32 circuit with the LDR at 2 lux: its resistance rises to about 31 kΩ and GPIO34 drops to roughly 0.8 V.

Tune it and go wireless

Note the millivolt reading at the moment you want the light to come on, set ON_BELOW_MV to it, and set OFF_ABOVE_MV about 300 mV higher.

Because GPIO34 is on ADC1, the same code keeps working when you add Wi-Fi. You can publish the light level to MQTT or Home Assistant, show it on a web page, or switch a smart plug at dusk. For battery projects, read the LDR, send the value, then go to deep sleep for a few minutes. See the ESP32 deep sleep guide. The divider draws 0.1–3 mA through the 10 kΩ, so power it from a GPIO pin set HIGH only while measuring if every microamp counts.

Which resistor goes with my LDR?

An LDR is read through a voltage divider: the LDR plus a fixed resistor. The divider is most sensitive where the fixed resistor equals the LDR's resistance, so pick the light level your project has to judge, and this calculator suggests the resistor and shows what your board will read from darkness to daylight.

10 lux · LDR ≈ 10 kΩ at this light

LDR details (optional)

GL5528: 8–20 kΩ at 10 lux, gamma about 0.7. Measure yours with a multimeter for the best result.

Suggested fixed resistor

10 kΩ

At 10 lux, A0
2.50 V
analogRead()
512
Change when light doubles
123 counts
Max divider current
0.50 mA
0V1.3V2.5V3.8V5V0.11101001k10k100klight (lux, log scale)
LightLDRVoltageanalogRead
Darkness (1 lx)50.1 kΩ0.83 V170
Dusk (10 lx)10 kΩ2.50 V512
Indoor (300 lx)925 Ω4.58 V936
Daylight (5,000 lx)129 Ω4.94 V1010

Formula: R_LDR = R10 × (lux ÷ 10)−γ (max 1 MΩ). With the LDR on the supply side, V = Vsupply × R_fixed ÷ (R_fixed + R_LDR). Best sensitivity when R_fixed ≈ R_LDR at your light level. Real LDRs vary by up to 2×, so treat readings as a starting point and fine-tune thresholds on the bench.

Troubleshooting

Use a multimeter on DC volts with the black probe on GND, and measure at GPIO34.

Five checks for an LDR on an ESP32: 3V3 supply, changing voltage on GPIO34, ADC1 with Wi-Fi, no stuck readings, tuned thresholds.

Common problems and fixes

  • Always reads 0 mV

    Likely cause
    LDR not connected to 3V3, or wrong pin
    Fix
    Check the orange wire and that the midpoint goes to the pin labelled 34
  • Always reads about 3100–3300 mV

    Likely cause
    10 kΩ not connected to GND
    Fix
    Check the resistor from the GPIO34 row to GND
  • Readings stop when Wi-Fi starts

    Likely cause
    Using an ADC2 pin
    Fix
    Move to GPIO32–39 (ADC1); GPIO34 is ideal
  • Readings very noisy

    Likely cause
    ADC noise and lamp flicker
    Fix
    Average more samples; add a 100 nF capacitor from GPIO34 to GND
  • Readings differ from the Uno version

    Likely cause
    Different supply (3.3 V) and ADC
    Fix
    Normal: thresholds are in millivolts on the ESP32, not 0–1023
  • LED never lights

    Likely cause
    LED reversed, or threshold too low
    Fix
    Flip the LED and set the thresholds from your own readings

Frequently asked questions

Can I use GPIO36 (VP) or GPIO39 (VN) instead? Yes. Both are ADC1 input-only pins like GPIO34. Change LDR_PIN.

Why not use analogRead() and map 0–4095? You can, but the ESP32's raw readings aren't linear near the ends of the range. analogReadMilliVolts() applies the chip's factory calibration, so thresholds in millivolts stay accurate from one board to the next.

Can I power the LDR from 5V for a bigger range? No. GPIO34 must stay at or below 3.3 V. Use 3V3 and adjust the fixed resistor if you need more sensitivity.

Does this work on an ESP32-S3 or C3? Yes, with the same 3.3 V divider. Use one of their ADC1 pins; check your board's pinout.

How do I save battery? Measure, send, then deep sleep. Power the divider from a spare GPIO instead of 3V3, set HIGH just before you read, so it draws nothing while asleep.

What to build next

  • Wi-Fi light logger: send the light level to MQTT and graph sunrise and sunset in Home Assistant.
  • Smart porch light: switch a relay or a smart plug at dusk.
  • Light-seeking robot: two LDRs on GPIO34 and GPIO35.
  • Real lux sensor: compare your LDR with a BH1750 on I2C.
  • Pin reference: the ESP32 pinout guide lists every ADC1 and ADC2 pin.

Keep learning

Related guides & projects