Kids' colour night light with timer
Build it step by step
0 of 7 doneTest the LED
Light red, green and blue in turn.
Make the colour wheel
A smooth cycle.
Add the fade-out
Dim over 20 minutes.
Add sleep
Wake on the button.
Check battery use
Measure the sleep current.
Make the diffuser
Frosted jar.
Test at bedtime
Check it is soft enough.
How do you build a kids' colour night light with timer?
A soft colour-changing light fades out over 20 minutes, so children fall asleep with it on. You need ATtiny85-20PU microcontroller, RGB LED 5 mm common anode, Resistor 220 Ω × 3, Tactile push button 6 mm, Battery holder 2 × AA and AA alkaline battery × 2. It takes 7 steps and about 90 minutes.
- Level
- Beginner
- Time
- About 90 min
- Parts
- 6
Wiring
| From | To | Why |
|---|---|---|
| Battery plus (3 V) | ATtiny85 pin 8 and the RGB LED common anode | Power |
| Battery minus | ATtiny85 pin 4 | Ground |
| RGB red cathode | 220 Ω to ATtiny85 PB0 (pin 5) | Red PWM |
| RGB green cathode | 220 Ω to ATtiny85 PB1 (pin 6) | Green PWM |
| RGB blue cathode | 220 Ω to ATtiny85 PB4 (pin 3) | Blue PWM |
| Button | ATtiny85 PB2 (pin 7) to ground | Start and stop |
Code
The light fades through a slow colour wheel while its brightness falls in a straight line to zero over 20 minutes, then the chip goes into power-down sleep, using almost no battery until the button is pressed again. Warm colours help children settle, so you can restrict the wheel to red and green and skip the blue segment. With 2 AA cells at 3 V a blue LED may look dim, because blue needs about 3 V; this is fine for a night light. Make a diffuser from a frosted jar, never leave the batteries accessible to young children, and use a fixed, non-flashing fade only, as sudden flashing is not suitable for bedtime and can bother people sensitive to flicker.
Runs on a virtual ATtiny85 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).
// Kids' night light: slow colour fade, dims to off over 20 minutes, sleeps until the button is pressed.
// Board: ATtiny85 (ATTinyCore, 8 MHz internal). Common-anode LED: PWM 255 = off.
#include <avr/sleep.h>
#include <avr/interrupt.h>
const int R = 0, G = 1, B = 4, BTN = 2;
const unsigned long TOTAL = 20UL * 60UL * 1000UL;
ISR(PCINT0_vect) {}
void setRGB(int r, int g, int b) { analogWrite(R, 255 - r); analogWrite(G, 255 - g); analogWrite(B, 255 - b); }
void hueToRGB(int h, int level, int &r, int &g, int &b) { // h 0..767, simple 3-segment colour wheel
int seg = h / 256, x = h % 256;
if (seg == 0) { r = 255 - x; g = x; b = 0; }
else if (seg == 1) { r = 0; g = 255 - x; b = x; }
else { r = x; g = 0; b = 255 - x; }
r = r * level / 255; g = g * level / 255; b = b * level / 255;
}
void sleepNow() {
setRGB(0, 0, 0);
GIMSK |= _BV(PCIE); PCMSK |= _BV(PCINT2);
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable(); sei(); sleep_cpu(); sleep_disable();
delay(300); // let the button settle
}
void setup() {
pinMode(R, OUTPUT); pinMode(G, OUTPUT); pinMode(B, OUTPUT);
pinMode(BTN, INPUT_PULLUP);
}
void loop() {
sleepNow();
unsigned long start = millis();
int hue = 0;
while (millis() - start < TOTAL) {
int level = 200 - (int)(200UL * (millis() - start) / TOTAL); // fades from 200 to 0
int r, g, b;
hueToRGB(hue, level, r, g, b);
setRGB(r, g, b);
hue = (hue + 1) % 768;
delay(120); // about 90 seconds for a full colour cycle
if (digitalRead(BTN) == LOW) { delay(300); break; } // press again to switch off early
}
}Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Press the button | Light fades in and starts cycling colours |
| After 20 minutes | Fully off and asleep |
| Press during the fade | Switches off early |
| Sleep current | A few microamps |
If it does not work
No colours
Check the common-anode wiring and the pin order.
Colours inverted
Common anode needs 255 minus the value.
Blue weak
Normal at 3 V.
Does not wake
Check the button on PB2 and the pin-change setting.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the kids' colour night light with timer?
ATtiny85-20PU microcontroller, RGB LED 5 mm common anode, Resistor 220 Ω × 3, Tactile push button 6 mm, Battery holder 2 × AA and AA alkaline battery × 2.
How long does the kids' colour night light with timer take to build?
About 90 minutes for a beginner maker, in 7 steps.
How do I know the kids' colour night light with timer works?
Press the button: Light fades in and starts cycling colours. After 20 minutes: Fully off and asleep. Press during the fade: Switches off early.
No colours: what should I check?
Check the common-anode wiring and the pin order.
Colours inverted: what should I check?
Common anode needs 255 minus the value.
Blue weak: what should I check?
Normal at 3 V.
Does not wake: what should I check?
Check the button on PB2 and the pin-change setting.
