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.
Run the LDR (Photoresistor) in your browser
Ready-wired circuit with code. No hardware, no install.
LDR (Photoresistor) at a glance
LDR at a glance. Resistance falls as light rises: about 10–20 kΩ at dim-room light (10 lux), around 1 MΩ in darkness. Read it with a voltage divider (LDR plus a fixed resistor, usually 10 kΩ) on an analog pin. It is non-linear and slow (tens of milliseconds) and varies a lot from part to part, so it's great for "dark or light?" decisions and poor for accurate lux measurement.
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.
How an LDR works
The orange zig-zag on the face of an LDR is a thin track of cadmium sulphide (CdS), a semiconductor. In the dark it has very few free electrons, so it conducts poorly and its resistance is high. When light hits it, the photons knock electrons free. More light means more free charge carriers and a lower resistance. Two metal comb-shaped electrodes either side of the track collect the current.
The track zig-zags to make it long and narrow across a small disc, which gives the large, easy-to-measure resistance change. The ceramic disc is coated in clear epoxy to protect it from moisture.
The relationship isn't a straight line. Each time the light gets about ten times brighter, the resistance drops to roughly a fifth. On a graph with logarithmic axes, that becomes a straight line whose slope is called gamma (about 0.7 for the GL5528).
What resistance to expect
| Situation | Approx. light | Typical GL5528 resistance |
|---|---|---|
| Dark room, moonlight | 0.1–1 lux | 100 kΩ – 1 MΩ |
| Dusk, dim room | 10 lux | 8–20 kΩ |
| Living room lamp | 50–150 lux | 2–5 kΩ |
| Office or bright room | 300–500 lux | 1–2 kΩ |
| Overcast daylight | 1,000–10,000 lux | 100–500 Ω |
Ballpark values from the GL5528 curve. Your parts may read up to about 2× higher or lower; that is normal for CdS cells.
Reading an LDR: the voltage divider
A microcontroller measures voltage, not resistance. To turn the LDR's resistance into a voltage, put it in series with a fixed resistor across the supply and connect the point between them to an analog input. This is a voltage divider:
Vout = Vsupply × R_fixed ÷ (R_fixed + R_LDR)
With the LDR on the supply side and the fixed resistor on the ground side, more light means a lower LDR resistance and a higher voltage, so the reading rises as it gets brighter. That is the arrangement used in our board guides. Swap the two parts and the reading falls as it gets brighter instead.
Worked example (5 V, 10 kΩ fixed resistor):
- In a bright room the LDR is about 925 Ω: Vout = 5 × 10,000 ÷ 10,925 = 4.58 V, and an Uno reads about 937.
- Almost dark, the LDR is about 31 kΩ: Vout = 5 × 10,000 ÷ 40,900 = 1.22 V, and an Uno reads about 250.
Choosing the fixed resistor
A 10 kΩ resistor is the usual choice, and for indoor projects it's a good one. But the divider is most sensitive where the LDR's resistance is close to the fixed resistor's value. That's where a small change in light gives the biggest change in voltage. So choose the fixed resistor based on the light level where you need to make your decision.
A practical method: measure your LDR's resistance with a multimeter at the light level where you want the switch to happen, then pick the nearest standard resistor.
Which fixed resistor?
Darkness: night lights, "lid closed"
- Choose R_fixed
- 47–100 kΩ
- Why
- Spreads out the low-light end so dusk and night are easy to tell apart
Indoor light levels
- Choose R_fixed
- 10 kΩ
- Why
- Good spread from dim to bright rooms
Bright light or sunlight: solar trackers
- Choose R_fixed
- 1–2.2 kΩ
- Why
- Keeps bright readings from bunching up near the supply voltage
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.
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.
Tip
Bare LDR or LDR module? Modules (usually with an LM393 comparator, a trimmer and 3 or 4 pins) add a DO digital output that switches at a threshold you set with the trimmer, plus an AO analog output. AO works like the divider above, but on most modules the reading *falls* as the light rises. The bare LDR used in our guides teaches the principle, costs less and lets you choose the resistor. Both read the same way in code.
Things to know before you rely on an LDR
- It's slow. The GL5528 takes about 20 ms to respond to light and around 30 ms to recover, and much longer to settle fully after a long time in the dark. That's fine for day and night, but too slow for reading fast flashes or data.
- It's non-linear and varies between parts. Two LDRs from the same bag can differ by a factor of two. Calibrate thresholds with your part in your light, rather than copying numbers.
- Mains lighting flickers. Lamps flicker at twice the mains frequency (100 Hz or 120 Hz). Average several readings to get a steady value.
- Temperature matters a little. The resistance shifts somewhat with temperature, so a sensor in direct sun reads differently as it heats up.
- It contains cadmium. CdS cells are restricted under RoHS in many regions for new commercial products. For hobby use that's fine; for a product, consider a phototransistor or a digital light sensor.
When you need real lux values or fast, consistent readings, use a digital sensor such as the BH1750 or TSL2561. They are calibrated at the factory and talk I2C.
LDR compared with other light sensors
LDR (GL5528)
- Output
- Resistance (analog)
- Accuracy
- Low; varies per part
- Speed
- Slow (~20–30 ms)
- Best for
- Night lights, dark/light switching, toys
Phototransistor
- Output
- Current (analog)
- Accuracy
- Medium
- Speed
- Fast (µs)
- Best for
- Beam breaks, fast light pulses
- Output
- Lux over I2C
- Accuracy
- Good, calibrated
- Speed
- 120 ms per reading
- Best for
- Real lux values, smart lighting
- Output
- Lux over I2C
- Accuracy
- Good, with IR compensation
- Speed
- 13–400 ms
- Best for
- Mixed or infrared-rich lighting
What you can build
- Automatic night light: an LED that turns on at dusk, as in both board guides.
- Dusk-to-dawn switch: a relay module for a porch light, with hysteresis so it doesn't flicker at sunset.
- Light-seeking robot: two LDRs, one per side, steer toward the brighter one.
- Laser tripwire or beam alarm: a laser on an LDR; the reading drops when someone breaks the beam.
- Screen or LED auto-brightness: dim a display in a dark room.
- Single-axis solar tracker: two LDRs either side of a divider plate keep a panel facing the sun.
Ready to build?
- LDR with Arduino Uno: divider on A0, an automatic night light with hysteresis, and a rough lux estimate.
- LDR with ESP32: 3.3 V divider on GPIO34 (ADC1, safe with Wi-Fi), calibrated millivolt readings, and a night light.
Frequently asked questions
Does an LDR have polarity? No. It's a resistor, so either leg can go either way.
Can I connect an LDR directly to an analog pin? Not on its own. The pin measures voltage, so you need the fixed resistor to form a divider. Without it, the pin floats and reads random values.
Why does my reading go down when it gets brighter? The LDR is on the ground side of the divider (or you're using a module whose AO works that way). Swap the LDR and resistor, or invert the value in code.
What resistor should I use with an LDR? 10 kΩ suits most indoor projects. Use a higher value (47–100 kΩ) to detect darkness well, or a lower one (1–2.2 kΩ) for bright light. The best value is close to the LDR's resistance at the light level you care about.
Can an LDR measure lux? Only roughly. You can estimate lux from the GL5528 curve, but expect errors of about ±50 % without calibrating each part. For real lux, use a BH1750.
Does it work through glass or plastic? Yes, through anything transparent to visible light. Tinted or frosted covers reduce the reading, so set your thresholds with the cover in place.
Why does my LDR react slowly after being in the dark? CdS cells have a "light history" effect: after a long time in the dark they take a while to settle. Give the reading a few seconds before you act on it.
Wiring & code
Board integration
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.

