Build a Dice Simulator Using Arduino and Push Button – Full Guide with Code

Build a Dice Simulator Using Arduino and Push Button – Full Guide with Code
0
0
3 min read

Learn how to build a Dice Simulator using Arduino, LEDs, and a push button. Perfect beginner project with full code, circuit diagram, and detailed explanation.


🎯 Introduction

What is a Dice Simulator?

A Dice Simulator is a digital version of a traditional six-faced dice. Instead of rolling a physical cube, you press a button and get a random number between 1 to 6 displayed using LEDs or other visual output methods.

What This Project Does

In this project, you’ll build a dice simulator that displays numbers from 1 to 6 using LEDs. When the push button is pressed, the Arduino generates a random number and lights up the corresponding LEDs in a pattern similar to dice pips.

Real-World Use Case

While it may seem basic, such simulations are widely used in:

  • Board game companions
  • Digital game prototypes
  • Interactive learning kits
  • Embedded system project demos

Skill Level & Expected Outcome

Skill Level: Beginner
You Will Learn:

  • How to interface multiple LEDs
  • Use of random() function in Arduino
  • Button debouncing logic
  • Modular coding practices

🔩 Components Required

ComponentQuantityDescriptionBuy Link
Arduino Uno with cable1Main controller boardBuy on Elecsynergy
Push Button1To trigger the dice rollBuy on Elecsynergy
5mm Red LEDs7To display dice numbersBuy on Elecsynergy
220Ω Resistors7To limit current to LEDsBuy on Elecsynergy
Breadboard1For prototyping circuitBuy on Elecsynergy
Jumper WiresAs requiredFor connectionsBuy on Elecsynergy

📊 Circuit Diagram + Explanation

🛠 Diagram:

Wiring Guide:

  • LEDs are arranged in a 2×3 grid + center dot.
  • Connect all LED cathodes to GND via 220Ω resistors.
  • Connect LED anodes to digital pins D2–D8.
  • Push button connected to pin D9 with internal pull-up (INPUT_PULLUP).
LED PositionArduino Pin
Top LeftD2
Top RightD3
Middle LeftD4
CenterD5
Middle RightD6
Bottom LeftD7
Bottom RightD8
ButtonD9

🧾 Arduino Code

// Dice Simulator with Push Button and LEDs

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

void setup() {
  for (int i = 0; i < 7; i++) {
    pinMode(ledPins[i], OUTPUT);
    digitalWrite(ledPins[i], LOW);
  }
  pinMode(buttonPin, INPUT_PULLUP);
  Serial.begin(9600);
  randomSeed(analogRead(0)); // Random seed
}

void loop() {
  if (digitalRead(buttonPin) == LOW) {
    delay(200); // Debounce
    int dice = random(1, 7); // 1 to 6
    showDice(dice);
    Serial.print("Dice Rolled: ");
    Serial.println(dice);
  }
}

void showDice(int number) {
  for (int i = 0; i < 7; i++) {
    digitalWrite(ledPins[i], LOW);
  }

  switch (number) {
    case 1:
      digitalWrite(ledPins[4], HIGH); // center
      break;
    case 2:
      digitalWrite(ledPins[0], HIGH); // top left
      digitalWrite(ledPins[6], HIGH); // bottom right
      break;
    case 3:
      digitalWrite(ledPins[0], HIGH);
      digitalWrite(ledPins[4], HIGH);
      digitalWrite(ledPins[6], HIGH);
      break;
    case 4:
      digitalWrite(ledPins[0], HIGH);
      digitalWrite(ledPins[1], HIGH);
      digitalWrite(ledPins[5], HIGH);
      digitalWrite(ledPins[6], HIGH);
      break;
    case 5:
      digitalWrite(ledPins[0], HIGH);
      digitalWrite(ledPins[1], HIGH);
      digitalWrite(ledPins[4], HIGH);
      digitalWrite(ledPins[5], HIGH);
      digitalWrite(ledPins[6], HIGH);
      break;
    case 6:
      digitalWrite(ledPins[0], HIGH);
      digitalWrite(ledPins[1], HIGH);
      digitalWrite(ledPins[2], HIGH);
      digitalWrite(ledPins[5], HIGH);
      digitalWrite(ledPins[6], HIGH);
      digitalWrite(ledPins[3], HIGH);
      break;
  }
}

⚙️ Working Explanation

When you press the push button, the Arduino generates a random number between 1 and 6. Depending on this number, a specific pattern of LEDs lights up, simulating the pips of a traditional dice. The code includes debouncing logic to prevent accidental double-presses.

Each number corresponds to a different combination of LEDs, arranged as:

  • 1: Center only
  • 2: Top-left & bottom-right
  • 3: Two above + center
  • 4: All corners except center
  • 5: Four corners + center
  • 6: All except center

🖼️ Output Preview / Demo


🛠️ Troubleshooting & Tips

  • Issue: LEDs not lighting
    Fix: Check resistors and pin mappings
  • Issue: Dice number not changing
    Fix: Ensure randomSeed() is initialized
  • Issue: Button press not detected
    Fix: Ensure you’re using INPUT_PULLUP and wiring correctly
  • Use short delay after button press to prevent bouncing
  • Label LEDs on breadboard for easy pin tracking

💡 Project Expansion Ideas

Here are some exciting ways to take this project further:

  • Add a 7-segment display instead of LEDs
  • Use a buzzer to beep with each dice roll
  • Add OLED display to show number graphically
  • Introduce a rolling animation before final result
  • Convert it into a two-player board game counter

🔚 Conclusion

You’ve successfully built your own Arduino-powered Dice Simulator using basic electronics and programming logic. This project introduces critical embedded system concepts like random number generation, user input handling, and LED control—all wrapped up in a fun, engaging use case.

📣 If you liked this project:

  • Share it with your friends
  • Comment your version below
  • Subscribe to our YouTube Channel

🛍️ Buy Full Kit – Ready to Plug & Play

🎁 Want to skip the hassle of sourcing components?
Buy the Complete Dice Simulator Kit from Elecsynergy

Leave a Reply