All projects
🌙🟢 Beginner 40 min

Smart Night Light

A light that switches itself on when the room gets dark — your first project that senses the world.

What You'll Build

This project turns your Arduino into something genuinely useful: a lamp that knows when it is dark. A light sensor watches the room, and the moment the brightness drops below a level you choose, the LED fades on. When someone opens the curtains again, it quietly switches off.

What You Need

  • Arduino Uno
  • LDR (photoresistor)
  • 10kΩ resistor
  • White LED
  • 220Ω resistor
  • Breadboard

How It Works

A photoresistor (LDR) changes its resistance depending on how much light hits it: bright light means low resistance, darkness means high resistance. Paired with a fixed 10kΩ resistor it forms a voltage divider, and the Arduino's analog pin reads that voltage as a number from 0 to 1023. Your code compares that number to a threshold and decides whether the lamp should be on.

Wiring

  1. 1One leg of the LDR to 5V.
  2. 2Other leg of the LDR to A0 and, through the 10kΩ resistor, to GND.
  3. 3LED long leg through a 220Ω resistor to pin 9, short leg to GND.

Step-by-Step Instructions

  1. 1

    Build the sensor divider

    The LDR and the 10kΩ resistor sit in a line between 5V and GND. The wire that reads the value comes from the point exactly between them — that middle point is what A0 measures.

  2. 2

    Add the lamp

    Wire the white LED to pin 9 through its 220Ω resistor. Pin 9 supports PWM, which means later you can fade the light instead of just switching it.

  3. 3

    Read the sensor first

    Upload the code and open the Serial Monitor at 9600 baud. Cover the LDR with your hand and watch the number change — that is your sensor talking to you.

  4. 4

    Pick your threshold

    Write down the reading in a bright room and the reading with your hand over the sensor. Choose a number halfway between them and put it in the code.

  5. 5

    Test in the dark

    Turn the room lights off. Your lamp should glow on smoothly within a second.

Arduino Code

smart-night-light.ino
const int LDR_PIN = A0;
const int LAMP = 9;
const int DARK_LEVEL = 400;   // change to suit your room

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

void loop() {
  int light = analogRead(LDR_PIN);
  Serial.println(light);

  if (light < DARK_LEVEL) {
    analogWrite(LAMP, 200);   // soft glow
  } else {
    analogWrite(LAMP, 0);     // off
  }
  delay(200);
}

Understanding the Code

analogRead(LDR_PIN)

Measures the voltage on A0 and gives back a whole number from 0 (0V) to 1023 (5V).

Serial.println(light)

Sends the value to your computer so you can watch it in the Serial Monitor. This is the easiest way to debug a sensor.

if (light < DARK_LEVEL)

A decision. If the reading is smaller than your threshold, the room is dark.

analogWrite(LAMP, 200)

PWM brightness from 0 to 255. 200 is bright but gentle on the eyes at night.

Challenges & Upgrades

Fade the lamp in slowly over two seconds instead of snapping on.
Add a second LED that shows the current light level as a bar.
Make the threshold adjustable with a potentiometer.

Troubleshooting

😖 Reading never changes

Fix: Your divider is probably wired wrong. A0 must connect to the point between the LDR and the resistor.

😖 Lamp flickers near the threshold

Fix: Add a small gap: turn on below 380 and off above 450. That gap is called hysteresis.

Up next

📡 Ultrasonic Radar

Build This