মূল বিষয়বস্তুতে যান
Learn/Sensing the world/Light sensor (LDR)
Sensing the world · Lesson
10 min read

Light sensor (LDR)

Turn changing resistance into a voltage with a divider, and build a night light.

What You Will Learn

  • Build a voltage divider with an LDR
  • Choose a threshold
  • Make decisions from sensor data

An LDR (light-dependent resistor) has high resistance in the dark (hundreds of kΩ) and low resistance in bright light (a few hundred Ω). The Arduino can't measure resistance directly, so we pair it with a fixed 10 kΩ resistor to make a voltage divider.

With the LDR from 5 V to A0 and 10 kΩ from A0 to GND, more light means a higher reading.

Automatic night lightarduino-uno
Open in Lab
UNOATmega328P: The 8-bit AVR microcontroller that runs your sketch (16 MHz, 32 KB flash, 2 KB RAM).ATmega328PUSB-B: USB connection for uploading sketches and for the Serial Monitor. Also powers the board (5 V).DC jack: Barrel jack for 7–12 V from a battery pack or adapter (centre positive).ATmega16U2: Converts USB to serial for uploads and Serial.print().RESET: Restarts your sketch from the beginning (setup() runs again).L (D13): Built-in LED connected to pin 13 (LED_BUILTIN).ON: Power indicator.5 V regulator: Turns 7–12 V from VIN/jack into a steady 5 V.16 MHz: Clock source: the microcontroller runs 16 million cycles per second.ICSP: In-circuit programming header (SPI). Used to burn a bootloader; not needed for normal uploads.400 lx10 kΩ220 Ω+
Note: Lower the Light slider until the lamp turns on. Adjust DARK_BELOW in the code to change the switching point.
Add hysteresis (two thresholds, e.g. on below 380, off above 420) so the lamp doesn't flicker at dusk.

Check Your Understanding

Test what you learned in this lesson

1Why do we add a fixed resistor to an LDR?