Built-in project · Arduino Uno R3

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 About 25 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $37. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • Moving a hobby servo gradually, instead of in one jump
  • Tying a lamp's brightness to the servo angle with map
  • Picking a random item without repeating the last one
  • Showing text on an I2C LCD
  • Difficulty: Beginner
  • Time: About 25 minutes
  • Board: Arduino Uno R3
  • Parts: 7

How it works

The LCD first reads A red envelope for you: press. When you press the button on pin 2, the sketch picks a wish from a list of six. If it is the same one as last time, it moves to the next. Then the servo on pin 9 swings the lid from 10 to 110 degrees.

As the lid opens, a yellow LED on pin 5 fades up with it. The wish appears on the LCD and stays for 6 seconds. Then the lid closes, the lamp goes out, and the first message returns.

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 pinConnects to
Hobby servo pin PWMArduino Uno R3 pin 9
Resistor, 220 Ω pin 1Arduino Uno R3 pin 5
Pushbutton pin 1aArduino Uno R3 pin 2
Hobby servo pin VCCArduino Uno R3 pin 5V
Character LCD with I2C backpack pin VCCArduino Uno R3 pin 5V
Pushbutton pin 2aArduino Uno R3 pin GND
LED, yellow pin CArduino Uno R3 pin GND
Hobby servo pin GNDArduino Uno R3 pin GND
Character LCD with I2C backpack pin GNDArduino Uno R3 pin GND
Character LCD with I2C backpack pin SDAArduino Uno R3 pin A4
Character LCD with I2C backpack pin SCLArduino Uno R3 pin A5
LED, yellow pin AResistor, 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.

sketch.ino · Arduino Uno R3
// 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);
}

Code walkthrough

The key functions of the sketch, and what each one does.

idle sketch.ino

Clears the LCD and shows the resting message on both lines. It runs at startup and after the lid closes.

void idle() {
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("A red envelope");
  lcd.setCursor(0, 1);
  lcd.print("for you: press");
}

swing sketch.ino

Moves the servo from one angle to another in steps of 5 degrees, 25 ms apart. On each step it also sets the lamp with analogWrite, using map to turn the angle into a brightness from 0 to 255. So the lamp and the lid move together.

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);
  }
}

loop sketch.ino

Returns at once if the button is not pressed. On a press it picks a wish with random, avoiding a repeat, opens the lid with swing, and shows the wish. After OPEN_MS it closes the lid, turns off the lamp, and waits for the button to be released.

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);
  // ... 1 more lines in the editor

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • The lid moves the wrong way or hits something This project
    Change SHUT and OPEN to angles that fit your box. They are 10 and 110.
  • The button does nothing This project
    Check the button connects pin 2 to ground. The pin uses INPUT_PULLUP, so a press reads LOW.
  • 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.
  • A blue or white LED stays dark on 3.3 V. LED
    Blue and white LEDs drop about 3 V, so 3.3 V leaves almost nothing for the resistor. Use a 5 V supply, or a smaller resistor and a red or green LED.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.