Built-in project · Arduino Uno R3

Pot-of-gold treasure box on an Arduino Uno

Type the lucky code on a 4x4 keypad and a servo unlocks the treasure box, a ring of LEDs shimmers gold inside and a piezo plays a tune. A wrong code just grumbles. 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.

Intermediate About 30 minutes to build and run. Runs in your browser. Free, and no account needed.

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

Pot-of-gold treasure box · Arduino Uno R3live0.000 s 0.00x
Click to open it in the editor
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

What you'll learn

  • Scanning a keypad with the Keypad library
  • Comparing typed text to a secret code
  • Controlling a servo latch
  • Driving addressable RGB LEDs
  • Guarding a buffer from overflow
  • Difficulty: Intermediate
  • Time: About 30 minutes
  • Board: Arduino Uno R3
  • Parts: 7

How it works

A 4x4 keypad takes the lucky code. Type 317 and then #, and the servo on pin 10 swings from 0 to 90 degrees. A ring of 12 LEDs shimmers gold for 5 seconds while the piezo plays a short tune. Then the servo locks again.

The * key clears what you typed. A wrong code gives a low buzz and three red flashes. Every key press also makes a short click on the piezo.

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
Piezo speaker pin 1Arduino Uno R3 pin 12
Resistor, 330 Ω pin 1Arduino Uno R3 pin 11
Hobby servo pin PWMArduino Uno R3 pin 10
Keypad, 4x4 pin R1Arduino Uno R3 pin 9
Keypad, 4x4 pin R2Arduino Uno R3 pin 8
Keypad, 4x4 pin R3Arduino Uno R3 pin 7
Keypad, 4x4 pin R4Arduino Uno R3 pin 6
Keypad, 4x4 pin C1Arduino Uno R3 pin 5
Keypad, 4x4 pin C2Arduino Uno R3 pin 4
Keypad, 4x4 pin C3Arduino Uno R3 pin 3
Keypad, 4x4 pin C4Arduino Uno R3 pin 2
Addressable RGB LEDs pin VDDArduino Uno R3 pin 5V
Hobby servo pin VCCArduino Uno R3 pin 5V
Addressable RGB LEDs pin VSSArduino Uno R3 pin GND
Piezo speaker pin 2Arduino Uno R3 pin GND
Hobby servo pin GNDArduino Uno R3 pin GND
Addressable RGB LEDs pin DINResistor, 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.

sketch.ino · Arduino Uno R3
// Pot-of-gold treasure box: a box that only opens for the lucky code. Type it
// on the keypad and the lid unlocks, the gold inside lights up and a little
// tune plays. Get it wrong and it grumbles.
//
//   keypad    rows -> pins 9, 8, 7, 6, columns -> pins 5, 4, 3, 2
//   latch     servo PWM -> pin 10, VCC -> 5 V, GND -> GND
//   gold      12 WS2812B LEDs in a ring, DIN <- 330 ohms <- pin 11
//   piezo     pin 12 -> piezo -> GND
//
// The code is 317, for March 17th, then # to try it. * clears what you have
// typed. The sketch keeps up to eight keys, so a long guess cannot run off
// the end of the buffer.
//
// The Keypad library scans the 16 keys by driving one row at a time and
// reading the four columns, so 16 keys need only 8 pins.
//
// Make it a hunt: hide clues to the code around the house, and fill the box
// with chocolate coins. A servo makes a good latch: its horn turns across the
// lid's edge to lock it and swings clear to open.
//
// Things to try. Type 3 1 7 #. Try a wrong code. Change CODE to your own.

#include <Keypad.h>
#include <Servo.h>
#include <Adafruit_NeoPixel.h>

const char CODE[] = "317";
const int LATCH_PIN = 10, GOLD_PIN = 11, PIEZO_PIN = 12;
const int LOCKED = 0, UNLOCKED = 90;
const int LEDS = 12;

const byte ROWS = 4, COLS = 4;
char keys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'},
};
byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};
Keypad pad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
Servo latch;
Adafruit_NeoPixel gold(LEDS, GOLD_PIN, NEO_GRB + NEO_KHZ800);

char typed[9];
int count = 0;

void shimmer(unsigned long ms) {
  unsigned long until = millis() + ms;
  while (millis() < until) {
    for (int i = 0; i < LEDS; i++) {
      int v = random(0, 8) == 0 ? 255 : random(120, 200);
      gold.setPixelColor(i, v, v * 3 / 5, 0);
    }
    gold.show();
    delay(40);
  }
  gold.clear();
  gold.show();
}

