Build a Motion-Activated Night Light

A PIR motion sensor on its own cannot tell time of day — it fires the same way at noon as it does at midnight, which means a light wired straight to a PIR turns on in broad daylight every time someone walks past it. Pairing the PIR with a light-dependent resistor (LDR) fixes that: the firmware reads both sensors and only switches the light on when the AND condition is true — motion detected AND the room reads below a darkness threshold. Two cheap sensors and one line of conditional logic turn a light that fires constantly into one that only fires when it is actually useful.

Why do you need two sensors instead of just a PIR?

A PIR sensor detects a moving heat source and outputs a simple high or low signal — it has no concept of ambient brightness. If that signal drives a light directly, the light comes on every time something crosses its field of view, day or night, which defeats the point of a night light and wastes power during daylight hours. Adding an LDR gives the firmware a second, independent reading: how bright the room currently is. The code then only lights up when both conditions hold at once, which is a two-line if statement in firmware but a real hardware decision in your parts list, since it means sourcing and wiring a light sensor alongside the motion sensor rather than skipping it.

What parts does a motion-activated night light need?

At minimum: a microcontroller to run the logic, a PIR module to detect movement, a light sensor to measure darkness, something to illuminate, a breadboard and jumper wires to connect it all, and a power source. The light sensor is commonly a bare LDR (light-dependent resistor) rather than a packaged module — it is inexpensive but is a passive component, not a sensor breakout, so it needs a resistor wired as a voltage divider before the microcontroller can read it as a changing analog value. What actually lights up varies by build: it can be a single LED, an LED strip for wider room coverage, or — for controlling an existing mains lamp — a relay module wired between the microcontroller and the lamp's power line.

How do you set the darkness threshold?

An LDR's resistance changes with the amount of light falling on it, but a microcontroller cannot read resistance directly — it reads voltage. Wiring the LDR into a voltage divider with a fixed resistor converts that changing resistance into a changing voltage, which an analog input pin then reads as a raw number (0-1023 on an Arduino Uno's 10-bit ADC). "Dark" is not a fixed number across rooms — a hallway with a nightlight already on somewhere else will read differently than a windowless closet — so the threshold in the firmware has to be tuned to your actual room by reading the raw LDR value in daylight and at the darkness level you want the light to trigger at, then setting the cutoff between the two.

Can you use this to control a mains lamp instead of an LED?

Yes, but not with the LED-only wiring described above. Switching a mains lamp means putting a relay module between the microcontroller and the lamp's live wire, with the microcontroller only ever touching the relay's low-voltage control pins and never the mains side directly. That mains side carries lethal voltage, so it needs real electrical precautions — a relay rated for the lamp's voltage and current, mains wiring kept physically separated from the low-voltage breadboard side, and enclosure or insulation on any exposed mains terminal. An LED or LED strip build has none of this risk since the entire circuit runs on the microcontroller's own low-voltage supply; a relay-driven mains lamp is a meaningfully bigger step up in both wiring complexity and required care, and isn't something to improvise without that awareness.

Watch it built live

We typed this into Compoden's storefront assistant, Soldr:

"I want to build a motion-activated night light that only turns on in the dark, using a PIR sensor and a light sensor with an Arduino Uno"

Compoden storefront AI assistant Soldr building a motion-activated night light parts tray with Arduino Uno, PIR sensor, LDR and LED strip

Soldr matched all 7 parts against the catalog on the first pass and priced the build at Rs.843. As it explained the build: "This build creates a night light that turns on automatically when it's dark and senses movement. It uses sensors to detect these conditions and then illuminates an LED strip, providing light only when and where it's needed." On the light sensor: "The GL5528 LDR is a Light Dependent Resistor, which means its electrical resistance changes based on how much light falls on it... You will need to calibrate it, which means figuring out what resistance values correspond to 'dark' in your specific environment." Soldr chose an RGB 5050 SMD LED strip as the actual light source rather than a single LED, and it flagged a real power caveat on that choice worth repeating in full: "power issues can arise; the RGB 5050 SMD LED Strip 12V 1M requires 12V, but the Arduino Uno R3 CH340G ATmega328P Board can only accept 7-12V DC as input and outputs 5V logic, and the 5V 2A Power Adapter (Barrel Connector) only provides 5V. This means you will need a separate 12V power source for the [strip]." In other words, this exact tray needs a second, 12V-rated power supply for the LED strip in addition to the 5V adapter that powers the Arduino — worth knowing before you order if you were expecting one adapter to run everything. It's also worth noting the GL5528 is the bare photoresistor rather than a pre-wired LDR module, so the voltage-divider resistor it needs isn't one of the 7 core parts — Soldr surfaced a resistor variety pack as an optional add-on alongside the main tray.

