Beginner
2 hours
Arduino Automatic Night Light with LDR
This project uses a light-dependent resistor (LDR) — a component whose resistance decreases as light increases — to sense ambient brightness. The Arduino reads the LDR’s value via an analogue pin and switches an LED (or a relay to control a mains lamp) on when it gets dark. It’s a simple but practical introduction to analogue sensors.
What You’ll Need
- Arduino Uno or Nano
- LDR (light-dependent resistor / photoresistor)
- 10 kΩ resistor (forms a voltage divider with the LDR)
- White or warm LED + 220 Ω resistor
- Breadboard and jumper wires
- USB cable or 9 V battery with barrel connector
Step-by-Step Instructions
- Build the voltage divider: Connect one leg of the LDR to 5 V. Connect the other LDR leg to both Arduino A0 and one end of the 10 kΩ resistor. Connect the other end of the 10 kΩ resistor to GND. This forms a voltage divider whose midpoint voltage changes with light.
- Wire the LED: Connect Arduino pin 9 through a 220 Ω resistor to the LED anode. LED cathode to GND.
- Write the sketch:
int ldrPin = A0; int ledPin = 9; int threshold = 400; // adjust based on your lighting void setup() { pinMode(ledPin, OUTPUT); } void loop() { int ldrVal = analogRead(ldrPin); if (ldrVal < threshold) digitalWrite(ledPin, HIGH); else digitalWrite(ledPin, LOW); delay(100); } - Calibrate: Upload, open Serial Monitor, and add
Serial.println(ldrVal);to see real values. Note the value in normal light and in darkness. Set threshold between them. - Test: Cover the LDR with your hand — the LED should turn on. Uncover — it should turn off.
Safety Notes
- If extending this to control a mains lamp via a relay module, use a relay rated for your mains voltage and treat mains wiring with full safety precautions — if unsure, ask an electrician.
Tips & Troubleshooting
- If the LED flickers near the threshold, add a small hysteresis band in code: turn ON below threshold-20, turn OFF above threshold+20.
- Use a brighter LED or add more LEDs in parallel (each with own resistor) for a useful bedside night light.
- A 5 V relay module can replace the LED to switch a real lamp — keep the Arduino circuit separate from mains voltage.