Skip to Content

ELECTRONIC DICE

Rolling dice is a fundamental part of many games, but traditional dice can sometimes be inconvenient, whether they get lost, roll off the table, or simply wear out over time. This project presents an electronic alternative: a digital dice powered by an Arduino, a button, and a 7-segment display. With a simple press of a button, this electronic dice generates and displays a random number between 1 and 6, simulating a real dice roll without the need for physical dice.

By integrating a microcontroller, a push button, and a display, this system provides a fun, reliable, and interactive way to generate random numbers, making it perfect for board games, educational purposes, and creative electronics projects.

Components Required

  • Arduino Uno
  • 7-Segment Display (Common Cathode)
  • Push Button
  • 220Ω Resistors (x7)
  • Breadboard
  • Jumper Wires
  • 10KΩ Resistor (for pull-down configuration)

Circuit Diagram

Component Connections

1. Connecting the 7-Segment Display:

  • A → D2 on Arduino
  • B → D3 on Arduino
  • C → D4 on Arduino
  • D → D5 on Arduino 
  • E → D6 on Arduino
  • F → D7 on Arduino
  • G → D8 on Arduino
  • Common Cathode → GND

2. Connecting the Push Button:

  • One terminal → D9 on Arduino
  • Other terminal → GND (via 10KΩ pull-down resistor)
  • One terminal also connected to 5V

Installation and Setup

1. Mount the 7-Segment Display

  • Position the display on the breadboard and connect it to the appropriate Arduino pins with resistors.

2. Connect the Button

  • Set up the push button with a pull-down resistor to ensure stable input readings.

3. Power the System

  •  Use a USB connection or an external power source to run the Arduino.

4. Upload the Code

  • Open Arduino IDE, paste the provided co​de, and uplo​ad it to the board​.

CODE

const int buttonPin = 9;  // Push button pin

const int segmentPins[7] = {2, 3, 4, 5, 6, 7, 8};  // 7-segment display pins

