Built-in project · Arduino Uno R3

Digital dice on a 74HC595 (pins 11, 8 and 12), on an Arduino Uno

A seven-segment digit on a 74HC595 (DS 11, SH_CP 8, ST_CP 12) spins through the numbers; press the button on pin 2 and it slows down and stops on a roll from 1 to 6. 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 Runs in your browser. Free, and no account needed.

Digital dice on a 74HC595 (pins 11, 8 and 12) · Arduino Uno R3live0.000 s 0.00x
Press the button to roll
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
// Digital dice: the digit spins, press the button and it stops on a roll.
//
//   74HC595 DS -> pin 11   SH_CP -> pin 8   ST_CP -> pin 12
//   Q0 to Q6 -> segments a to g; the common cathode -> 220 ohm -> GND
//   button: one side -> 5V, the other -> pin 2 and 10k to GND
//   the Uno's own LED on pin 13 lights while the dice settles
//
// The digit flicks through 1 to 6 quickly. A press makes each step a little
// slower until it stops; random() picks every number, so the stop is fair.

const int DS = 11;
const int SHCP = 8;
const int STCP = 12;
const int BUTTON = 2;
const int LED = 13;

//                      gfedcba
const byte PATTERNS[7] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D};

void show(int n) {
  digitalWrite(STCP, LOW);
  shiftOut(DS, SHCP, MSBFIRST, PATTERNS[n]);
  digitalWrite(STCP, HIGH);
}

void setup() {
  pinMode(DS, OUTPUT);
  pinMode(SHCP, OUTPUT);
  pinMode(STCP, OUTPUT);
  pinMode(BUTTON, INPUT);
  pinMode(LED, OUTPUT);
  randomSeed(analogRead(A0));
  Serial.begin(9600);
  Serial.println("Mokxi Uno: digital dice. Press the button on pin 2 to roll.");
}

void loop() {
  show(random(1, 7));
  delay(60);
  if (digitalRead(BUTTON) == HIGH) {
    digitalWrite(LED, HIGH);
    int n = 1;
    for (int wait = 60; wait < 400; wait += 40) {
      n = random(1, 7);
      show(n);
      delay(wait);
    }
    digitalWrite(LED, LOW);
    Serial.print("rolled a ");
    Serial.println(n);
    while (digitalRead(BUTTON) == HIGH) {
    }
    delay(1500);
  }
}

Parts list

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

How it is wired

15 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; 74HC595 shift register pin GND; 74HC595 shift register pin OE; Resistor, 220 Ω pin 2; Resistor, 10k Ω pin 1
  • Arduino Uno R3 pin 12; 74HC595 shift register pin STCP
  • Arduino Uno R3 pin 11; 74HC595 shift register pin DS
  • Arduino Uno R3 pin 8; 74HC595 shift register pin SHCP
  • Arduino Uno R3 pin 2; Pushbutton pin 2a
  • Arduino Uno R3 pin 5V; 74HC595 shift register pin MR; 74HC595 shift register pin VCC; Pushbutton pin 1a
  • 74HC595 shift register pin Q1; Seven segment display pin B
  • 74HC595 shift register pin Q2; Seven segment display pin C
  • 74HC595 shift register pin Q3; Seven segment display pin D
  • 74HC595 shift register pin Q4; Seven segment display pin E
  • 74HC595 shift register pin Q5; Seven segment display pin F
  • 74HC595 shift register pin Q6; Seven segment display pin G
  • 74HC595 shift register pin Q0; Seven segment display pin A
  • Seven segment display pin COM1; Resistor, 220 Ω pin 1
  • Pushbutton pin 2b; Resistor, 10k Ω pin 2

Change it and keep it

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