IoTSimulator

Build an Automatic Night Light with an Arduino Nano

Build an intelligent, adaptive ambient night light using the compact Arduino Nano V3. Read continuous light intensity from a photoresistor (LDR) on analog pin A0 and modulate an RGB LED with warm amber PWM fading as dusk falls.
Muhammad Ichsanul Fadhil
IoTSim Editor
October 2, 2026
Build an Automatic Night Light with an Arduino Nano

Live project track

Interactive hardware & logic preview

Waking up in the middle of the night to pitch-black darkness often leaves you fumbling for light switches, only to be blinded by a harsh ceiling bulb that shocks your eyes and makes falling back asleep difficult. A smart night light solves this problem gently: it stays completely turned off during the bright daytime, but as evening approaches, it gradually illuminates your room with a soft, warm candle-like glow.

In this project, you will build an automatic night light using a compact Arduino Nano V3, a photoresistor light sensor, and an RGB LED. Instead of clicking abruptly between full brightness and pitch darkness, your circuit will smoothly fade its brightness up and down while blending red and green channels to produce a soothing, eye-friendly sunset amber glow.

How Light Sensors Detect Darkness

Ambient Light vs Adaptive LED Brightness Curve
Figure 1: As ambient room light decreases below 400, the night light smoothly ramps up brightness from 50 to 255.

A photoresistor, often called an LDR (Light Dependent Resistor), changes its electrical resistance based on the amount of light falling on its surface. In bright sunlight, its resistance drops dramatically, allowing more current to pass. In darkness, its resistance surges, reducing current flow. When combined with a fixed resistor on a sensor module, this produces an analog voltage that the Arduino Nano reads on pin A0.

Ambient ConditionSensor Reading (ADC Pin A0)Night Light StateLED Brightness (PWM)
Direct Sunlight / Bright Room700 to 1023Completely OFF0 (Conserves energy)
Dim Room / Overcast400 to 699Completely OFF0 (Above trigger threshold)
Sunset / Twilight200 to 399Soft Night Glow50 to 150 (Gentle amber)
Deep Darkness / Night0 to 199Full Night Glow150 to 255 (Clear path lighting)

By setting a darkness threshold of 400, the Arduino Nano knows exactly when twilight arrives. As room light drops below 400, the microcontroller smoothly scales up the LED brightness. This gradual transition prevents the jarring sudden flash common in cheaper click-on night lights.

Creating Warm Amber Light Without Harsh Glare

Mixing a Warm Sunset Amber Glow with RGB PWM
Figure 2: Combining 100% red with 33% green creates a warm sunset amber while leaving blue completely off.

Standard white LEDs often emit strong blue light wavelengths that stimulate daylight receptors in human eyes, telling your brain that it is morning and disrupting natural sleep patterns. To make our night light comfortable for bedrooms and hallways, we use an RGB LED to custom-blend an eye-friendly warm amber glow.

LED Color ChannelArduino Nano PinPWM Output RatioVisual Function
Red ChannelDigital Pin D9 (PWM)100% (Full brightness)Provides warm foundational glow and primary night visibility
Green ChannelDigital Pin D10 (PWM)33% (brightness / 3)Softens the deep red into a cozy, golden sunset amber
Blue ChannelDigital Pin D11 (PWM)0% (Completely OFF)Prevents harsh glare and avoids disrupting restful sleep cycles

By driving the green channel at exactly one-third the brightness of the red channel, the LED keeps its warm sunset tone whether it is running at a faint 20% glow during early dusk or full brightness in the middle of the night.

Everyday Magic: How Automatic Night Lights Work

Automatic bedside night light turning on as daylight fades into night
Figure 3: Automatic night light logic: daylight keeps the lamp turned off, and as room light fades, a warm cozy glow gently turns on.

Think of this project like a smart hallway or bedside night light. During the day when the sun is out, the photoresistor detects plenty of light, so the Arduino keeps the LED completely off to save electricity. But as sunset arrives and the room grows dark, the microcontroller smoothly turns on a warm amber glow to keep paths visible. The interactive connection tables below show how each module connects to the Arduino Nano.

Pin Connection Map
Photoresistor Module Pin
AO (Analog Out)
→
Arduino Nano Pin
Analog Pin A0
Explanation
Carries variable light voltage (0V to 5V)
Photoresistor Module Pin
VCC
→
Arduino Nano Pin
5V
Explanation
Provides 5V operating power to sensor circuit
Photoresistor Module Pin
GND
→
Arduino Nano Pin
GND.1
Explanation
Sensor ground return connection

The photoresistor module has a built-in voltage divider resistor on board, allowing you to connect its analog output pin directly to analog pin A0 on the Arduino Nano without any extra breadboard resistors.

Pin Connection Map
Common Cathode RGB LED Pin
Red Anode (R)
→
Arduino Nano Pin
Digital Pin D9
Explanation
Timer 1 Channel A hardware PWM output
Common Cathode RGB LED Pin
Green Anode (G)
→
Arduino Nano Pin
Digital Pin D10
Explanation
Timer 1 Channel B hardware PWM output
Common Cathode RGB LED Pin
Blue Anode (B)
→
Arduino Nano Pin
Digital Pin D11
Explanation
Timer 2 Channel A hardware PWM output (Held at 0)
Common Cathode RGB LED Pin
Common Cathode (COM)
→
Arduino Nano Pin
GND.2
Explanation
Shared ground return for all three color diodes

A common cathode RGB LED has four pins: the longest pin is the cathode, which connects directly to the Nano's ground header. The other three pins are the individual anodes for red, green, and blue. Connecting them to pins D9, D10, and D11 gives us full hardware PWM dimming control.

