Home / Circuit lab / LDR light sensor

LDR light sensor circuit simulator

A light dependent resistor changes its resistance with light. Put it in a voltage divider, feed an analogue input, and turn light into a number your code can use.

Beginner

0 V (GND)LDRLDRanalogue input

Challenges

  1. Set it up so the reading goes up when it gets darker.
  2. On the Uno, get the reading between 470 and 555 at 100 lux (within 5%), so 100 lux sits near the middle.
  3. Choose a fixed resistor that gives a change of over 60% of the supply between 10 lux and 1,000 lux.
  4. Make a dark-detector: LDR at the bottom, reading above 800 on the Uno at 1 lux and below 200 at 1,000 lux.

Controls

Readings

How an LDR works

A light dependent resistor (LDR), or photoresistor, is a small disc of cadmium sulphide with a wiggly track across it. Light frees electrons in the material, so the brighter it is, the lower its resistance. A typical GL5528-type LDR measures around 10 to 20 kΩ in a dim room, a few hundred ohms in bright daylight, and a megohm or more in the dark.

The change isn't a straight line. Each tenfold increase in light divides the resistance by roughly five. The datasheet describes this with a number called gamma, about 0.7 for this type: R = R10 × (lux ÷ 10)−γ, where R10 is the resistance at 10 lux. That's why the light slider here works on a logarithmic scale, like your eye.

Turning resistance into a voltage

A microcontroller can't measure resistance directly, but it can measure voltage. Put the LDR and a fixed resistor in series across the supply, a voltage divider, and the voltage at the middle changes with light:

Vout = Vsupply × Rbottom ÷ (Rtop + Rbottom)

With the LDR on top, more light lowers the top resistance and Vout rises. With the LDR at the bottom, it works the other way round: darkness raises Vout, which suits a night light that switches on in the dark.

Getting the biggest change

The fixed resistor sets the sensitivity. It gives the biggest swing between two light levels when it equals the geometric mean of the LDR's resistances at those levels: R = √(Rdark × Rbright). For a sensor that has to tell a dim room (10 lux, about 15 kΩ) from a bright one (1,000 lux, about 600 Ω), that's about 3 kΩ. A 10 kΩ resistor is the usual starting point because it works reasonably over the indoor range.

The ADC reading

The analogue-to-digital converter turns the voltage into a whole number. The Arduino Uno's is 10-bit: analogRead() returns 0 to 1023, measured against the 5 V supply. The Pico's is 12-bit (0 to 4095) against 3.3 V, though MicroPython's read_u16() scales it to 0 to 65535. Because the LDR and the ADC both work from the same supply, the reading doesn't change if the supply drifts a little. Our guide to how an ADC works explains resolution and reference voltages.

Using LDRs well

  • LDRs are slow: they take tens of milliseconds to respond to more light and longer to recover from it. Fine for day and night, useless for data.
  • They vary a lot between parts and with temperature, so calibrate in your real setting instead of trusting exact lux figures.
  • Add hysteresis in your code, a gap between the switch-on and switch-off readings, so a lamp doesn't flicker at dusk. The Schmitt trigger lab shows the same idea in hardware.
  • Cadmium sulphide LDRs are restricted under RoHS in many products, so commercial designs now often use a photodiode or phototransistor instead. They are still common in hobby kits.

How this simulation works

The LDR follows the gamma law above with typical GL5528-type figures: 15 kΩ at 10 lux, gamma 0.7, and no more than 1 MΩ in the dark. Real LDRs of the same type can be 8 to 20 kΩ at 10 lux, so treat the readings as typical. The resistor, supply and ADC are ideal, and the ADC reading is the voltage as a fraction of the supply times 1024 (Uno) or 4096 (Pico), rounded down.

Keep exploring