Projects / Automatic fading night light

Automatic fading night light

Beginner2 hGuided buildReviewed

Build it step by step

0 of 8 done
  1. NANO11.0 mA

    Cut a short strip

    About 50 cm at the marked cut line, then check its watts per metre.

  2. NANO323 lux

    Wire the LDR divider

    LDR from 5V to A0, 10 kilohm from A0 to GND.

  3. NANOIRLZ44

    Wire the MOSFET

    Gate to D9 through 220 ohm, 10 kilohm gate to GND, source to common GND.

  4. NANO1211.1 mA

    Wire the strip

    Plus through the fuse to the adapter, minus to the drain.

  5. NANO11.1 mA

    Upload with USB only

    Strip stays off in a bright room.

  6. NANO1211.1 mA

    Plug in the 12 V adapter

    Check with a multimeter that no wire gets warm.

  7. NANO327 lux

    Cover the LDR

    The strip fades up slowly.

  8. NANO11.1 mA

    Press the button

    The strip fades out and stays off, press again to resume.

In short

How do you build an automatic fading night light?

A light sensor fades a warm LED strip brighter as the room gets darker, never switching on harshly. You need Arduino Nano, LDR Photoresistor, Resistor 10 kΩ × 2, Resistor 220 Ω, MOSFET IRLZ44N, 12 V LED strip warm white (5 m, 2835), 12 V 2 A DC adapter, Fuse 1 A glass (5 × 20 mm), Tactile push button 6 mm, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm). It takes 8 steps and about 120 minutes.

Level
Beginner
Time
About 120 min
Parts
11

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
LDR between 5V and A0, 10 kilohm from A0 to GNDNano A0Brighter room gives a higher reading
Nano D9 through 220 ohmMOSFET gatePWM brightness control
10 kilohm from gate to GNDMOSFET gateKeeps the strip off while the Nano starts
MOSFET sourceGND of both the Nano and the 12 V adapterCommon ground is essential
LED strip minusMOSFET drainLow-side switching
LED strip plus12 V adapter plus through a 1 A fuseUse a cut piece of about 50 cm so current stays under 1 A
Override buttonNano D2 to GNDInternal pull-up, toggles auto and off

Code

The LDR and 10 kilohm resistor form a voltage divider, so the A0 reading rises with light. The sketch smooths it with a slow filter so a passing shadow does not flicker the strip, converts it to a darkness value from 0 to 1, squares that for a gentle curve, then moves the output a few percent toward the target every pass so changes are always a slow fade. The logic-level MOSFET switches the 12 V strip on the low side: the Nano drives the gate through a 220 ohm resistor and the 10 kilohm pull-down keeps the strip off at power-up. The Nano ground and the adapter ground must be joined. A 5 m strip can draw several amps at full brightness, far more than a 2 A adapter and a 1 A fuse allow, so cut off a short length of about 50 cm at the marked cut points, or check the watts per metre on its label and keep the total well under 1 A. Use the fuse, keep the strip off hot surfaces, and never connect mains wiring to this circuit.

Runs on a virtual Arduino Nano in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).

const uint8_t LDR_PIN = A0;
const uint8_t LED_PIN = 9;          // PWM to the MOSFET gate
const uint8_t BTN_PIN = 2;

const int DARK_RAW = 200;           // below this reading the room is dark
const int BRIGHT_RAW = 600;         // above this reading the light stays off
const uint8_t MAX_DUTY = 200;       // about 78 percent, keeps the strip gentle

float smooth = 800;                 // smoothed light reading
float duty = 0;                     // smoothed output
bool autoOn = true;
bool lastBtn = HIGH;

void setup() {
  pinMode(LED_PIN, OUTPUT);
  pinMode(BTN_PIN, INPUT_PULLUP);
  analogWrite(LED_PIN, 0);
  Serial.begin(9600);
}

void loop() {
  bool b = digitalRead(BTN_PIN);
  if (lastBtn == HIGH && b == LOW) { autoOn = !autoOn; delay(30); }   // toggle auto mode
  lastBtn = b;

  int raw = analogRead(LDR_PIN);
  smooth += (raw - smooth) * 0.02;           // slow filter, ignores passing shadows

  float darkness = constrain((BRIGHT_RAW - smooth) / (float)(BRIGHT_RAW - DARK_RAW), 0.0, 1.0);
  float target = autoOn ? darkness * MAX_DUTY : 0;
  target = target * target / MAX_DUTY;       // curve so low light levels feel gentle
  duty += (target - duty) * 0.03;            // fade, never jump
  analogWrite(LED_PIN, (int)duty);

  static unsigned long t0 = 0;
  if (millis() - t0 > 1000) {
    t0 = millis();
    Serial.print("raw "); Serial.print((int)smooth);
    Serial.print("  duty "); Serial.println((int)duty);
  }
  delay(10);
}

What the serial monitor should show

Open the serial monitor at the baud rate in the code. This is what our simulator prints for a virtual Arduino Nano with its sensors at their default values. Your numbers will follow your real readings, but the format should match.

raw 550  duty 1
raw 517  duty 7
raw 512  duty 9

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
Bright roomStrip off
Cover the LDR with your handStrip fades up over about two seconds
Remove your handStrip fades down
Press the button in the darkStrip fades out
Quick hand wave over the LDRNo visible flicker

If it does not work

Strip is always on

The LDR divider is upside down or the gate pull-down is missing.

Strip flickers

Join the grounds, and lower the 0.02 filter value for a slower response.

Nano resets when the strip turns on

Poor ground or too much current, use a shorter strip.

Fuse blows

The strip is too long or the wiring shorts, check before replacing the fuse.

MOSFET gets hot

Make sure it is the logic-level IRLZ44N and the strip current is low.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the automatic fading night light?

Arduino Nano, LDR Photoresistor, Resistor 10 kΩ × 2, Resistor 220 Ω, MOSFET IRLZ44N, 12 V LED strip warm white (5 m, 2835), 12 V 2 A DC adapter, Fuse 1 A glass (5 × 20 mm), Tactile push button 6 mm, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm).

How long does the automatic fading night light take to build?

About 120 minutes for a beginner maker, in 8 steps.

How do I know the automatic fading night light works?

Bright room: Strip off. Cover the LDR with your hand: Strip fades up over about two seconds. Remove your hand: Strip fades down.

Strip is always on: what should I check?

The LDR divider is upside down or the gate pull-down is missing.

Strip flickers: what should I check?

Join the grounds, and lower the 0.02 filter value for a slower response.

Nano resets when the strip turns on: what should I check?

Poor ground or too much current, use a shorter strip.

Fuse blows: what should I check?

The strip is too long or the wiring shorts, check before replacing the fuse.