Freedom Sale
Independence Day Special — Unlock the AI Path 70% off our most popular AI course · Limited time offer
--Days
--Hrs
--Min
--Sec
Claim Your Discount
✦ Beginner ⏱ 20 min

🎲 Build a Digital Dice with Arduino

🎯 What You'll Build

A digital dice with a 7-segment display, shake animation, and multiple dice modes (D6, D12, D20) — triggered by a push button.

📋 What You'll Need

1

Control a 7-segment display

A 7-segment display has 7 LED segments (a–g). We control each segment to form digits 0–9.

// 7-segment pin mapping (common cathode display)
// Segments: a=2, b=3, c=4, d=5, e=6, f=7, g=8, DP=9
const int SEG[] = {2,3,4,5,6,7,8};

// Digit encoding: which segments are ON for each digit 0-9
// Segments:    a  b  c  d  e  f  g
const bool DIGITS[10][7] = {
  {1,1,1,1,1,1,0},  // 0
  {0,1,1,0,0,0,0},  // 1
  {1,1,0,1,1,0,1},  // 2
  {1,1,1,1,0,0,1},  // 3
  {0,1,1,0,0,1,1},  // 4
  {1,0,1,1,0,1,1},  // 5
  {1,0,1,1,1,1,1},  // 6
  {1,1,1,0,0,0,0},  // 7
  {1,1,1,1,1,1,1},  // 8
  {1,1,1,1,0,1,1},  // 9
};

void setup() {
  for (int i = 0; i < 7; i++) pinMode(SEG[i], OUTPUT);
}

void showDigit(int n) {
  if (n < 0 || n > 9) return;
  for (int i = 0; i < 7; i++)
    digitalWrite(SEG[i], DIGITS[n][i]);
}

void loop() {
  for (int d = 0; d <= 9; d++) { showDigit(d); delay(500); }
}
2

Add a roll button and animation

Press the button to roll. The display shuffles through random numbers before landing.

const int BUTTON   = 12;
const int MAX_FACE = 6;  // D6 — change to 12 for D12, 20 for D20

void showDash() {
  for (int i = 0; i < 7; i++) digitalWrite(SEG[i], i == 6 ? HIGH : LOW); // only segment g
}

int rollDice() {
  return random(1, MAX_FACE + 1);
}

void animateRoll() {
  // Shuffle animation: rapidly show random digits
  int shuffleCount = random(10, 20);
  for (int i = 0; i < shuffleCount; i++) {
    showDigit(random(1, 10));
    delay(50 + i * 8);  // gradually slow down
  }
}

void setup() {
  randomSeed(analogRead(A5));  // seed from floating pin
  for (int i = 0; i < 7; i++) pinMode(SEG[i], OUTPUT);
  pinMode(BUTTON, INPUT);
  showDash();
  Serial.begin(9600);
}

void loop() {
  if (digitalRead(BUTTON) == HIGH) {
    delay(50);  // debounce
    if (digitalRead(BUTTON) == HIGH) {
      animateRoll();
      int result = rollDice();
      if (result <= 9) {
        showDigit(result);
      } else {
        // Flash two-digit result for D12/D20
        for (int flash = 0; flash < 6; flash++) {
          showDigit(result / 10); delay(300);
          showDigit(result % 10); delay(300);
        }
        showDigit(result % 10);  // leave last digit showing
      }
      Serial.print("Rolled: "); Serial.println(result);
      while (digitalRead(BUTTON) == HIGH); // wait for release
    }
  }
}
💡 Tip: analogRead(A5) on a floating (unconnected) analog pin reads electrical noise — this makes a good random seed for randomSeed(). Without seeding, random() produces the same sequence every time the Arduino resets.

🎉 You Did It!

A satisfying physical gadget in 50 lines of code. The same button-debounce + display pattern is used in digital scoreboards, combination locks, and number entry keypads.

Found something wrong?

Spotted a bug, broken code, or something that doesn't look right? Tell us what's off and we'll fix it.