Watch the firmware get generated

We then took the same brief to Compoden's VoltIQ/Soldr code assistant and asked for working firmware:

"I'm building a motion-activated night light with an Arduino Uno, a PIR motion sensor, an LDR light sensor, and an LED. Write firmware that only turns the LED on when motion is detected AND ambient light is below a threshold."

VoltIQ PWA generated Arduino firmware for motion-activated night light with PIR and LDR AND condition

For this firmware pass the assistant matched the request to a simpler light source than the storefront's LED strip — a 5mm common-cathode RGB LED — since the prompt asked specifically for "an LED." The generated sketch defines its pins directly in comments: #define PIR_SENSOR_PIN 2 // HC-SR501 PIR Motion Sensor Module data pin, #define LDR_SENSOR_PIN A2 // GL5528 LDR analog input pin, and #define RGB_LED_PIN 3. The darkness cutoff is set as #define DARK_THRESHOLD 300, with an inline comment explaining the scale: "Lower value means darker environment required to trigger. Typical values range from 0 (dark) to 1023 (bright)." The build log above the code panel confirmed the same 7-part match Soldr had already made — PIR sensor, LDR, LED, breadboard, jumper wires, and a power adapter — and the instructions read: "Upload this sketch to your Arduino Uno R3 using the Arduino IDE, ensuring the correct board and port are selected."

Get everything in this build

These are the exact parts Soldr selected for the live storefront build above, with real prices and direct add-to-cart links.

Part Price Add to cart
Arduino Uno R3 CH340G ATmega328P Board Rs.230 Add to cart
HC-SR501 PIR Motion Sensor Module Rs.70 Add to cart
GL5528 LDR Rs.5 Add to cart
RGB 5050 SMD LED Strip 12V 1M Rs.288 Add to cart
400-point solderless breadboard Rs.40 Add to cart
Male-to-Male Breadboard Jumper Wires (20 cm, 24 AWG) Rs.40 Add to cart
5V 2A Power Adapter (Barrel Connector) Rs.170 Add to cart
Total Rs.843 Add all 7 to cart

As noted above, the LED strip in this tray runs on 12V and needs its own 12V-rated power source separate from the 5V adapter listed here, and the GL5528 LDR needs a fixed resistor for its voltage divider, which Soldr offers as an optional add-on rather than including in the core 7 parts.

Built and Backed by Compoden

Every part listed above ships from Compoden's own India stock, checked for compatibility before being carded together for a build like this one. Orders typically arrive in 3-7 days, with Cash on Delivery and UPI both supported at checkout, and Compoden's support team is available if you run into wiring or firmware questions while putting the project together.

Frequently asked questions

Why won't a single PIR sensor work for a night light on its own?
A PIR only reports motion — it has no way to tell whether the room is already lit. Wired directly to a light, it triggers the same way at noon as it does at midnight. Adding an LDR gives the firmware a second reading (ambient brightness) so it can require both motion and darkness before switching on.

Do I need to calibrate the LDR before this works properly?
Yes. An LDR's raw reading depends on your specific room's lighting, so a fixed threshold picked without testing may trigger during daylight or never trigger at night. Read the raw analog value in your room at the darkness level you want as the cutoff, then set the firmware's threshold there.

Can I run the LED strip from the same power adapter as the Arduino?
Not with the parts in this tray. The RGB 5050 SMD LED strip needs a 12V supply, while the 5V 2A adapter listed here only powers the Arduino side. You'll need a separate 12V-rated power source for the strip itself.

Is switching a mains lamp instead of an LED a simple swap?
No. It requires adding a relay module between the microcontroller and the lamp's live wire, and working with mains voltage safely — keeping mains wiring separated from the low-voltage side and insulating exposed terminals. It's a meaningfully bigger step than an LED or LED-strip build.

Back to blog