void open() {
  Serial.println("Lucky! The pot of gold is open");
  latch.write(UNLOCKED);
  const int tune[] = {659, 784, 880, 1047, 880, 1047};
  for (int i = 0; i < 6; i++) {
    tone(PIEZO_PIN, tune[i], 120);
    delay(150);
  }
  shimmer(5000);
  latch.write(LOCKED);
  Serial.println("Locked again");
}

void wrong() {
  Serial.println("Not this time, try again");
  tone(PIEZO_PIN, 160, 400);
  for (int i = 0; i < 3; i++) {
    gold.fill(gold.Color(40, 0, 0));
    gold.show();
    delay(120);
    gold.clear();
    gold.show();
    delay(120);
  }
}

void setup() {
  Serial.begin(9600);
  latch.attach(LATCH_PIN);
  latch.write(LOCKED);
  gold.begin();
  gold.setBrightness(180);
  gold.show();
  Serial.println("Treasure box locked: type the lucky code, then #");
}

void loop() {
  char key = pad.getKey();
  if (!key) return;
  tone(PIEZO_PIN, 1200, 30);
  if (key == '*') {
    count = 0;
    Serial.println("Cleared");
  } else if (key == '#') {
    typed[count] = 0;
    if (strcmp(typed, CODE) == 0) open();
    else wrong();
    count = 0;
  } else if (count < 8) {
    typed[count++] = key;
    Serial.print("Key ");
    Serial.println(key);
  }
}

Code walkthrough

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

open sketch.ino

Runs the success routine. It moves the servo to UNLOCKED, plays six notes from the tune array, shimmers the LEDs for five seconds, then returns the servo to LOCKED.

void open() {
  Serial.println("Lucky! The pot of gold is open");
  latch.write(UNLOCKED);
  const int tune[] = {659, 784, 880, 1047, 880, 1047};
  for (int i = 0; i < 6; i++) {
    tone(PIEZO_PIN, tune[i], 120);
    delay(150);
  }
  shimmer(5000);
  latch.write(LOCKED);
  Serial.println("Locked again");
}

wrong sketch.ino

Plays a low 160 Hz tone and flashes the whole ring dim red three times. It is the only feedback for a bad code.

void wrong() {
  Serial.println("Not this time, try again");
  tone(PIEZO_PIN, 160, 400);
  for (int i = 0; i < 3; i++) {
    gold.fill(gold.Color(40, 0, 0));
    gold.show();
    delay(120);
    gold.clear();
    gold.show();
    delay(120);
  }
}

shimmer sketch.ino

For a set number of milliseconds, gives each LED a random gold brightness every 40 ms. Occasionally one is full bright. It then turns the ring off.

void shimmer(unsigned long ms) {
  unsigned long until = millis() + ms;
  while (millis() < until) {
    for (int i = 0; i < LEDS; i++) {
      int v = random(0, 8) == 0 ? 255 : random(120, 200);
      gold.setPixelColor(i, v, v * 3 / 5, 0);
    }
    gold.show();
    delay(40);
  }
  gold.clear();
  gold.show();
}

loop sketch.ino

Reads one key at a time. * resets the count, # ends the guess and compares it with CODE, and other keys are stored up to eight characters.

void loop() {
  char key = pad.getKey();
  if (!key) return;
  tone(PIEZO_PIN, 1200, 30);
  if (key == '*') {
    count = 0;
    Serial.println("Cleared");
  } else if (key == '#') {
    typed[count] = 0;
    if (strcmp(typed, CODE) == 0) open();
    else wrong();
    count = 0;
  } else if (count < 8) {
    typed[count++] = key;
    Serial.print("Key ");
    Serial.println(key);
  }
}

Troubleshooting

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

  • Nothing happens when I press keys. This project
    Rows go to pins 9 to 6 and columns to pins 5 to 2, in order. A swapped pair types the wrong keys.
  • The right code is rejected. This project
    Press # after the digits. To use your own code, change CODE.
  • The LEDs stay dark. This project
    DIN gets pin 11 through the 330 ohm resistor, with 5 V and GND on the ring.
  • Pressing one key reports several keys, or the wrong key. Keypad
    Match the keymap and the row and column pin order to your keypad's pinout. Without diodes, pressing three keys at once can ghost a fourth.
  • No keys are detected. Keypad
    Rows need pull-ups, or use INPUT_PULLUP, while one column is driven low at a time. The Keypad library does this for you if you pass the pins in the right order.
  • 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 first LED flickers, shows wrong colors, or the whole strip glitches. Addressable RGB LEDs
    Join the grounds of the strip and the controller. Add a 300 to 500 ohm resistor in series with the data line. Put a 1000 uF capacitor across the strip's 5 V and GND.

Change it and keep it

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