Skip to content
RoboLearn
Not completed

Light sensors (LDR)

What you’ll learn

  • How an LDR (light-dependent resistor) works
  • Why it needs a voltage divider
  • How to calibrate a light sensor and use a threshold

The LDR

An LDR’s resistance changes with light: high in the dark (hundreds of kΩ or more) and low in bright light (around 1 kΩ or less). The Arduino can’t read resistance directly, only voltage.

The voltage divider

Put the LDR in series with a fixed resistor (often 10 kΩ) and read the voltage in the middle:

5V ── LDR ──┬── 10 kΩ ── GND
│
A0

With the LDR on top, more light → lower LDR resistance → higher voltage on A0 → bigger number. Swap the two parts and it works the other way round. Many LDR modules contain this divider already and give an analog output (AO), sometimes plus a digital output (DO) with a threshold you set with a small screw.

Calibrate, then threshold

Light levels differ in every room, so don’t guess numbers:

  1. Print the reading in the dark (cover the sensor) and in normal light.
  2. Pick a threshold halfway between them.
  3. Use it: if (light < threshold) { ... }.

Finished this lesson?