Build it step by step
0 of 8 doneWire the PIR
VCC to 5V, GND to GND, OUT to D2.
Wire the light sensor
LDR from 5V to A0, 10 kilohm from A0 to GND.
Wire the LEDs
Each through 330 ohm on D9, D10 and D11.
Turn the PIR delay screw
To its lowest setting.
Upload the sketch and wait
Thirty seconds for the sensor to settle.
Cover the LDR and wave a hand
The LEDs fade in.
Stay still
They fade out after a minute.
Try in a bright room
The LEDs stay off.
How do you build a motion night light?
A PIR sensor turns on a soft night light for a minute when someone walks past in the dark. You need Arduino Uno R3, PIR Motion Sensor HC-SR501, LDR Photoresistor, Resistor 10 kΩ, LED 5 mm white × 3, Resistor 330 Ω × 3, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm). It takes 8 steps and about 60 minutes.
- Level
- Beginner
- Time
- About 60 min
- Parts
- 8
Parts list
- Development BoardsBuyArduino Uno R3 × 1
- SensorsBuy
- SensorsBuy
- PassivesBuyResistor 10 kΩ × 1
- OptoelectronicsBuyLED 5 mm white × 3
- PassivesBuyResistor 330 Ω × 3
- PrototypingBuy
- PrototypingBuy
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| PIR VCC | Uno 5V | Power |
| PIR GND | Uno GND | Ground |
| PIR OUT | Uno D2 | High when motion is seen |
| LDR one leg | Uno 5V | Top of the light divider |
| LDR other leg and 10 kilohm | Uno A0 | Divider midpoint, 10 kilohm goes to GND |
| White LED 1 through 330 ohm | Uno D9 to GND | Night light |
| White LED 2 through 330 ohm | Uno D10 to GND | Night light |
| White LED 3 through 330 ohm | Uno D11 to GND | Night light |
Code
A passive infrared sensor notices the change in heat when a warm body moves across its view, and its output goes high. On its own it would switch a light on in daytime too, so a light sensor makes it useful: the LDR and the 10 kilohm resistor form a divider whose voltage rises as the room gets brighter, and the light only comes on when the reading is below a threshold. The light stays on for a minute after the last motion and fades in and out smoothly using PWM, which looks gentler than a sudden switch. Three LEDs on three pins, each with its own resistor, keep every pin well within its current limit. The HC-SR501 has two small screws: one sets how long its own output stays high and one sets sensitivity; turn the delay to the minimum so the sketch controls the timing. It needs about 30 seconds after power on to settle, which the sketch waits for. Mount it facing a doorway, away from heaters or sunny windows, which can cause false triggers.
Runs on a virtual Arduino Uno 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 int PIR = 2;
const int LDR = A0;
const int LEDS[] = {9, 10, 11};
const int DARK_BELOW = 350; // 0 to 1023, higher means brighter, tune for your room
const unsigned long HOLD_MS = 60000; // stay on for 60 seconds after the last motion
unsigned long lastMotion = 0;
int level = 0; // current brightness 0 to 255
bool lit = false;
void setLeds(int v) {
for (int i = 0; i < 3; i++) analogWrite(LEDS[i], v);
}
void setup() {
pinMode(PIR, INPUT);
for (int i = 0; i < 3; i++) pinMode(LEDS[i], OUTPUT);
Serial.begin(9600);
delay(30000); // the PIR needs about 30 seconds to settle
}
void loop() {
int light = analogRead(LDR);
bool dark = (light < DARK_BELOW);
if (digitalRead(PIR) == HIGH && dark) {
lastMotion = millis();
lit = true;
}
if (lit && (millis() - lastMotion > HOLD_MS || !dark)) lit = false;
int target = lit ? 200 : 0; // keep it soft, not full brightness
if (level < target) level += 4; // fade in
if (level > target) level -= 2; // fade out slowly
level = constrain(level, 0, 255);
setLeds(level);
Serial.print(light); Serial.print(' ');
Serial.println(lit ? "on" : "off");
delay(20);
}Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Dark and motion | Light fades in |
| No more motion | Off after 60 seconds |
| Bright room and motion | Stays off |
| Light turns on while lit | Switches off |
| First 30 seconds | No response while settling |
If it does not work
Light never comes on
Lower the room light or raise DARK_BELOW.
Comes on in daylight
Lower DARK_BELOW, check the LDR divider.
Random triggers
Move away from heat sources and shield from sunlight.
Light stays on forever
The PIR delay screw is too high, turn it down.
LEDs flicker
Check connections on D9, D10 and D11.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the motion night light?
Arduino Uno R3, PIR Motion Sensor HC-SR501, LDR Photoresistor, Resistor 10 kΩ, LED 5 mm white × 3, Resistor 330 Ω × 3, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm).
How long does the motion night light take to build?
About 60 minutes for a beginner maker, in 8 steps.
How do I know the motion night light works?
Dark and motion: Light fades in. No more motion: Off after 60 seconds. Bright room and motion: Stays off.
Light never comes on: what should I check?
Lower the room light or raise DARK_BELOW.
Comes on in daylight: what should I check?
Lower DARK_BELOW, check the LDR divider.
Random triggers: what should I check?
Move away from heat sources and shield from sunlight.
Light stays on forever: what should I check?
The PIR delay screw is too high, turn it down.
