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 About 25 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $33. On Mokxi they're free.
What you'll learn
- Shifting a byte out with
shiftOut - Using a shift register to save pins
- Seven segment patterns stored as bytes
- A button with an external pull-down resistor
- Difficulty: Intermediate
- Time: About 25 minutes
- Board: Arduino Uno R3
- Parts: 7
How it works
A 74HC595 shift register drives a seven segment digit from three Uno pins: DS on 11, SH_CP on 8 and ST_CP on 12. The sketch sends one byte for the digit, and the register lights the right segments on Q0 to Q6.
With no press, the digit flicks through random numbers from 1 to 6 every 60 ms. Press the button on pin 2 and each step gets slower, until the digit stops on a roll and the sketch prints it. The Uno's LED on pin 13 is lit while it settles.
Parts list
7 parts, plus the jumper wires. They retail for about $33; on Mokxi they're free.
- 1 × Arduino Uno R3In Starter Kit, Claw Machine Kit, Vending Machine Kit.
- 1 × Full-size breadboard
- 1 × 74HC595 shift registerIn LED Cube Kit.
- 1 × Seven segment displayIn Display Kit.
- 1 × Resistor, 220 ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 1 × PushbuttonIn Starter Kit, Christmas Lights Kit, Logic Lab Kit.
- 1 × Resistor, 10k ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
Kits that can build it
These 8 starter kits have 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 pin | Connects to |
|---|---|
| 74HC595 shift register pin GND | Arduino Uno R3 pin GND |
| 74HC595 shift register pin OE | Arduino Uno R3 pin GND |
| Resistor, 220 Ω pin 2 | Arduino Uno R3 pin GND |
| Resistor, 10k Ω pin 1 | Arduino Uno R3 pin GND |
| 74HC595 shift register pin STCP | Arduino Uno R3 pin 12 |
| 74HC595 shift register pin DS | Arduino Uno R3 pin 11 |
| 74HC595 shift register pin SHCP | Arduino Uno R3 pin 8 |
| Pushbutton pin 2a | Arduino Uno R3 pin 2 |
| 74HC595 shift register pin MR | Arduino Uno R3 pin 5V |
| 74HC595 shift register pin VCC | Arduino Uno R3 pin 5V |
| Pushbutton pin 1a | Arduino Uno R3 pin 5V |
| Seven segment display pin B | 74HC595 shift register pin Q1 |
| Seven segment display pin C | 74HC595 shift register pin Q2 |
| Seven segment display pin D | 74HC595 shift register pin Q3 |
| Seven segment display pin E | 74HC595 shift register pin Q4 |
| Seven segment display pin F | 74HC595 shift register pin Q5 |
| Seven segment display pin G | 74HC595 shift register pin Q6 |
| Seven segment display pin A | 74HC595 shift register pin Q0 |
| Resistor, 220 Ω pin 1 | Seven segment display pin COM1 |
| Resistor, 10k Ω pin 2 | Pushbutton pin 2b |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// 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);
}
}
Code walkthrough
The key functions of the sketch, and what each one does.
show sketch.ino
Shows one number. It pulls the latch pin ST_CP low, shifts out the segment pattern for that number with shiftOut, then raises the latch. The 595 only changes its outputs when the latch goes high.
void show(int n) {
digitalWrite(STCP, LOW);
shiftOut(DS, SHCP, MSBFIRST, PATTERNS[n]);
digitalWrite(STCP, HIGH);
}setup sketch.ino
Sets the shift register pins, the button and the pin 13 LED. It seeds the random generator from a floating pin A0 so rolls differ each time. It then opens the serial port and prints a hello message.
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.");
}loop sketch.ino
Shows a random 1 to 6 every 60 ms. When the button reads HIGH, it steps through rolls with a growing delay from 60 to about 400 ms, prints the final number, waits for release and pauses 1.5 s.
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);
}
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The button seems stuck or always rolls This project
The button reads HIGH when pressed. It needs the 10k resistor from pin 2 to GND so it reads LOW otherwise. - The wrong segments light This project
Check Q0 to Q6 go to segments A to G in order.PATTERNSassumes that order. - The display is blank This project
Check the 595's OE is on GND and MR is on 5V, and the display's common goes through the 220 ohm resistor to GND. - Outputs show garbage, or the register never clears. 74HC595 shift register
Tie OE low and SRCLR (pin 10) high. Left floating, these pins make the chip behave randomly. Then pulse the latch pin after shifting the byte. - The bits come out in the wrong order, or LEDs are dim. 74HC595 shift register
Choose MSBFIRST or LSBFIRST in shiftOut to flip the order. Give each LED its own resistor, since each output can source only about 6 mA safely. - Segments stay dark, or light when they should be off. Seven segment display
Check whether the display is common cathode or common anode. Common cathode: common pin to GND, drive segments HIGH. Common anode: common pin to VCC, drive segments LOW. - One segment is very bright or the LEDs burn out. Seven segment display
Each segment is an LED, so it needs its own resistor, about 220 ohm to 330 ohm on 5 V. One resistor on the common pin makes brightness change with the number of lit segments. - 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.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.