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
| Pin | Name | Connection |
|---|---|---|
| Leg 1 | Terminal | To the supply (5 V on an Uno, 3.3 V on an ESP32) in the usual divider |
| Leg 2 | Terminal | To the analog pin and, through the fixed resistor, to GND |
| — | Divider midpoint | Not 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 Arduino Uno
In this guide you will read a light-dependent resistor (LDR) with an Arduino Uno and build an automatic night light: an LED that switches on at dusk and off again when the room is bright. Along the way you'll see the raw light readings in the Serial Monitor, learn why the code uses two thresholds instead of one, and estimate the light level in lux.
It takes about 15 minutes and needs no libraries. If you don't have the parts yet, open the ready-made circuit in the SolderHub Simulator, change the light level on the LDR, and watch the readings.
What you need
Arduino Uno R3 (or compatible)
- Qty
- 1
- Notes
- Any 5 V Uno
LDR (GL5528 or similar, 5 mm)
- Qty
- 1
- Notes
- Any common CdS photoresistor works
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. Limits the LED current
Breadboard and jumper wires
- Qty
- 1 + 6
USB cable
- Qty
- 1
How the circuit works
The Uno's analog pins measure voltage, from 0 to 5 V, as a number from 0 to 1023. An LDR changes resistance, not voltage, so we pair it with a 10 kΩ resistor to form a voltage divider:
- 5V → LDR → A0 → 10 kΩ → GND
In bright light the LDR's resistance is low, so A0 sits close to 5 V and reads high (around 900+). In the dark the LDR's resistance is high, so A0 drops toward 0 V and reads low. The LED on pin 9 is the night light. Pin 9 can also do PWM, so you could dim the light later.
Wiring: LDR to Arduino Uno
| From | To | Wire colour | Why |
|---|---|---|---|
| Uno 5V | LDR leg 1 | Red | Top of the divider |
| LDR leg 2 | Uno A0 | Green | The divider midpoint: the voltage you measure |
| 10 kΩ resistor | Between A0 row and GND | Black (to GND) | Bottom of the divider |
| Uno D9 | 220 Ω → LED long leg | Blue | Night-light output |
| LED short leg | Uno GND | Black | Completes the LED circuit |
Wire it up, step by step
- 1Unplug the Uno from USB.
- 2Push the LDR into the breadboard so its two legs are in different rows. It has no polarity.
- 3Run a red wire from the Uno's 5V pin to the row with the first LDR leg.
- 4Put the 10 kΩ resistor from the row with the second LDR leg to an empty row, and connect that row to GND with a black wire.
- 5Run a green wire from the second LDR leg's row (the midpoint) to A0.
- 6Put the LED in the breadboard: long leg (anode) in one row, short leg (cathode) in another.
- 7Connect D9 to the 220 Ω resistor, and the resistor's other end to the LED's long leg. Connect the LED's short leg to GND.
- 8Check that the LDR and the 10 kΩ share exactly one row (the midpoint), and that the midpoint goes to A0.
Upload the night-light sketch
No library is needed. In Arduino IDE 2, choose Tools → Board → Arduino Uno, select the port, paste this sketch into a new file and click Upload:
// LDR (GL5528) + Arduino Uno: light meter and automatic night light
// Divider: 5V -> LDR -> A0 -> 10 kΩ -> GND (brighter = higher reading)
const byte LDR_PIN = A0;
const byte LED_PIN = 9; // D9 -> 220 Ω -> LED -> GND
const int ON_BELOW = 500; // about 10 lux (dusk): switch the light on
const int OFF_ABOVE = 600; // a little brighter: switch it off again
bool lightOn = false;
void setup() {
pinMode(LED_PIN, OUTPUT);
Serial.begin(9600);
Serial.println(F("LDR night light ready"));
}
void loop() {
// Average 8 readings to smooth out flicker from mains-powered lamps
long sum = 0;
for (int i = 0; i < 8; i++) {
sum += analogRead(LDR_PIN);
delay(2);
}
int level = sum / 8; // 0 = dark ... 1023 = very bright
// Two thresholds (hysteresis) stop the LED flickering at dusk
if (!lightOn && level < ON_BELOW) lightOn = true;
if (lightOn && level > OFF_ABOVE) lightOn = false;
digitalWrite(LED_PIN, lightOn ? HIGH : LOW);
Serial.print(F("Light level: "));
Serial.print(level);
Serial.print(F(" Night light: "));
Serial.println(lightOn ? F("ON") : F("OFF"));
delay(250);
}How the code works
- Averaging. The loop takes 8 readings 2 ms apart and averages them. Mains-powered lamps flicker at 100 or 120 Hz, which makes single LDR readings jump around. Averaging smooths it out.
levelis 0 (dark) to 1023 (very bright), because the LDR sits on the 5 V side of the divider.- Two thresholds. The light switches on below 500 and off above 600. With a single threshold, a reading hovering around dusk would flick the LED on and off. The gap between the two numbers is called hysteresis, the same trick a thermostat uses.
lightOnremembers the current state, so the LED only changes when a threshold is actually crossed.
The thresholds of 500 and 600 correspond to roughly 10–17 lux with a typical GL5528 and a 10 kΩ resistor, which is about dusk. Every LDR is different, so tune them as described below.
What you should see
Open Tools → Serial Monitor at 9600 baud. In a normal room you'll see high numbers and the night light off. Cover the LDR with your hand:
LDR night light ready
Light level: 912 Night light: OFF
Light level: 905 Night light: OFF
Light level: 214 Night light: ON
Light level: 198 Night light: ON
Light level: 887 Night light: OFFTune it for your room
- Upload the sketch and open the Serial Monitor.
- At the light level where you want the night light to come on (dusk, or a lamp switched off), note the reading. Say it's 430.
- Set
ON_BELOWto that value andOFF_ABOVEabout 100 higher, for example 430 and 530. - Upload again and test by slowly shading the LDR.
If the readings bunch up near the bottom in dim light, use a bigger fixed resistor (47 kΩ) to spread out the dark end. The main LDR page explains how to choose it.
Calculator
Open full page →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
| Light | LDR | Voltage | analogRead |
|---|---|---|---|
| Darkness (1 lx) | 50.1 kΩ | 0.83 V | 170 |
| Dusk (10 lx) | 10 kΩ | 2.50 V | 512 |
| Indoor (300 lx) | 925 Ω | 4.58 V | 936 |
| Daylight (5,000 lx) | 129 Ω | 4.94 V | 1010 |
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.
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 (Dark, Dusk, Room, Daylight), press Run, and open the Serial Monitor to watch the reading fall and the night light switch on.
Try this circuit in the SolderHub Simulator
Run this exact circuit in your browserReady-wired, code loadedConfirm simulator behavior against the real wiring and the sensor datasheet before connecting hardware.
Level up: estimate the light in lux
You can work backwards from the reading to the LDR's resistance, then use the GL5528's curve to estimate lux. It won't be precise, because LDRs vary, but it's a useful way to see the numbers behind the sensor.
The divider formula rearranges to R_LDR = R_fixed × (1023 ÷ reading − 1), and the GL5528 curve gives lux ≈ 10 × (R_LDR ÷ R10)^(−1/0.7), where R10 is the LDR's resistance at 10 lux.
// Estimate lux from a GL5528 LDR on an Arduino Uno (rough, about ±50 %)
const byte LDR_PIN = A0;
const float R_FIXED = 10000.0; // the 10 kΩ resistor from A0 to GND
const float R10 = 15000.0; // LDR resistance at 10 lux (GL5528: 8-20 kΩ)
const float GAMMA = 0.7; // slope of the GL5528 curve
void setup() {
Serial.begin(9600);
}
void loop() {
int adc = analogRead(LDR_PIN);
if (adc < 1) adc = 1; // avoid dividing by zero
float rLdr = R_FIXED * (1023.0 / adc - 1.0); // divider maths
float lux = 10.0 * pow(rLdr / R10, -1.0 / GAMMA);
Serial.print(F("ADC: "));
Serial.print(adc);
Serial.print(F(" LDR: "));
Serial.print(rLdr / 1000.0, 1);
Serial.print(F(" kΩ ≈ "));
Serial.print(lux, 0);
Serial.println(F(" lux"));
delay(500);
}For better accuracy, measure your own LDR at a known light level (a phone light-meter app gives a rough reference) and set R10 to match. Expect about ±50 % without calibration. If you need real lux, use a BH1750.
Troubleshooting
Use a multimeter on DC volts with the black probe on GND, and measure at A0.
Common problems and fixes
Reading stuck at 0
- Likely cause
- LDR not connected to 5V, or the midpoint not on A0
- Fix
- Check the red wire to the LDR and the green wire to A0
Reading stuck at 1023
- Likely cause
- 10 kΩ resistor missing or not connected to GND
- Fix
- Make sure the resistor goes from the A0 row to GND
Reading falls when it gets brighter
- Likely cause
- LDR and resistor swapped
- Fix
- Put the LDR on the 5V side, or use
1023 - analogRead(A0)
Numbers jump by 20–50
- Likely cause
- Lamp flicker or loose breadboard contacts
- Fix
- Average more readings and press the parts firmly into the breadboard
LED flickers at dusk
- Likely cause
- Thresholds too close together
- Fix
- Increase the gap between ON_BELOW and OFF_ABOVE
LED never lights
- Likely cause
- LED reversed, or ON_BELOW too low for your room
- Fix
- Flip the LED (long leg to the resistor) and tune the thresholds from your readings
Readings all bunched at the low end
- Likely cause
- Fixed resistor too small for dim light
- Fix
- Try 47 kΩ or 100 kΩ instead of 10 kΩ
Frequently asked questions
Can I use a different analog pin?
Yes. Any of A0 to A5 works. Change LDR_PIN in the sketch.
Can the night light fade in instead of switching?
Yes. Pin 9 supports PWM. Replace digitalWrite with analogWrite(LED_PIN, brightness), where brightness goes from 0 to 255. For example, map(level, 0, 1023, 255, 0) makes the LED brighter as the room gets darker.
Can I switch a real lamp? Use a relay module on the output pin instead of the LED, and follow mains-safety rules. Keep the hysteresis so the relay doesn't chatter at dusk.
Will this work on an Arduino Nano or Mega? Yes. The pins and code are the same.
Why 5 V for the divider? The Uno's analog reference is 5 V by default, so a 5 V divider uses the full 0–1023 range.
What to build next
- Dusk-to-dawn porch light: drive a relay module from D9.
- Light-seeking robot: two LDRs on A0 and A1; steer toward the brighter side.
- Laser tripwire: shine a laser pointer on the LDR and sound a buzzer when the beam is broken.
- Real lux meter: upgrade to a BH1750 and compare it with your LDR.