Complete Code

Here is the complete Arduino sketch for your automatic night light. It reads the ambient light level five times per second and smoothly adjusts the amber glow:

C++ Source
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// ============================================================================
// Arduino Nano Automatic Adaptive Night Light
// LDR Sensor on Analog Pin A0
// Common Cathode RGB LED on PWM Pins: Red=9, Green=10, Blue=11
// ============================================================================

const int LDR_PIN   = A0;
const int RED_PIN   = 9;  // Hardware Timer 1 PWM
const int GREEN_PIN = 10; // Hardware Timer 1 PWM
const int BLUE_PIN  = 11; // Hardware Timer 2 PWM

// Threshold below which darkness triggers the night light
const int DARKNESS_THRESHOLD = 400;

void setup() {
  Serial.begin(9600);
  
  pinMode(RED_PIN, OUTPUT);
  pinMode(GREEN_PIN, OUTPUT);
  pinMode(BLUE_PIN, OUTPUT);
  
  Serial.println(F("Arduino Nano Automatic Adaptive Night Light Initialized!"));
}

void loop() {
  // 1. Read ambient illumination from Photoresistor (0 to 1023)
  int lightLevel = analogRead(LDR_PIN);
  
  // 2. Evaluate daylight vs darkness
  if (lightLevel < DARKNESS_THRESHOLD) {
    // Darkness detected: Map light inversely to smooth PWM duty cycle (50 to 255)
    int brightness = map(lightLevel, DARKNESS_THRESHOLD, 0, 50, 255);
    brightness = constrain(brightness, 50, 255);
    
    // Mix Warm Sunset Amber: Full Red + 1/3 Green + 0 Blue (No harsh blue light)
    analogWrite(RED_PIN, brightness);
    analogWrite(GREEN_PIN, brightness / 3);
    analogWrite(BLUE_PIN, 0);
    
    Serial.print(F("[NIGHT GLOW] Lux ADC: "));
    Serial.print(lightLevel);
    Serial.print(F(" | Brightness: "));
    Serial.println(brightness);
  } else {
    // Daytime / Room light ON: Completely extinguish LED to conserve energy
    analogWrite(RED_PIN, 0);
    analogWrite(GREEN_PIN, 0);
    analogWrite(BLUE_PIN, 0);
    
    Serial.print(F("[DAYLIGHT] Lux ADC: "));
    Serial.print(lightLevel);
    Serial.println(F(" | Lamp: OFF"));
  }

  delay(200); // 5Hz sampling cycle
}

How the Code Works, Part by Part

The sketch follows a simple, non-blocking sequence: measuring room illumination, checking if it is dark enough to turn on, calculating proportional brightness, and driving the LED pins.

Reading the Ambient Light Level

Every 200 milliseconds, the loop begins by taking an analog reading from the photoresistor:

C++ Source
1
int lightLevel = analogRead(LDR_PIN);

The Arduino Nano's analog-to-digital converter turns the sensor's voltage into a whole number between 0 (complete darkness) and 1023 (bright direct light).

Smooth Fading with the Map and Constrain Functions

When darkness is detected, the program calculates the target brightness using an inverse mathematical scaling:

C++ Source
1
2
int brightness = map(lightLevel, DARKNESS_THRESHOLD, 0, 50, 255);
brightness = constrain(brightness, 50, 255);

The map() function translates our sensor range (400 down to 0) into an output range of 50 up to 255. When the sensor reads 400, brightness is 50. As darkness deepens towards 0, brightness ramps up to 255. The constrain() function ensures the number never exceeds safe 8-bit limits.

Blending Sunset Amber via PWM Pins

Finally, the calculated brightness is distributed across the RGB LED color pins using pulse-width modulation:

C++ Source
1
2
3
analogWrite(RED_PIN, brightness);
analogWrite(GREEN_PIN, brightness / 3);
analogWrite(BLUE_PIN, 0);

Setting the blue pin to 0 ensures zero blue light is emitted. Driving the red channel at full calculated brightness and the green channel at one-third yields the relaxing amber glow that makes this night light so comfortable.

Fixing Common Problems

If your night light is not turning on or showing the right colors, use the table below to quickly find and fix the issue:

Observed ProblemLikely CauseRecommended Solution
LED never illuminates, even in darkCommon cathode pin wired to 5V instead of GNDEnsure the longest leg of the RGB LED connects to a GND header pin on the Arduino Nano.
LED glows cyan or magenta instead of amberRed and green jumper wires swappedVerify that Pin D9 connects to the Red anode and Pin D10 connects to the Green anode.
Lamp flickers rapidly when starting to glowOptical feedback: LED shines directly onto sensorShield the photoresistor so light from the LED does not shine directly into the sensor surface.
Serial Monitor displays constant 1023Sensor ground wire loose or disconnectedInspect the black jumper wire connecting the sensor module GND pin to Nano GND.1.

Opening the Arduino Serial Monitor at 9600 baud allows you to watch the live sensor reading in real time, making it simple to calibrate the threshold for your specific room lighting.

Try It in the Simulator

Click the Start Simulation button in the top toolbar to test your circuit. Click on the photoresistor module to reveal its interactive brightness slider. Drag the slider to the left toward darkness: watch the LED smoothly illuminate and warm up to sunset amber as the reading drops below 400. Drag the slider back to the right to watch the lamp gracefully extinguish.

Keywords
#Arduino Nano #Photoresistor #LDR #RGB LED #PWM #Analog #Smart Home #Beginner
Total word count: 1185 words

Project discussion

Questions, feedback, and community insights
No discussions yet. Be the first to start!