Built-in project · Arduino Uno R3

Tilt dice on an 8x8 matrix, on an Arduino Uno

Shake the tilt switch on pin 2 and an 8x8 MAX7219 matrix (DIN 13, CLK 7, CS 8) rolls a die and settles on a number, while an RGB LED on pins 9, 6 and 5 shows when it has stopped. The whole build, an Arduino Uno and 6 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 $35. On Mokxi they're free.

Tilt dice on an 8x8 matrix · Arduino Uno R3live0.000 s 0.00x
Click the tilt switch a few times to shake it
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

  • Detecting shakes with a tilt switch
  • Pin change interrupts
  • Drawing pictures on a MAX7219 LED matrix
  • Random numbers with random and randomSeed
  • Slowing down an animation to a stop
  • Difficulty: Intermediate
  • Time: About 30 minutes
  • Board: Arduino Uno R3
  • Parts: 8

How it works

A tilt switch is a ball in a tube. When shaken, the ball bounces on the contacts, and pin 2 flickers between HIGH and LOW. An interrupt counts every flicker, so the sketch never has to watch the pin.

While flickers keep coming, the die on the 8x8 LED matrix keeps changing faces, fast. When they stop, it slows down a little each time and settles on a number. The RGB LED is blue while rolling and green once it stops.

The sketch rolls once at start-up, then again after each shake.

Parts list

8 parts, plus the jumper wires. They retail for about $35; on Mokxi they're free.

Kits that can build it

This starter kit has 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
LED matrix, 8x8 pin DINArduino Uno R3 pin 13
Resistor, 220 Ω (3) pin 1Arduino Uno R3 pin 9
LED matrix, 8x8 pin CSArduino Uno R3 pin 8
LED matrix, 8x8 pin CLKArduino Uno R3 pin 7
Resistor, 220 Ω (2) pin 1Arduino Uno R3 pin 6
Resistor, 220 Ω (1) pin 1Arduino Uno R3 pin 5
Tilt switch pin 2Arduino Uno R3 pin 2
LED matrix, 8x8 pin VCCArduino Uno R3 pin 5V
RGB LED pin CArduino Uno R3 pin GND
Tilt switch pin 1Arduino Uno R3 pin GND
LED matrix, 8x8 pin GNDArduino Uno R3 pin GND
Resistor, 220 Ω (3) pin 2RGB LED pin R
Resistor, 220 Ω (2) pin 2RGB LED pin G
Resistor, 220 Ω (1) pin 2RGB LED pin B

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
// Electronic dice: shake the tilt switch, watch the die roll and stop.
//
//   tilt switch -> pin 2 and GND (INPUT_PULLUP)
//   MAX7219 DIN -> pin 13    CLK -> pin 7    CS -> pin 8
//   RGB LED red -> pin 9, green -> pin 6, blue -> pin 5, each through 220 ohm
//
// A tilt switch is a ball in a tube. Shaken, the ball bounces on the contacts
// and the pin flickers between HIGH and LOW. An interrupt on pin 2 counts
// every flicker without the sketch having to watch for it. While flickers keep
// coming the die keeps rolling; when they stop, it slows down and settles on
// a number, and the RGB LED turns green.
//
// Click the tilt switch a few times to shake it.

#include <LedControl.h>

const int TILT = 2;
const int RED_PIN = 9;
const int GREEN_PIN = 6;
const int BLUE_PIN = 5;

LedControl lc = LedControl(13, 7, 8, 1);  // DIN, CLK, CS, modules

// One face per number, eight rows each.
const byte FACES[6][8] = {
    {0x00, 0x00, 0x00, 0x18, 0x18, 0x00, 0x00, 0x00},
    {0x00, 0x06, 0x06, 0x00, 0x00, 0x60, 0x60, 0x00},
    {0xC0, 0xC0, 0x00, 0x18, 0x18, 0x00, 0x03, 0x03},
    {0x00, 0x66, 0x66, 0x00, 0x00, 0x66, 0x66, 0x00},
    {0xC3, 0xC3, 0x00, 0x18, 0x18, 0x00, 0xC3, 0xC3},
    {0x66, 0x66, 0x00, 0x66, 0x66, 0x00, 0x66, 0x66},
};

