Learn Arduino

Lesson 6 of 16 · 20 min

Arduino Light Sensor: LDR Wiring and a Night Light

An LDR (light-dependent resistor) falls from about 1 MΩ in darkness to 10–20 kΩ in daylight; in a voltage divider with a fixed 10 kΩ resistor it turns light level into a 0–1023 reading on an analog pin.

This lesson also runs on an interactive 3D board: wire it, press Run and watch the circuit respond.

Open the 3D lesson

A photoresistor — an LDR, light-dependent resistor — is a resistor whose value drops as light falls on it. GL5528, the part in most kits, is around 10–20 kΩ in daylight and about 1 MΩ in a dark room. On its own that is hard to measure, so you pair it with a fixed resistor to make a voltage divider.

With the LDR on the 5 V side and 10 kΩ to ground, bright light pulls the reading up towards 1023 and darkness drops it towards 0. Pick a threshold in between — around 300 for a normally lit room — and the sketch becomes an automatic night light.

What you need

Everything below is in a standard Arduino starter kit.
PartQtyNotes
Arduino Uno R31A0 reads the divider, pin 9 drives the LED
GL5528 LDR1Any photoresistor works; note its two values
10 kΩ resistor1The fixed half of the divider
220 Ω resistor + LED1The night light
Breadboard + 5 jumpers1

How to wire it

5 V → LDR → A0, and A0 → 10 kΩ → GND. Pin 9 → 220 Ω → LED → GND. Swap the LDR and the fixed resistor if you want the reading to rise in the dark instead.

Pin-by-pin wiring for this lesson.
PinConnects toWhat it does
5VLDR (left leg)Top of the voltage divider.
A0LDR + 10 kΩThe middle of the divider: higher when brighter.
GND10 kΩBottom of the divider.
~D9220 Ω → LEDThe night light.
GNDLED (−)The LED’s return path.
Wiring map for Arduino Light Sensor: 5V to LDR (left leg), A0 to LDR + 10 kΩ, GND to 10 kΩ, ~D9 to 220 Ω → LED, GND to LED (−).
Every connection this lesson needs, drawn from the pin table above.

How an Arduino light sensor divider works

An LDR on its own gives you a resistance, and an Arduino measures voltage. The divider converts one into the other: 5 V → LDR → the analog pin, and that same pin → 10 kΩ → ground. The reading is 1023 × R_fixed ÷ (R_ldr + R_fixed), so it rises as the LDR's resistance falls — that is, as the room gets brighter.

The fixed resistor decides where the sensor is most sensitive. A GL5528 is around 10–20 kΩ at 10 lux and about 1 MΩ in the dark, so 10 kΩ puts the interesting range in the middle of the scale. A 1 kΩ resistor would leave the divider blind in dim rooms; 100 kΩ would saturate in daylight.

Turning readings into a usable threshold

Do not copy a threshold from a tutorial. A bright desk can read 800 while a dim corner reads 250, with identical code. Print the numbers in your own room, at the light level you actually care about, then put the threshold between the two states with room to spare.

A single threshold makes a night light oscillate at dusk: the LED lights the sensor, the sensor sees light, the LED goes out. Hysteresis fixes it — switch on below 280, switch off only above 340. Averaging five readings on top removes the last of the flicker.

The sketch

light.ino
const int SENSOR = A0;   // middle of the LDR divider
const int LED = 9;       // the night light
const int DARK = 300;    // below this it counts as dark

void setup() {
  Serial.begin(9600);
  pinMode(LED, OUTPUT);
}

void loop() {
  int light = analogRead(SENSOR);  // 0 = dark, 1023 = bright
  Serial.println(light);

  if (light < DARK) {
    digitalWrite(LED, HIGH);       // dark: switch the light on
  } else {
    digitalWrite(LED, LOW);
  }
  delay(100);
}

What the code does

  1. analogRead(SENSOR) reads the middle of the divider: more light, less LDR resistance, higher voltage.
  2. The threshold is a judgement call — print the numbers in your own room first, then set it between the light and dark values.
  3. digitalWrite(LED, HIGH) inside the if, LOW in the else, keeps the two states explicit.
  4. delay(100) is fast enough to feel instant and slow enough to keep the serial monitor readable.

Specifications

Typical values for the parts used here.
ItemValue
LDRGL5528 photoresistor
In daylight≈ 10–20 kΩ
In the dark≈ 1 MΩ
Divider resistor10 kΩ to GND
Response time≈ 20–30 ms

Common mistakes

Sources

The figures in this lesson come from these documents. Every value in the tables above can be checked against them.

Frequently asked questions

What is the difference between an LDR and a phototransistor?

An LDR is slow (20–30 ms) and fairly linear in resistance but drifts with temperature. A phototransistor is much faster and more sensitive, but you need to know whether it expects light or infrared.

Why does my LDR reading stay at 1023?

The divider is incomplete. You need one leg of the LDR to 5 V, the other to the analog pin, and a resistor from that same pin to ground. Without the resistor there is no current path.

Can I detect a flame or a flame-like flicker with an LDR?

Roughly. A flame gives a strong, fast flicker that an LDR can see, but an IR flame sensor is far more reliable. Use the LDR for ambient light levels, not for safety.

How do I make the night light stop flickering at dusk?

Add hysteresis: switch on below 280, switch off only above 340. The gap stops the light from triggering itself on and off at the threshold.

Want to see this circuit before you own the parts? The Arduino simulator draws this exact pin table on a 3D Uno R3: click any jumper to see what it carries, press Run and read the serial monitor.

Open the Arduino simulator

Published 12 November 2026 · last updated 19 November 2026 · written and tested by Nick Hu.