Red envelope box on an Arduino Uno
Press the button and a servo opens the lid of a red envelope box, a lamp fades up inside and a 16x2 LCD shows a good wish for the new year. 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.
// Red envelope reveal box: a little box for a red envelope. Press the button
// and the lid swings open, a light comes on inside and the display shows a
// good wish for the new year.
//
// button pin 2 -> button -> GND (INPUT_PULLUP, pressed reads LOW)
// servo PWM -> pin 9, VCC -> 5 V, GND -> GND (the lid)
// lamp pin 5 -> 220 ohms -> yellow LED -> GND
// LCD 16x2 with an I2C backpack: SDA -> A4, SCL -> A5
//
// Red envelopes (hongbao in Mandarin, lai see in Cantonese, li xi in
// Vietnamese) are given at Lunar New Year with money inside and good wishes
// outside. Put one in the box, and the wish on the screen is the card.
//
// The wishes are in a list and one is picked at random each time. A random
// pick can repeat, so the sketch remembers the last one and moves on if it
// comes up again.
//
// Things to try. Press the button. Write your own wishes into WISHES, two
// lines of up to 16 letters each. Make the lid stay open longer with OPEN_MS.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
const int BUTTON_PIN = 2;
const int LAMP_PIN = 5;
const int SERVO_PIN = 9;
const int SHUT = 10, OPEN = 110;
const unsigned long OPEN_MS = 6000;
LiquidCrystal_I2C lcd(0x27, 16, 2);
Servo lid;
const char *WISHES[][2] = {
{"Happy Lunar", "New Year!"},
{"Xin nian", "kuai le!"},
{"Wishing you", "good health"},
{"Peace and joy", "all year long"},
{"May your year", "be bright"},
{"Lots of luck", "and laughter"},
};
const int COUNT = sizeof(WISHES) / sizeof(WISHES[0]);
int last = -1;
int opened = 0;
void idle() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("A red envelope");
lcd.setCursor(0, 1);
lcd.print("for you: press");
}
void swing(int from, int to) {
int step = from < to ? 5 : -5;
for (int a = from; a != to + step; a += step) {
lid.write(a);
analogWrite(LAMP_PIN, map(a, SHUT, OPEN, 0, 255));
delay(25);
}
}
void setup() {
Serial.begin(9600);
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(LAMP_PIN, OUTPUT);
lid.attach(SERVO_PIN);
lid.write(SHUT);
lcd.init();
lcd.backlight();
randomSeed(analogRead(A0));
idle();
Serial.println("Red envelope box ready");
}
void loop() {
if (digitalRead(BUTTON_PIN) == HIGH) return;
int pick = random(0, COUNT);
if (pick == last) pick = (pick + 1) % COUNT;
last = pick;
opened++;
// Open slowly, so the envelope is revealed rather than thrown.
swing(SHUT, OPEN);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(WISHES[pick][0]);
lcd.setCursor(0, 1);
lcd.print(WISHES[pick][1]);
Serial.print("Opened ");
Serial.print(opened);
Serial.print(": ");
Serial.print(WISHES[pick][0]);
Serial.print(" ");
Serial.println(WISHES[pick][1]);
delay(OPEN_MS);
swing(OPEN, SHUT);
digitalWrite(LAMP_PIN, LOW);
idle();
Serial.println("Lid shut");
while (digitalRead(BUTTON_PIN) == LOW) delay(10);
}
Parts list
7 parts, plus the jumper wires. Every one is in the editor's parts bin.
- 1 × Arduino Uno R3
- 1 × Half breadboard
- 1 × Pushbutton
- 1 × Resistor, 220 Ω
- 1 × LED, yellow
- 1 × Hobby servo
- 1 × Character LCD with I2C backpack
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 9; Hobby servo pin PWM
- Arduino Uno R3 pin 5; Resistor, 220 Ω pin 1
- Arduino Uno R3 pin 2; Pushbutton pin 1a
- Arduino Uno R3 pin 5V; Hobby servo pin VCC; Character LCD with I2C backpack pin VCC
- Ground: Arduino Uno R3 pin GND; Pushbutton pin 2a; LED, yellow pin C; Hobby servo pin GND; Character LCD with I2C backpack pin GND
- 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, 220 Ω pin 2; LED, yellow pin A
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.