What Is an LDR? Voltage Divider Circuit and STM32 Guide
Learn how an LDR works, choose a voltage-divider resistor, read it with STM32, and avoid ADC errors with a calculated 3.3 V example.
Contents
An LDR (light-dependent resistor) is a resistor whose resistance decreases as the light on it increases. Its two leads have no polarity. To read one with a microcontroller, place it in a voltage divider and measure the midpoint with an ADC. It is useful for day/night detection and relative brightness; an uncalibrated LDR circuit does not directly measure lux.
How an LDR works, and when to use one
Light changes the conductivity of the photosensitive material. The resistance depends on illumination, wavelength, temperature and the particular component. A bare LDR has no I2C, SPI or UART interface. A module containing an LDR may also contain a comparator and expose a digital threshold output; that output is different from the resistance of the bare part.
Use an LDR for an automatic lamp, a simple light-triggered input or comparing brighter and darker conditions. For a reading in lux, consider a BH1750 light sensor. For fast optical pulses, investigate a photodiode or phototransistor with an appropriate circuit.
A voltage divider that gets brighter as voltage rises
Connect the LDR between 3.3 V and VOUT, and a fixed resistor R1 between VOUT and ground. Connect VOUT to a suitable ADC input and share ground with the microcontroller. For the STM32 example below, PA0/ADC1_IN0 applies only if that mapping is supported by the exact device and board.
VOUT = VCC × R1 / (RLDR + R1). With this arrangement, more light lowers RLDR and raises VOUT. Swapping the two resistors reverses that direction.
Calculated example: assume VCC = 3.3 V and R1 = 10 kΩ. If RLDR is 10 kΩ, VOUT is 1.65 V. If RLDR falls to 2 kΩ, VOUT becomes 2.75 V. These are assumed resistances for explaining the circuit, not measured light levels or a lux calibration.
Choose the resistor and check the ADC input
A 10 kΩ fixed resistor is a useful starting point. Measure the LDR resistance near the light level where you want the circuit to switch, then try a fixed resistor of a similar value. A larger value, such as 47 kΩ, shifts the useful range toward higher LDR resistances. It also increases source impedance and can make ADC sampling less accurate.
Check the ADC acquisition time and input range for your exact microcontroller. Increasing the sampling time, reducing source impedance or using a suitable buffer may be necessary. Do not power the divider from 5 V and assume its output is safe for a 3.3 V ADC. Start with the sensor fundamentals guide if ADC readings and sensor outputs are new to you.
Read the exact part's datasheet
| Parameter | What to check |
|---|---|
| Light resistance | Resistance at a specified illumination and light-source condition; this guides divider selection. |
| Dark resistance | The specified dark interval matters as well as the resistance limit. |
| Response time | How quickly resistance changes; a slow part can miss short pulses. |
| Spectral response | Different lamps can produce different readings even at similar perceived brightness. |
| Voltage, power and temperature | Stay within the exact component's operating limits. |
GL5528 is a common market designation, but an unidentified seller's part should not inherit specifications from another manufacturer. As a documented example of an LDR, Advanced Photonix provides an official PDV-P8103 datasheet. It is a different part, not a specification for GL5528. Some LDRs contain cadmium sulfide; consult the supplier's material declarations for the intended product and market.
STM32 HAL example with error handling
Configure the exact MCU, ADC pin, resolution and sampling time in CubeMX first. This is an integration snippet using hadc1, not a complete project or a claim of hardware testing. It returns a new average only when conversion succeeds. Any threshold must be established for your circuit and light conditions.
#define LDR_SAMPLES 16U
/* Snippet: provide hadc1 configured by CubeMX for your exact STM32. */
static HAL_StatusTypeDef ldr_read_adc_average(uint32_t *out)
{
if (out == NULL) return HAL_ERROR;
uint32_t sum = 0;
for (uint32_t i = 0; i < LDR_SAMPLES; ++i) {
HAL_StatusTypeDef status = HAL_ADC_Start(&hadc1);
if (status != HAL_OK) return status;
status = HAL_ADC_PollForConversion(&hadc1, 20);
if (status != HAL_OK) { HAL_ADC_Stop(&hadc1); return status; }
sum += HAL_ADC_GetValue(&hadc1);
status = HAL_ADC_Stop(&hadc1);
if (status != HAL_OK) return status;
}
*out = sum / LDR_SAMPLES;
return HAL_OK;
}
/* Usage: uint32_t value;
if (ldr_read_adc_average(&value) == HAL_OK) { use valid value; }
else { report ADC error; do not show stale/zero data as a new sample; } */
Download the existing STM32 integration snippet and guide (Persian documentation).
Check the circuit before choosing a threshold
- With power disconnected, measure the LDR resistance in darkness, room light and under a repeatable light source.
- Build the divider, then compare VOUT measured by a multimeter with the ADC result.
- Try a second fixed resistor and record where the output approaches either supply rail.
- Check whether a nearby indicator LED or lamp is illuminating the sensor itself.
- If a lamp flickers near the threshold, use separate turn-on and turn-off thresholds (hysteresis) rather than relying on a single boundary.
These are proposed checks, not reported LCENJ measurements. Noise filtering can help an unstable reading, but it cannot recover a saturated signal or turn an uncalibrated divider into a lux meter. Continue with sensor filtering or the light sensor learning path.
Sources and scope
- Advanced Photonix: PDV-P8103 product and datasheet — documented LDR behavior; not identification of the photographed or locally supplied part.
- STMicroelectronics AN2834 — ADC accuracy, source impedance and acquisition time.
The divider values above are teaching assumptions. The photo and connection diagram do not demonstrate a tested assembly. No new personal testing or measured performance is claimed.
Question and answer article
Ask your technical question
Questions and answers are published after moderation. Authors can see their own pending drafts.
No confirmed questions have been registered for this article yet.