Built-in project · Arduino Uno R3

Electronic dice on an Arduino Uno

A button rolls a die and an Uno tumbles the number onto a seven-segment display. The whole build, an Arduino Uno and 3 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.

IntermediateRuns in your browser. Free, and no account needed.

Step by step, with the wiring and the common mistakes: Arduino 7 Segment Display: an Electronic Die

Electronic dice · Arduino Uno R3live0.000 s 0.00x
Hold the button
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

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: press the button, watch it tumble, read the number.
//
//   segment a b c d e f g  ->  pins 2 3 4 5 6 7 8
//   the common cathode     ->  220 ohm to GND
//   pin 9                  ->  the roll button, to GND, internal pull-up
//
// The display is common cathode, so a segment lights when its pin goes HIGH
// and all the return current leaves through the one resistor on the common
// pin. That is the cheap way to wire one: the more segments are lit the less
// current each gets, so an 8 is dimmer than a 1. Seven resistors, one per
// segment, is the right way.
//
// SEGMENTS[] is the usual seven-segment font, one bit per segment, bit 0 = a.
// The tumble is just the roll repeated with a delay that grows each step, which
// reads as the die slowing down and settling.

const int SEG_PINS[] = {2, 3, 4, 5, 6, 7, 8};
const int BUTTON = 9;

// bit 0 a, 1 b, 2 c, 3 d, 4 e, 5 f, 6 g
const uint8_t DIGITS[] = {
    0x3F,  // 0
    0x06,  // 1
    0x5B,  // 2
    0x4F,  // 3
    0x66,  // 4
    0x6D,  // 5
    0x7D,  // 6
};

static void show(uint8_t mask) {
  for (int i = 0; i < 7; i++) {
    digitalWrite(SEG_PINS[i], (mask >> i) & 1 ? HIGH : LOW);
  }
}

static bool pressed() { return digitalRead(BUTTON) == LOW; }

void setup() {
  for (int i = 0; i < 7; i++) {
    pinMode(SEG_PINS[i], OUTPUT);
  }
  pinMode(BUTTON, INPUT_PULLUP);
  Serial.begin(115200);
  Serial.println("Mokxi Arduino Uno: press the button to roll");
  show(DIGITS[6]);
}

void loop() {
  if (!pressed()) {
    delay(10);
    return;
  }

  int face = 1;
  unsigned long step = 40;
  for (int i = 0; i < 14; i++) {
    face = (int)random(1, 7);
    show(DIGITS[face]);
    delay(step);
    step += step / 4 + 5;
  }

  Serial.print("rolled ");
  Serial.println(face);

  while (pressed()) {
    delay(10);
  }
}

Parts list

5 parts, plus the jumper wires. Every one is in the editor's parts bin.

How it is wired

10 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.

  • Ground: Arduino Uno R3 pin GND; Resistor, 220 Ω pin 2; Pushbutton pin 2a
  • Arduino Uno R3 pin 9; Pushbutton pin 1a
  • Arduino Uno R3 pin 8; Seven segment display pin G
  • Arduino Uno R3 pin 7; Seven segment display pin F
  • Arduino Uno R3 pin 6; Seven segment display pin E
  • Arduino Uno R3 pin 5; Seven segment display pin D
  • Arduino Uno R3 pin 4; Seven segment display pin C
  • Arduino Uno R3 pin 3; Seven segment display pin B
  • Arduino Uno R3 pin 2; Seven segment display pin A
  • Seven segment display pin COM1; Resistor, 220 Ω pin 1

Change it and keep it

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