Tutorial 19: Arduino Dice

Created on: 6 August 2012
Updated on: 31 January 2023

In this tutorial you will build an Arduino controlled electronic dice (or die rather) that is shaken by holding a push button switch in and thrown by releasing the push button switch. The shake, throw and number thrown are animated and displayed on a seven segment display.

A 74HC595 IC is used to interface the 7-segment display to the Arduino, using only 3 Arduino digital pins. An additional pin is used to connect the push button switch to the Arduino.

The following video shows the Arduino dice project in action.


Know how to use a DIP IC, e.g. from tutorial 17 – Electronic Dice. Read about seven segment displays.

Arduino Dice Circuit Components

Besides an Arduino Uno or compatible board, USB cable, wire links and a breadboard, you will need:

Qty Part Designator Notes Type
1 10k resistor (brown - black - orange) R9 1/4W 5% or better Resistors
8 470 Ω resistors (yellow - violet - brown) R1 to R8
1 100n capacitor C1 Non-polorized capacitor Capacitor
1 74HC595 U1 74HC595 IC (16 pin DIP) Semiconductors
1 Common cathode 7-segment display S1 Common cathode 7-segment display, e.g. DMR14C from SunLED, or similar
1 Push button switch SW1 Can also use a wire link to simulate a switch Switch

Arduino Dice Circuit Diagram

The circuit diagram is shown below. The seven segment display could have been directly interfaced to the Arduino, but by using the 74HC595, only 3 Arduino pins are used.

Find more information about interfacing the 74HC595 IC to the Arduino in the Serial to Parallel Shifting-Out with a 74HC595 article from the Arduino website. The circuit diagram uses the same Arduino pins as this article and the switch is wired the same as the Arduino Button example.

Arduino dice circuit diagram
Arduino Dice Circuit Diagram – click for a bigger image
DMR14C Common Cathode 7-segment Display
DMR14C Common Cathode 7-segment Display

Building the Arduino Dice Circuit

The completed Arduino dice breadboard circuit is shown below. Click the picture for a bigger image.

The complete Arduino dice circuit on breadboard
Arduino Dice Circuit on a Breadboard

The suggested sequence for building the circuit is:

  1. Insert the 74HC595 IC into the breadboard and hook up its power and ground pins to the top and bottom breadboard rails.
  2. Insert and wire up capacitor C1 (100n).
  3. Insert the 7-segment display.
  4. Insert the 470 ohm resistors.
  5. Wire the resistors to the 7-segment display.
  6. Wire the resistors to the 74HC595 IC.
  7. Wire pin 10 of the IC to 5V.
  8. Wire pin 13 of the IC to GND.
  9. Connect the push button switch and R9.
  10. Wire the switch to the Arduino.
  11. Wire the 74HC595 to the Arduino

Programming the Arduino Dice Project

Download the Arduino_Dice sketch or copy it from the listing below. Afterwards, load it to the Arduino.

  Program:      Arduino_Dice

  Description:  Simulates a dice being thrown. Press a button
                to shake the dice, release the button to throw
                the dice. After dice settles, the number that
                was thrown will be shown.
  Hardware:     Uses a 74HC595 serial-in parallel-out IC to
                interface a common cathode seven segment
                display to the Arduino. Uses Arduino pins
                8, 11 and 12.
                A push button switch is interfaced to
                Arduino pin 2.
  References:   Uses pin numbers, interface and variable
                names from:
                Thanks to the authors of the above projects.

  Date:         26 April 2012
  Author:       W.A. Smith, http://startingelectronics.com

// Arduino pins used to interface to 74HC595
const int latchPin = 8;
const int clockPin = 12;
const int dataPin = 11;
// Arduino pin interfaced to switch
const int buttonPin = 2;

// 1 to 6 and DP (decimal point) on 7-seg display
unsigned char lookup_7seg[] = {0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x80};
// shaking dice pattern on 7-seg display
unsigned char shake_dice[] = {0x63, 0x5C};
// rolling dice on 7-seg display
unsigned char roll_dice[] = {0x1C, 0x58, 0x54, 0x4C};
int rand_seed;                     // used for variable dice throw duration
int rand_num = 0;                  // random number generated
unsigned char shake_toggle = 0;    // for shaking dice animation
int index = 0;                     // for rolling dice animation
int shake_speed;                   // accelerates dice shake speed

void setup() {
  // output pins for controlling the 74HC595
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
  // pin for reading button state
  pinMode(buttonPin, INPUT);
  // display DP on seven-seg display at startup
  digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, MSBFIRST, lookup_7seg[6]);
  digitalWrite(latchPin, HIGH);
  // generate random seed

void loop() {
  if (digitalRead(buttonPin)) {
    shake_speed = 150;                   // reset dice shaking speed
    delay(30);                           // debounce switch
    // generate number to use for random seed and show animated shaking dice
    while (digitalRead(buttonPin)) {
      rand_seed++;                       // for generating random number
      // animate shaking dice
      if (shake_toggle) {
        AnimateDice(0, shake_dice);
        shake_toggle = 0;
      else {
        AnimateDice(1, shake_dice);
        shake_toggle = 1;
      delay(80 + shake_speed);          // accelerating animation speed
      if (shake_speed > 0) {
        shake_speed -= 10;
    // animate rolling dice
    for (int rolls = 0; rolls < (rand_seed % 10 + 14); rolls++) {
      AnimateDice(index, roll_dice);
      delay((1 + rolls) * 20);
      if (index > 3) {
        index = 0;

    // generate and display number thrown on dice
    rand_num = random(0, 6);

// displays the dice number on the 7-seg display
void DiceNumber(unsigned char num)
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, MSBFIRST, lookup_7seg[num]);
    digitalWrite(latchPin, HIGH);

// displays one frame of the shaking or rolling dice animation
void AnimateDice(int seg, unsigned char *table)
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, MSBFIRST, table[seg]);
    digitalWrite(latchPin, HIGH);

Operating the Circuit

Press and hold the push button to shake the dice. Release the button to throw the dice. After the dice settles, the number that was thrown is displayed.