Digital Pig Dice Game
Hello! My name is Ishandeep, and this Instructable will guide you through the creation of my newest engineering project, an Digital Pig Dice Game. This project combines digital electronics and programming using a 555 Timer, CD4017 Decade Counter, 7447 BCD Decoder, Seven-Segment Display, and an Arduino. While the circuit may appear complicated at first, this guide breaks the entire process into simple, manageable steps, making it easy to follow even if you have limited experience with electronics.
Before building the project, it is important to understand how the game works. The Digital Pig Dice Game is based on the classic dice game "Pig." Two players take turns rolling an electronic die by pressing a push button. Each roll generates a random number from 1 to 6, which is displayed on the seven-segment display. Any value from 2 to 6 is added to the player's temporary turn score, allowing them to continue rolling and accumulating points. However, if a player rolls a 1, they immediately lose all points earned during that turn, and it switches to the other player.
At any point during their turn, a player may choose to "bank" their accumulated turn score, adding those points to their overall total before ending their turn. The objective is to continue making strategic decisions between risking another roll or securing earned points. The player who reaches the highest score wins the game.
The electronic die itself is generated using a 555 Timer and CD4017 Decade Counter. The timer produces rapid clock pulses, while the counter cycles through different outputs to create the appearance of randomness. The resulting value is then displayed on a seven-segment display through the 7447 decoder. An Arduino is used to manage the game logic, track player scores, and communicate game information through the serial monitor.
When deciding on a final project, I wanted to combine concepts from both the Digital Circuits and Microcontroller units because they were the areas I found most interesting throughout the semester. Earlier in the course, several of my peers were building electronic dice circuits, which inspired me to expand the idea into a complete multiplayer game after seeing similar projects online. By integrating additional hardware and software features, I transformed a simple electronic die into an interactive game that demonstrates both digital logic and programming concepts. Throughout this Instructable, I will explain how each component works and provide detailed instructions for building, wiring, and programming the project from start to finish.
Let’s get into it!
Gather the Components
- 1x - Arduino UNO
- 1x - Large Breadboard
- 1x - 555 Timer
- 1x - CD4017 IC (Decade Counter)
- 1x - 7447 Decoder
- 1x - Push Button
- 1x - Slide Switch
- 1x - 10µF Capacitor
- 9x - 1N4148 Diodes
- 1x - 10kΩ Resistors
- 8x - 1kΩ Resistors
- 1x - 7-Segment Display (Common Anode)
- Jumper Wires
Plan the Project
I believe that planning and having a clear idea of what you're going to do on a project is the best way to go before starting it. In these designs, I made the project I was going to make in real life, but online. This helped assure me that the project would work when I tried building it by hand.
Planning the project helped me create a neat and tidy Breadboard when I built the project by hand, and I recommend that everyone do this before starting a new project, as it helped me a lot!
Place Down the Components
- Here are some pictures of how the Components should be placed before we start wiring them!
- Make sure to neatly place down every component so the project can be as neat and clean as possible!
Before starting any wiring, take a few minutes to plan the component placement for your Digital Pig Dice Game on the breadboard. This project uses most of the available space, so organizing the layout ahead of time makes assembly easier and reduces the chances of needing to move components later.
Begin by placing the 555 Timer on the left side of the breadboard. Next, position the push button beside it, followed by the 4017 Decade Counter. Leave a gap after the counter to allow room for additional wiring components such as resistors and diodes before inserting the 7447 decoder. Finally, place the seven-segment display on the far right side of the breadboard.
To keep the build process organized and easier to follow, the wiring will be divided into three sections based on the components used in the project.
Questions You May Have:
Why are we using so many resistors on a lot of the components?
- We are using resistors on components we have, such as push buttons, LEDs, and Seven-Segment Displays, in order to limit the flow of electrical current, reduce voltage, and protect other components on the breadboard. Essentially, we need to use resistors in order to prevent too much current from going into the Arduino, which can fry it.
You got this :)
Wiring (555 Timer)
With the wiring for the Digital Pig Dice Game, I tried to be as neat as possible and made sure to correctly trim every single wire I placed. I suggest you do the same.
Tips:
- Try using your hands to straighten the wires
- Rather than having wires all over the place, try to bend them at a 90-degree angle to make them neater and more organized
Here is a link to the 555 Timer pinout for reference. Start by placing the 555 Timer on the breadboard with the groove facing left to make the wiring layout easier to follow. Connect power to PIN 8 and ground to PIN 1, then connect the reset pin (PIN 4) to power.
Next, connect the 1kΩ resistors from PIN 7–6 to power and connect the 10µF capacitor between ground and the trigger pin (PIN 2). Connect the output pin (PIN 3) to a push button that is wired to ground using a 10kΩ resistor.
An EasyEDA schematic is included in this tutorial if you want to zoom in and inspect the exact connections more closely. Take your time while wiring and double-check that every component is placed in the correct pin location before moving on.
555 Timer Function in the Pig Dice Game:
The 555 Timer is an integrated circuit used to generate timed pulses or repeating signals. In this project, it operates in astable mode, meaning it continuously switches between HIGH and LOW states to create fast clock pulses. These pulses are used to drive the game logic and create the rolling effect for the electronic dice.
Wiring (CD4017B Decade Counter)
Next, wire the CD4017 Decade Counter. Use the CD4017 pinout as a reference while following this step. Start by connecting power to PIN 16 and ground to PIN 8. Connect the clock enable pin (PIN 13) to ground, then connect the push button from the previous step to the clock input pin (PIN 14).
Next, install the 9 1N4148 diodes and complete the remaining output connections by following the schematics and breadboard images above, which are labelled for clarity.
- Pay close attention when wiring the diodes. Since they are placed close together, it is easy to accidentally connect to the wrong row or reverse a connection. Work slowly and complete one diode connection at a time.
After placing the diodes, connect the four 1kΩ resistors to ground. Some diodes connect directly to these resistors using the following connections:
- Diode 1 → R4
- Diode 2 → R5
- Diode 7 → R6
4017 Function in the Pig Dice Game:
The CD4017 receives clock pulses from the 555 Timer and advances through its output pins one at a time (Q0–Q9). For this project, only outputs Q0–Q5 are used to represent dice values 1–6, while Q6 is connected to reset so the sequence loops back to the beginning like a real die. When the button is held, the outputs change rapidly to simulate rolling, and releasing the button stops the count on a random result.
Wiring (7447 BCD Decoder)
Lastly, wire the 7447 BCD Decoder. Use the 7447 pinout as a reference while completing this step. Start by connecting power to PIN 16 and ground to PIN 8. Then connect PINS 5, 4, and 3 to power.
This chip receives inputs from the four 1kΩ resistors added in the previous step using the following connections:
- R4 → PIN 7
- R5 → PIN 1
- R6 → PIN 2
- R7 → PIN 6
Next, connect the 7447 to the seven-segment display. Since these connections can be confusing, refer to the EasyEDA schematic to closely follow the wiring between both components.
- Segment a → PIN 13
- Segment b → PIN 12
- Segment c → PIN 11
- Segment d → PIN 10
- Segment e → PIN 9
- Segment f → PIN 15
- Segment g → PIN 14
7447 Function in the Pig Dice Game:
The outputs from the 4017 are converted into binary values using the diodes and pull-down resistors, then sent to the 7447 BCD to 7-Segment Decoder. The decoder translates these values into the correct output pattern so the seven-segment display shows the correct dice value.
Code Variables
Let's now begin the coding!
In any Arduino project, the code is an extremely crucial step for the game, and the game won't even work if we don't have working code, so let's get started with the variables in the code! This code was inspired by another individual who made a digital dice that generated random numbers 1-6 using Arduino.
To begin the code, variables are declared for the Arduino pins and game data. Variables allow the Arduino to store information and reference components more easily throughout the program. The general format is shown below:
int (variable name) = pinNumber;
Remember to include a semicolon (;) at the end of each line.
The variables in1–in6 represent the six output pins from the CD4017 that correspond to dice values 1–6. These outputs are connected to the Arduino so it can detect which number was rolled.
Arduino pin connections:
int in1 = 2;
int in2 = 3;
int in3 = 4;
int in4 = 5;
int in5 = 6;
int in6 = 7;
The rolled variable acts as a flag to determine whether a dice roll has occurred, while score stores the player's current total score. The currentState and prevState variables keep track of the push button’s current and previous states, allowing the Arduino to detect a new button press instead of repeatedly reading the same press. These variables start at 0, which represents a LOW state.
The button variable stores the pin connected to the push button, and the slide variable stores the pin connected to the slide switch, which is used to continue or end the game.
Arduino pin connections:
int button = 8;
int slide = 9;
Setup Function Code
Now that all variables have been initialized, they must be assigned as either inputs or outputs inside void setup(), which is a function that runs once when the Arduino first powers on. This section prepares the Arduino by defining what each pin will do throughout the game.
Pins are configured using the following format:
pinMode(variable name, INPUT/OUTPUT);
In this project, the Arduino reads signals from the push button, slide switch, and the six outputs of the CD4017, so all of these pins are set as INPUTS.
pinMode(button,INPUT);
pinMode(slide,INPUT);
pinMode(in1,INPUT);
pinMode(in2,INPUT);
pinMode(in3,INPUT);
pinMode(in4,INPUT);
pinMode(in5,INPUT);
pinMode(in6,INPUT);
Lastly, to display game information and guide the player while playing Pig Dice, the Serial Monitor is used (a tool in the Arduino IDE that displays text sent from the Arduino). To enable this, add the following code:
Serial.begin(9600);
Serial.println();
Serial.println("Welcome to Pig of Dice! Let's Play!");
This starts serial communication at 9600 baud and displays a welcome message when the Arduino begins running. Throughout the game, the Serial Monitor is also used to show score updates and tell the player whether to roll again or bank their points.
Loop Function Code
Next is the loop function, which contains the main game logic. Unlike the setup function, this function runs continuously while the Arduino is powered and controls how the Pig Dice Game is played.
Detecting Button Press:
The first part of the code checks whether the slide switch is ON or OFF. If the slide switch is ON (HIGH), the game continues running. If the slide switch is turned OFF (LOW), the game ends and the final score is displayed.
When the game is active, the Arduino reads the push button state. By comparing currentState and prevState (the current and previous button states), the code detects a new button press instead of repeatedly reading a held button. This prevents multiple rolls from being counted from a single press.
If a new press is detected, rolled is set to 1. Otherwise, it remains 0.
Rolling the Dice:
When rolled becomes true, the Arduino waits 1 second before reading the dice value to allow the signal to stabilize. The Arduino then reads which output pin from the CD4017 is HIGH to determine the rolled value (1–6).
If the player rolls 2–6, that value is added to the current score and displayed in the Serial Monitor. If the player rolls a 1, the score immediately resets to 0, ending the player's turn.
After each successful roll, the player can choose to continue rolling for a higher score or stop and bank their current points.
For example, if the player rolls a 4 and then a 3, the values are added together and the displayed score becomes 7.
Ending the Game:
At any point, the player can end the game by switching the slide switch OFF. This banks the current score and displays a "Thanks for playing!" message along with the final score in the Serial Monitor.
Downloads
Have Fun :D
Here’s a cool video showing my Digital Pig Dice Game in action!
I hope you enjoyed following along with this project.
Thank you so much for checking it out! 😄