Message display gift on an Arduino Uno
A 16x2 LCD gift full of your own notes for Mother’s Day, Father’s Day or anyone who looks after you: a button turns to the next one and a red LED heart glows. The whole build, an Arduino Uno and 4 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 15 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $34. On Mokxi they're free.
What you'll learn
- Showing text on an I2C LCD
- Making a custom LCD character
- A button with INPUT_PULLUP
- Fading an LED with analogWrite
- Cycling through a list with the modulo operator
- Difficulty: Beginner
- Time: About 15 minutes
- Board: Arduino Uno R3
- Parts: 6
How it works
A 16x2 LCD on the I2C pins A4 and A5 shows short notes from a list called MESSAGES. Each note has two lines of up to 16 letters. The first one shows at power-up.
Press the button on pin 2 to see the next note. After the last, it starts again. If nobody presses for 20 seconds, it moves on by itself.
Each time a note appears, a small heart is drawn in the bottom right of the screen, and the red LED on pin 5 fades up and back down.
Parts list
6 parts, plus the jumper wires. They retail for about $34; on Mokxi they're free.
- 1 × Arduino Uno R3In Starter Kit, Claw Machine Kit, Vending Machine Kit.
- 1 × Half breadboard
- 1 × PushbuttonIn Starter Kit, Christmas Lights Kit, Logic Lab Kit.
- 1 × Resistor, 220 ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 1 × LED, redIn Starter Kit, Halloween Prop Kit, Logic Lab Kit.
- 1 × Character LCD with I2C backpackIn Claw Machine Kit, Vending Machine Kit, Rhythm Game Kit.
Kits that can build it
These 3 starter kits have every part in the circuit. Each kit's page lists what else it can build.
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, 220 Ω pin 1 | Arduino Uno R3 pin 5 |
| Pushbutton pin 1a | Arduino Uno R3 pin 2 |
| Character LCD with I2C backpack pin VCC | Arduino Uno R3 pin 5V |
| Pushbutton pin 2a | Arduino Uno R3 pin GND |
| LED, red pin C | Arduino Uno R3 pin GND |
| Character LCD with I2C backpack pin GND | Arduino Uno R3 pin GND |
| Character LCD with I2C backpack pin SDA | Arduino Uno R3 pin A4 |
| Character LCD with I2C backpack pin SCL | Arduino Uno R3 pin A5 |
| LED, red pin A | Resistor, 220 Ω pin 2 |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Message display gift: a little screen for Mother's Day, Father's Day or a
// birthday, full of notes you wrote yourself. Press the button for the next
// one, and a heart glows on every new message.
//
// LCD 16x2 with an I2C backpack: SDA -> A4, SCL -> A5
// button pin 2 -> button -> GND (INPUT_PULLUP)
// heart pin 5 -> 220 ohms -> red LED -> GND
//
// The whole gift is the list in MESSAGES: two lines of up to 16 letters each.
// Write as many as you like, for a mom, a dad, a grandparent, a stepparent or
// anyone who looks after you, with their name in it. The first message
// shows at power-up, and after the last one it starts again.
//
// The heart on the LCD is a custom character, eight rows of five dots. When
// nobody has pressed the button for a while, the screen moves on by itself,
// so the gift keeps talking on a shelf.
//
// Things to try. Press the button. Write your own MESSAGES, with a name.
// Set AUTO_MS to 0 to make it wait for the button.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
const int BUTTON_PIN = 2;
const int HEART_PIN = 5;
const unsigned long AUTO_MS = 20000UL;
LiquidCrystal_I2C lcd(0x27, 16, 2);
const uint8_t HEART[8] = {0b00000, 0b01010, 0b11111, 0b11111, 0b01110, 0b00100, 0b00000, 0b00000};
const char *MESSAGES[][2] = {
{"Happy Mother's", "Day!"},
{"Thank you for", "everything"},
{"You make every", "day better"},
{"Best hugs in", "the world"},
{"I love you", "to the moon"},
{"Love always,", "for you, Mom"},
};
const int COUNT = sizeof(MESSAGES) / sizeof(MESSAGES[0]);
int shown = 0;
unsigned long shownAt = 0;
void show(int n) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(MESSAGES[n][0]);
lcd.setCursor(0, 1);
lcd.print(MESSAGES[n][1]);
lcd.setCursor(15, 1);
lcd.write(0);
shownAt = millis();
Serial.print("Message ");
Serial.print(n + 1);
Serial.print(": ");
Serial.print(MESSAGES[n][0]);
Serial.print(" ");
Serial.println(MESSAGES[n][1]);
// The heart glows up and fades.
for (int v = 0; v < 256; v += 8) {
analogWrite(HEART_PIN, v);
delay(8);
}
for (int v = 255; v >= 0; v -= 4) {
analogWrite(HEART_PIN, v);
delay(8);
}
}
void setup() {
Serial.begin(9600);
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(HEART_PIN, OUTPUT);
lcd.init();
lcd.backlight();
lcd.createChar(0, HEART);
show(0);
}
void loop() {
bool pressed = digitalRead(BUTTON_PIN) == LOW;
bool timeUp = AUTO_MS > 0 && millis() - shownAt >= AUTO_MS;
if (pressed || timeUp) {
shown = (shown + 1) % COUNT;
show(shown);
while (digitalRead(BUTTON_PIN) == LOW) delay(10);
}
delay(10);
}
Code walkthrough
The key functions of the sketch, and what each one does.
show sketch.ino
Clears the screen, prints the two lines of the chosen message, then draws the heart custom character at column 15 of the second row. It records the time in shownAt and prints the message to serial. Finally it fades the LED up and then down.
void show(int n) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(MESSAGES[n][0]);
lcd.setCursor(0, 1);
lcd.print(MESSAGES[n][1]);
lcd.setCursor(15, 1);
lcd.write(0);
shownAt = millis();
Serial.print("Message ");
Serial.print(n + 1);
Serial.print(": ");
Serial.print(MESSAGES[n][0]);
Serial.print(" ");
Serial.println(MESSAGES[n][1]);
// The heart glows up and fades.
for (int v = 0; v < 256; v += 8) {
analogWrite(HEART_PIN, v);
delay(8);
}
for (int v = 255; v >= 0; v -= 4) {
analogWrite(HEART_PIN, v);
delay(8);
}
}setup sketch.ino
Sets the button to INPUT_PULLUP and the LED pin as an output. It starts the LCD, turns on the backlight, builds the heart with createChar and shows message 0.
void setup() {
Serial.begin(9600);
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(HEART_PIN, OUTPUT);
lcd.init();
lcd.backlight();
lcd.createChar(0, HEART);
show(0);
}loop sketch.ino
Checks whether the button is pressed or AUTO_MS has passed. If so, it moves to the next message with (shown + 1) % COUNT and shows it. Then it waits for the button to be released so one press moves one step.
void loop() {
bool pressed = digitalRead(BUTTON_PIN) == LOW;
bool timeUp = AUTO_MS > 0 && millis() - shownAt >= AUTO_MS;
if (pressed || timeUp) {
shown = (shown + 1) % COUNT;
show(shown);
while (digitalRead(BUTTON_PIN) == LOW) delay(10);
}
delay(10);
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The screen is lit but shows no text This project
The sketch expects the backpack at address 0x27. Check SDA on A4 and SCL on A5. - A message is cut off This project
Each line holds only 16 characters. Shorten it inMESSAGES. - The screen changes without a press This project
That is the auto advance. SetAUTO_MSto 0 to make it wait for the button. - 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. - A pull-up or pull-down resistor is missing, so the input floats and reads random values. Resistor
Tie the input to 5 V or ground through about 10k. Or use INPUT_PULLUP and wire the switch to ground. - The LED does not light, or it lights once and then dies. LED
LEDs have polarity. The longer leg (anode) goes toward the positive side. Always add a series resistor, about 220 ohms to 1 k ohm on a 5 V pin.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.