Kind words night light on an Arduino Uno
When the room goes dark a ring of WS2812B LEDs fades up to soft amber and a 16x2 LCD shows a kind note. A button brings the next one, and the screen sleeps after half a minute. The whole build, an Arduino Uno and 5 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
Teaching? Assign it to your class in one click.
Beginner About 20 minutes to build and run. Runs in your browser. Free, and no account needed.
Build it as the First Inventions Kit: the parts in numbered bags and a booklet that checks every step. Retails for about $35, free on Mokxi.
What you'll learn
- Reading a light sensor with
analogRead - Hysteresis: two thresholds to avoid flicker
- Fading addressable LEDs
- Mixing a warm amber color
- Timing an auto-off with
millis
- Difficulty: Beginner
- Time: About 20 minutes
- Board: Arduino Uno R3
- Parts: 7
How it works
The photoresistor module on A0 measures the room light. When the reading rises above 700, the room counts as dark. The ring of 8 LEDs on pin 6 fades up to soft amber and the LCD shows a kind note.
It turns off again when the reading falls below 600. Using two different limits stops flicker near the edge. The LCD backlight goes out after 30 seconds, and the button on pin 2 shows the next note.
Parts list
7 parts, plus the jumper wires. They retail for about $38; on Mokxi they're free.
- 1 × Arduino Uno R3In Starter Kit, Claw Machine Kit, Vending Machine Kit.
- 1 × Half breadboard
- 1 × Photoresistor sensor moduleIn Desk Buddy Kit, Escape Box Kit, First Inventions Kit.
- 1 × PushbuttonIn Starter Kit, Christmas Lights Kit, Logic Lab Kit.
- 1 × Resistor, 330 ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 1 × Addressable RGB LEDsIn Claw Machine Kit, Halloween Prop Kit, Christmas Lights Kit.
- 1 × Character LCD with I2C backpackIn Claw Machine Kit, Vending Machine Kit, Rhythm Game Kit.
Wiring table
Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.
| Part and pin | Connects to |
|---|---|
| Resistor, 330 Ω pin 1 | Arduino Uno R3 pin 6 |
| Pushbutton pin 1a | Arduino Uno R3 pin 2 |
| Photoresistor sensor module pin VCC | Arduino Uno R3 pin 5V |
| Addressable RGB LEDs pin VDD | Arduino Uno R3 pin 5V |
| Character LCD with I2C backpack pin VCC | Arduino Uno R3 pin 5V |
| Photoresistor sensor module pin GND | Arduino Uno R3 pin GND |
| Pushbutton pin 2a | Arduino Uno R3 pin GND |
| Addressable RGB LEDs pin VSS | Arduino Uno R3 pin GND |
| Character LCD with I2C backpack pin GND | Arduino Uno R3 pin GND |
| Photoresistor sensor module pin AO | Arduino Uno R3 pin A0 |
| Character LCD with I2C backpack pin SDA | Arduino Uno R3 pin A4 |
| Character LCD with I2C backpack pin SCL | Arduino Uno R3 pin A5 |
| Addressable RGB LEDs pin DIN | Resistor, 330 Ω pin 2 |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Kind words night light: a warm night light that turns on when the room goes
// dark and says something nice on its little screen. Press the button for
// another kind word.
//
// light sensor module AO -> A0, VCC -> 5 V, GND -> GND
// glow 8 WS2812B LEDs in a ring, DIN <- 330 ohms <- pin 6
// LCD 16x2 with an I2C backpack: SDA -> A4, SCL -> A5
// button pin 2 -> button -> GND (INPUT_PULLUP)
//
// A gift for Mother's Day or Father's Day, for a new baby's room, or for a
// grandparent who likes a light on in the hall. When the room goes dark the
// ring fades up to a soft amber and the screen shows one of the notes in
// NOTES. The screen's backlight turns off after half a minute so it does not
// keep anyone awake, and a press of the button brings it back with the next
// note.
//
// Amber, with very little blue, is the kindest color for a night light: it
// is easy on sleepy eyes and still enough to find the door by.
//
// Things to try. Drag the light sensor's slider down to make it night. Press
// the button. Write your own NOTES, two lines of 16 letters each.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Adafruit_NeoPixel.h>
const int BUTTON_PIN = 2;
const int LEDS = 8;
const int DARK_ABOVE = 700; // A0 reading: night above this
const int LIGHT_BELOW = 600; // and day below this
const unsigned long SCREEN_MS = 30000UL;
LiquidCrystal_I2C lcd(0x27, 16, 2);
Adafruit_NeoPixel glow(LEDS, 6, NEO_GRB + NEO_KHZ800);
const char *NOTES[][2] = {
{"You are loved", "Sleep well"},
{"Proud of you,", "always"},
{"Thank you for", "all you do"},
{"Sweet dreams", "See you soon"},
{"You make home", "feel like home"},
};
const int COUNT = sizeof(NOTES) / sizeof(NOTES[0]);
bool night = false;
int note = 0;
unsigned long noteAt = 0;
bool screenOn = false;
void say(int n) {
lcd.backlight();
lcd.clear();
lcd.print(NOTES[n][0]);
lcd.setCursor(0, 1);
lcd.print(NOTES[n][1]);
screenOn = true;
noteAt = millis();
Serial.print("Kind word: ");
Serial.print(NOTES[n][0]);
Serial.print(" ");
Serial.println(NOTES[n][1]);
}
void amber(int level) {
for (int i = 0; i < LEDS; i++) glow.setPixelColor(i, level, level * 2 / 5, level / 20);
glow.show();
}
void setup() {
Serial.begin(9600);
pinMode(BUTTON_PIN, INPUT_PULLUP);
glow.begin();
glow.show();
lcd.init();
lcd.noBacklight();
Serial.println("Night light waiting for the dark");
}
void loop() {
int light = analogRead(A0);
if (!night && light > DARK_ABOVE) {
night = true;
Serial.println("Good night: light on");
for (int v = 0; v <= 160; v += 4) {
amber(v);
delay(25);
}
say(note);
} else if (night && light < LIGHT_BELOW) {
night = false;
amber(0);
lcd.clear();
lcd.noBacklight();
screenOn = false;
note = (note + 1) % COUNT; // a new note tomorrow night
Serial.println("Good morning: light off");
}
if (night && digitalRead(BUTTON_PIN) == LOW) {
note = (note + 1) % COUNT;
say(note);
while (digitalRead(BUTTON_PIN) == LOW) delay(10);
}
if (screenOn && millis() - noteAt >= SCREEN_MS) {
lcd.noBacklight();
screenOn = false;
}
delay(20);
}
Code walkthrough
The key functions of the sketch, and what each one does.
say sketch.ino
Turns on the backlight, clears the screen and prints both lines of the chosen note. It also remembers the time so the screen can switch off later, and prints the note in the serial monitor.
void say(int n) {
lcd.backlight();
lcd.clear();
lcd.print(NOTES[n][0]);
lcd.setCursor(0, 1);
lcd.print(NOTES[n][1]);
screenOn = true;
noteAt = millis();
Serial.print("Kind word: ");
Serial.print(NOTES[n][0]);
Serial.print(" ");
Serial.println(NOTES[n][1]);
}amber sketch.ino
Sets all 8 LEDs to the same color. Red is the full level, green is two fifths of it and blue is a twentieth. That ratio makes a warm amber.
void amber(int level) {
for (int i = 0; i < LEDS; i++) glow.setPixelColor(i, level, level * 2 / 5, level / 20);
glow.show();
}loop sketch.ino
Reads the sensor and changes between day and night using DARK_ABOVE and LIGHT_BELOW. At night it fades the ring up, handles the button, and turns the backlight off after SCREEN_MS.
void loop() {
int light = analogRead(A0);
if (!night && light > DARK_ABOVE) {
night = true;
Serial.println("Good night: light on");
for (int v = 0; v <= 160; v += 4) {
amber(v);
delay(25);
}
say(note);
} else if (night && light < LIGHT_BELOW) {
night = false;
amber(0);
lcd.clear();
lcd.noBacklight();
screenOn = false;
note = (note + 1) % COUNT; // a new note tomorrow night
Serial.println("Good morning: light off");
}
if (night && digitalRead(BUTTON_PIN) == LOW) {
note = (note + 1) % COUNT;
say(note);
while (digitalRead(BUTTON_PIN) == LOW) delay(10);
}
if (screenOn && millis() - noteAt >= SCREEN_MS) {
lcd.noBacklight();
screenOn = false;
// ... 3 more lines in the editorTroubleshooting
The usual mistakes with this circuit, and the fix for each.
- The light never turns on. This project
Check the sensor's AO pin goes to A0 and that the reading goes above 700 when it is dark. - It flickers around dusk. This project
KeepDARK_ABOVEhigher thanLIGHT_BELOW, with a good gap. - The LCD is blank. This project
SDA goes to A4 and SCL to A5. - The digital output never changes, or is always on, no matter the light. Photoresistor sensor module
Turn the trimmer on the module until DO flips at the light level you want. The digital output is set by that threshold, and which direction means bright depends on the board. - Analog readings go down when it gets brighter, and the code expects the opposite. Photoresistor sensor module
The LDR sits in a divider, so the direction depends on how the module is built. Print the raw value in the light and dark, then flip it in code with 1023 minus the reading if needed. - The input reads random HIGH and LOW when the button is not pressed. Pushbutton
A floating pin picks up noise. Add a 10 k pull-down or pull-up resistor, or use INPUT_PULLUP and wire the button between the pin and GND. Then pressed reads LOW. - The button seems always pressed, or never changes. Pushbutton
A 4-leg tactile switch has legs joined in pairs. Place it across the breadboard center gap so the pairs sit on different rows. Wired along one side, the legs are already connected. - The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.