মূল বিষয়বস্তুতে যান
Back to Projects
BeginnerArduino Uno R3
20 minutes build

Automatic night light

An LDR divider senses darkness and switches a lamp on.

Interactive Simulation

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 Ω+

Required Components Checklist

Assembly Steps

1

Build the divider

LDR from 5V to A0, 10 kΩ from A0 to GND.

2

Find your threshold

Print the readings in normal light and with your hand over the sensor, then pick a value in between.

3

Switch the lamp

Compare the reading with the threshold and set the LED.

Arduino Sketch

const int LIGHT_PIN = A0;
const int LED_PIN = 9;
const int DARK_BELOW = 400; // tune this: lower = darker before the light turns on

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

void loop() {
  int light = analogRead(LIGHT_PIN); // more light → lower LDR resistance → higher reading
  bool dark = light < DARK_BELOW;
  digitalWrite(LED_PIN, dark ? HIGH : LOW);
  Serial.print("light=");
  Serial.print(light);
  Serial.println(dark ? "  -> lamp ON" : "  -> lamp off");
  delay(300);
}

Challenges & Extensions

Ready to take this project further? Try these challenges:

  • •Add hysteresis so it doesn't flicker at dusk.
  • •Fade the LED brighter as it gets darker using analogWrite.
  • •Keep the lamp on for 10 s after it becomes light again.