volatile unsigned int shakes = 0;
unsigned int seen = 0;

void onShake() {
  shakes++;
}

void showFace(int n) {
  for (int row = 0; row < 8; row++) lc.setRow(0, row, FACES[n - 1][row]);
}

void color(int r, int g, int b) {
  analogWrite(RED_PIN, r);
  analogWrite(GREEN_PIN, g);
  analogWrite(BLUE_PIN, b);
}

void roll() {
  color(0, 0, 160);
  int n = 1;
  int wait = 40;
  while (wait < 300) {
    n = random(1, 7);
    showFace(n);
    delay(wait);
    if (shakes != seen) {
      seen = shakes;
      wait = 40;  // still shaking: keep rolling fast
    } else {
      wait += 30;
    }
  }
  color(0, 160, 0);
  Serial.print("rolled a ");
  Serial.println(n);
}

void setup() {
  pinMode(TILT, INPUT_PULLUP);
  pinMode(RED_PIN, OUTPUT);
  pinMode(GREEN_PIN, OUTPUT);
  pinMode(BLUE_PIN, OUTPUT);
  lc.shutdown(0, false);
  lc.setIntensity(0, 6);
  lc.clearDisplay(0);
  attachInterrupt(digitalPinToInterrupt(TILT), onShake, CHANGE);
  randomSeed(analogRead(A5));
  Serial.begin(9600);
  Serial.println("Mokxi Uno: tilt dice. Shake the tilt switch.");
  roll();
}

void loop() {
  if (shakes != seen) {
    seen = shakes;
    roll();
  }
}

Code walkthrough

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

onShake sketch.ino

The interrupt handler. Each time pin 2 changes, it adds one to shakes. That is all it does, which keeps it fast.

void onShake() {
  shakes++;
}

showFace sketch.ino

Draws a die face on the matrix by writing eight row bytes from FACES, one for each row of the display.

void showFace(int n) {
  for (int row = 0; row < 8; row++) lc.setRow(0, row, FACES[n - 1][row]);
}

roll sketch.ino

Turns the RGB LED blue, then picks and shows a random face each pass, starting with a 40 ms wait. If new shakes arrived, the wait resets to 40. If not, it grows by 30. Once it passes 300, the die stops, the LED turns green and the number is printed.

void roll() {
  color(0, 0, 160);
  int n = 1;
  int wait = 40;
  while (wait < 300) {
    n = random(1, 7);
    showFace(n);
    delay(wait);
    if (shakes != seen) {
      seen = shakes;
      wait = 40;  // still shaking: keep rolling fast
    } else {
      wait += 30;
    }
  }
  color(0, 160, 0);
  Serial.print("rolled a ");
  Serial.println(n);
}

loop sketch.ino

Waits for shakes to differ from seen. When it does, it updates seen and calls roll.

void loop() {
  if (shakes != seen) {
    seen = shakes;
    roll();
  }
}

Troubleshooting

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

  • The die never rolls This project
    The tilt switch goes between pin 2 and ground, and the pin uses INPUT_PULLUP. Check both wires.
  • The matrix stays dark This project
    Check DIN on pin 13, CLK on pin 7 and CS on pin 8, plus 5 V and ground.
  • The colors are wrong or one color never lights. RGB LED
    Check whether your LED is common cathode (shared leg to ground) or common anode (shared leg to 5 V). Common anode works backwards: LOW turns a color on.
  • One color is much brighter than the others, or the LED runs too hot. RGB LED
    Use one resistor per color leg, never one for the shared leg. Typical values are 220 ohm for red and 100 to 150 ohm for green and blue on 5 V.
  • 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 reading flickers when the switch is moved. Tilt switch
    The ball bounces on the contacts, giving many fast changes. Debounce in software by waiting 20 to 50 ms before accepting a new state, or average a few readings.
  • The input floats and never gives a clean reading. Tilt switch
    A tilt switch is just a switch, so the pin floats when it is open. Use INPUT_PULLUP with the switch between the pin and GND, or add a 10 k pull-down resistor.

Change it and keep it

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