const int diceNumbers[6][7] = {

  {1, 1, 1, 1, 1, 1, 0},  // 0 (not used)

  {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

};


void setup() {

  pinMode(buttonPin, INPUT_PULLUP);

  for (int i = 0; i < 7; i++) {

    pinMode(segmentPins[i], OUTPUT);

  }

}


void loop() {

  if (digitalRead(buttonPin) == LOW) {

    int diceRoll = random(1, 7);  // Generate a number between 1 and 6

    displayNumber(diceRoll);

    delay(500); // Debounce delay

  }

}


void displayNumber(int num) {

  for (int i = 0; i < 7; i++) {

    digitalWrite(segmentPins[i], diceNumbers[num][i]);

  }

}


CODE EXPLANATION

1. Pin Definitions for 7-Segment Display

int segmentPins[] = {2, 3, 4, 5, 6, 7, 8}; 

int buttonPin = 9;  // Push button input pin

int diceValue = 1;  // Initial dice value

  • segmentPins[]: This is an array that holds the Arduino pins to which the 7-segment display is connected. Each number (2, 3, 4, etc.) corresponds to a pin on the Arduino. We use these pins to control the segments of the display (which make up the numbers).
  • buttonPin: This is the pin where the push button is connected. When you press the button, it tells the Arduino to roll the dice.
  • diceValue: This variable keeps track of the current dice value (the number that will be displayed on the 7-segment display). Initially, it's set to 1.

2. 7-Segment Display Digits (Mapping Numbers)

byte digits[6] = {

  B1111110,  // 0

  B0110000,  // 1

  B1101101,  // 2

  B1111001,  // 3

  B0110011,  // 4

  B1011011,  // 5

  B1011111   // 6

};

  • This section defines how the numbers 0-6 will be displayed on the 7-segment display.
  • A 7-segment display has 7 segments (labeled a through g). We use B1111110, B0110000, and so on, as binary numbers to turn specific segments ON or OFF to show each number.
  • For example, B1111110 lights up the segments that form the number "0," while B0110000 lights up the segments for the number "1."

3.  Setup Function

void setup() {

  // Initialize the 7-segment display pins

  for (int i = 0; i < 7; i++) {

    pinMode(segmentPins[i], OUTPUT);

  }

  // Initialize the button pin

  pinMode(buttonPin, INPUT_PULLUP);

  randomSeed(analogRead(0));  // Seed the random number generator

}

  • setup(): This function runs once when you power up the Arduino or restart it. It’s used to set up everything before the main part of the code runs.
    • pinMode(segmentPins[i], OUTPUT): This loop sets each pin connected to the 7-segment display as an output, so we can control them (turn segments ON/OFF).
    • pinMode(buttonPin, INPUT_PULLUP): This sets the button pin as an input. The INPUT_PULLUP mode means the button will be "pulled high" by default (connected to 5V) and goes low (connected to ground) when pressed.
    • randomSeed(analogRead(0)): This is used to initialize the random number generator. It reads random noise from an unused analog pin (pin 0), which ensures the random numbers are not the same each time the system starts.

4. Main Loop

void loop() {

  // Check if the button is pressed

  if (digitalRead(buttonPin) == LOW) {

    // Generate a random number between 1 and 6

    diceValue = random(1, 7);

    displayDice(diceValue);  // Display the number on the 7-segment

    delay(200);  // Debounce delay

  }

}

  • loop(): This function runs continuously after the setup() function. It keeps checking if something needs to be done (like pressing the button).
    • digitalRead(buttonPin) == LOW: This checks if the button is pressed. If it is, the state of the button will be LOW (because it's connected to ground when pressed).
    • random(1, 7): This generates a random number between 1 and 6 (the possible dice values). The random() function ensures the dice rolls are random.
    • displayDice(diceValue): This function will take the generated number and show it on the 7-segment display.
    • delay(200): This is a small delay to prevent multiple button presses from being detected too quickly (debouncing).

5.Displaying the Dice Value

void displayDice(int value) {

  byte segmentsToShow = digits[value - 1];

  for (int i = 0; i < 7; i++) {

    digitalWrite(segmentPins[i], bitRead(segmentsToShow, i));

  }

}

  • displayDice(int value): This function takes the number (value) and displays it on the 7-segment display.
    • digits[value - 1]: We subtract 1 because the digits array is 0-indexed, meaning the first number in the array is for "0," the second for "1," and so on.
    • bitRead(segmentsToShow, i): This checks the individual bits (representing each segment) and turns them on or off based on the binary number (segmentsToShow).
    • digitalWrite(segmentPins[i], bitRead(segmentsToShow, i)): This sends the data to each of the 7 pins, turning the corresponding segments of the 7-segment display ON or OFF to form the number.

How It Works Together

  1. The system remains idle until the button is pressed.
  2. When pressed, the Arduino generates a random number.
  3. The corresponding number lights up on the 7-segment display.

OBSERVING FUNCTIONALITY

  • Idle Mode: The system remains in standby until the button is pressed.
  • Rolling the Dice: Pressing the button triggers a new random number, which is displayed instantly.
  • Ensuring Fairness: The use of Arduino’s random function ensures fair results.

COMMON PROBLEMS AND SOLUTIONS

Display Not Working:

  • Cause: Incorrect wiring of the 7-segment display.
  • Solution: Double-check connections and resistor placements.

Button Press Not Registering:

  • Cause: Loose connections or improper pull-down resistor.
  • Solution: Ensure the pull-down resistor is correctly wired and the button is working.

Repeated Same Numbers:

  • Cause: Arduino’s random() function needs seeding.
  • Solution: Add randomSeed(analogRead(A0)); in setup() to improve randomness.

Conclusion

This electronic dice project provides a modern twist on traditional dice rolling, eliminating common inconveniences while maintaining the randomness needed for fair gameplay. Whether for gaming, learning, or fun, this simple yet effective circuit showcases the power of microcontroller-based electronics in an interactive and engaging way.


Comments

Wi-Fi SMART HOME CONTROLLER