Build a Stable LDR Light Switch: Divider Choice, Calibration and Hysteresis
A light-dependent resistor makes an approachable first sensor: shine a light on it and its resistance changes. Turning that change into a dependable automatic light switch takes a little more thought. Without suitable thresholds, a lamp can chatter on and off as the room hovers near the chosen brightness.
This guide develops a low-voltage light detector for a model, display or classroom project. It focuses on a stable light/dark decision rather than claiming that an uncalibrated LDR provides an accurate lux measurement.
Choose an LDR for the conditions you want to distinguish
Photoresistors generally decrease in resistance as illumination increases. Different types cover different resistance ranges, so select one whose behaviour is useful near your intended switching point. A part optimised for bright conditions may give little useful change in a dim enclosure.
PMD Way's LDR collection includes selectable light-dependent resistor packs. Check the chosen variant and its supporting specifications. The resistance quoted for a light level is a measurement condition, not a fixed resistance like that of an ordinary resistor.
As a manufacturer example, the Senba GL5528 manual, hosted by Marutsu describes resistance under defined illumination and a spectral response. This illustrates the information to seek; it does not identify every variant in the PMD listing as that particular device.
Convert resistance into a voltage
A microcontroller ADC measures voltage, so pair the LDR with a fixed resistor as a divider. In one useful arrangement, connect the LDR between a suitable supply and the sensing node, then connect the fixed resistor from that node to ground. The sensing node connects to the analogue input.
For this arrangement:
Sensor voltage = supply voltage × fixed resistance ÷ (LDR resistance + fixed resistance).
The voltage rises as the LDR becomes brighter and its resistance falls. Reversing the divider positions reverses that relationship. Either arrangement can work, but the software's comparison must match the wiring.
Use a supply and ADC reference arrangement compatible with the board. A circuit copied from a 5 V example must not send 5 V into a board whose analogue input permits only a lower voltage. Check the board's input limits, reference selection and recommended source impedance.
Pick the fixed resistor around the useful light level
A practical starting point is a fixed resistance of roughly the same order as the LDR resistance near the desired threshold. This keeps the divider away from an extreme and gives useful voltage movement around the decision point.
For a hypothetical 3.3 V divider with a 10 kΩ fixed resistor, an LDR measuring 10 kΩ produces 1.65 V. If that LDR changes to 30 kΩ, the node becomes about 0.825 V. These are arithmetic examples using assumed resistances, not a prediction of a particular light level.
Measure the actual divider in the intended bright and dark conditions. If both readings sit close to one rail, try a better-matched fixed resistor after checking the LDR range. Also check divider current and component dissipation; a sensing circuit should not needlessly heat its sensor.
Calibrate in the final position
The enclosure window, sensor direction and nearby objects all affect the reading. Record values with the LDR mounted where it will operate. Test normal lighting, the desired changeover condition and clearly dark conditions.
Arduino's sensor calibration example demonstrates recording minimum and maximum readings. For a permanent installation, an explicit calibration step is often more predictable than assuming every startup will include the full range of expected light.
Do not label a simple minimum-to-maximum mapping as lux. LDR resistance is not generally linear with illumination, and its spectral response means different light sources can produce different readings for the same nominal illuminance. A relative scale is perfectly useful if you name it honestly, such as light level or brightness index.
Add two thresholds instead of one
Suppose your brighter-is-higher reading moves around a single threshold of 400. Small noise or a passing shadow could repeatedly reverse the output. Hysteresis introduces a gap between the turn-on and turn-off decisions.
For an illustrative dark-activated lamp, turn it on when the reading falls below 350. Once on, keep it on until the reading rises above 450. Between those values, preserve the previous state. The numbers must be chosen from your measurements and ADC scale; they are not universal Arduino settings.
| Current state | Reading | Action |
|---|---|---|
| Off | Below lower threshold | Turn on |
| On | Above upper threshold | Turn off |
| Either | Between thresholds | Keep current state |
The same principle can be implemented in hardware using a comparator with positive feedback. Texas Instruments' comparator hysteresis circuit explains the role of separate transition thresholds for noisy or slowly changing signals.
Use time filtering for brief disturbances
Hysteresis handles small changes around a threshold. A persistence timer handles a different problem: a brief event that crosses the threshold decisively. For example, require the dark condition to remain true for an intentionally chosen interval before changing the lamp state.
Choose that interval for the application. A demonstration may need an immediate response; an outdoor-style day/night model may benefit from ignoring a short shadow. Avoid an excessively long average that makes the detector seem broken when someone intentionally changes the light.
Keep sampling and control timing explicit. If your main loop sometimes pauses for other work, a count of samples may not represent a constant duration. Use elapsed time when the requirement is expressed in seconds.
Stop the lamp from fooling its own sensor
A common installation fault is optical feedback. The LDR sees darkness, switches the lamp on, then sees the lamp and switches it off. Hysteresis may reduce the effect but cannot always compensate for a poorly chosen sensor position.
Aim or shield the LDR so it measures the intended ambient light. Test with the controlled lamp both on and off. If the lamp changes the sensor reading enough to cross both thresholds, fix the placement before widening the gap indefinitely.
Verify startup and faults
Define the initial output state and wait for a valid sensor reading before acting. Test a disconnected sensor and an accidental short under controlled conditions, since both may resemble extreme light levels. Decide how the application should respond.
Drive a lamp or relay through circuitry rated for its load; the sensing divider is not a power driver. Finish by recording the divider value, installed bright/dark readings, thresholds and delay. Those settings turn a responsive demonstration into a repeatable light-control system.