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 Runs in your browser. Free, and no account needed.
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);
}
Parts list
7 parts, plus the jumper wires. Every one is in the editor's parts bin.
How it is wired
8 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.
- Arduino Uno R3 pin 6; Resistor, 330 Ω pin 1
- Arduino Uno R3 pin 2; Pushbutton pin 1a
- Arduino Uno R3 pin 5V; Photoresistor sensor module pin VCC; Addressable RGB LEDs pin VDD; Character LCD with I2C backpack pin VCC
- Ground: Arduino Uno R3 pin GND; Photoresistor sensor module pin GND; Pushbutton pin 2a; Addressable RGB LEDs pin VSS; Character LCD with I2C backpack pin GND
- Arduino Uno R3 pin A0; Photoresistor sensor module pin AO
- Arduino Uno R3 pin A4; Character LCD with I2C backpack pin SDA
- Arduino Uno R3 pin A5; Character LCD with I2C backpack pin SCL
- Resistor, 330 Ω pin 2; Addressable RGB LEDs pin DIN
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.