In this DIY electronics section, let’s make an electronics dice using Seven Segment Display (SSD) and Arduino Uno. This is a simple and fun to build project, and the good thing about this project is that it can be practically used to play games! So let’s get started with this fun project!
Working of the Project:
When we hold the push button, the dice is said to be rolling and a series of numbers (difficult for the human eye to make out) is running. When we leave the push button, the dice is considered to be rolled and a random number will be displayed (using the inbuilt Arduino Random Number function).
Components Required:
- Arduino Uno x1
- Seven Segment Display *(Common Cathode is used here) x1
- Resistor (12 ohm is used here. Can use 100ohm, 220ohm or 470ohm) x1
- Push button x1
- Bread Board x1
- Jumper wires
Working of SSD:
The Seven Segment Display (SSD) is a typical output device for displaying digits and letters. It's typically found in calculators, digital watches, and other electronic devices. It's basically seven LED segments organised in the shape of a "8" for easy viewing. The 'a', 'b', 'c', 'd', 'e', ‘f’ and 'g' pins are used to link each segment to a pin. In addition, it has a 'dp' pin, which is a dot.
Fig. 2: Seven Segment Display common cathode diagram
There are two kinds of SSDs: common cathode and common anode. All the cathode pins of LED segments are connected in the common cathode, which is designated as the SSD's "Common pin." When a positive voltage is applied to any segment, this pin is connected to ground, completing the circuit and lighting that segment. Similarly, with a common anode, all of the LED segments' positive terminals (anode terminals) are shorted and delivered to the SSD's "Common pin," which is connected to positive voltage.
Circuit Connections for Arduino Electronic Dice using Seven Segment Display
Fig. 3: Circuit connections for Electronic Dice using Arduino Uno and SSD
As shown in the above circuit, Connect, A pin of SSD to Digital pin 7 of Arduino; connect B pin of SSD to Digital pin 6 of Arduino; connect C pin of SSD to Digital pin 4 of Arduino; connect D pin of SSD to Digital pin 3 of Arduino; connect E pin of SSD to Digital pin 2 of Arduino; connect F pin of SSD to Digital pin 8 of Arduino; connect G pin of SSD to Digital pin 9 of Arduino; Connect one end of Push Button to Digital pin 12 of Arduino. Connect GND pin of SSD to the other end of push button via a 12 Ohm Resistor (Note: you can also use 100 ohm or 220ohm, etc.).
Note: DP pin of SSD is not being used in this project hence, it’s not connected.
Arduino Dice Code Explanation
const int A = 7;
const int B = 6;
const int G = 9;
In this part, we declare the connections of the pins. This helps us to control each segment of the SSD.
int random_int = 0;
This part helps us store the value which will be the end number displayed after the dice is rolled.
void setup() {
pinMode(A, OUTPUT);
pinMode(B, OUTPUT);
pinMode(G, OUTPUT);
pinMode(12,INPUT_PULLUP);
}
This part helps us setup the pins as Output or Input. All the LED segments of the SSD are Outputs and the Push button gives us the input. The push button is declared as input because when the button is pressed, the dice is considered to be rolling. When we leave the push button, the number is displayed.
void loop() {
int pusshed = digitalRead(12);
if (pusshed == LOW)
{
random_int = random(0,6);
Whatever code is written in void loop function, keeps looping. Because of this, we can give our project the effect of “rolling” the dice by displaying a series of numbers. To do this, we need to first read the data from 12th pin to check if the push button is pressed. Then a series of functions is called which basically displays the numbers. “random_int” is used to store the value of the number that is displayed when the dice is rolled. “random” is an inbuilt function of Arduino. Here, we’re using it to display a random number between 0 and 6.
one();delay(20);
two();
delay(20);
six();
delay(20);
}
Here, the functions are called to display numbers with very minimum delay.
else
{
switch(random_int)
{
case 1: one();
break;
case 2: two();
break;
…
case 6: six();
break;
}
delay(200);
}
}
In the “else” statement section, we display the number. When the push button is released, we need to display a random number between 0 and 6. This is done by switch case. When “random” function generates a random number, the switch case accordingly calls the respective function to tell the SSD to light up those segments to display that number. A delay of 200 milliseconds will hold that number.
void one(){
digitalWrite(A, LOW);
digitalWrite(B, HIGH);
digitalWrite(C, HIGH);
digitalWrite(D, LOW);
digitalWrite(E, LOW);
digitalWrite(F, LOW);
digitalWrite(G, LOW);
}
void two(){
digitalWrite(A, HIGH);
digitalWrite(B, HIGH);
digitalWrite(C, LOW);
digitalWrite(D, HIGH);
digitalWrite(E, HIGH);
digitalWrite(F, LOW);
digitalWrite(G, HIGH);
}
void six(){
digitalWrite(A, HIGH);
digitalWrite(B, LOW);
digitalWrite(C, HIGH);
digitalWrite(D, HIGH);
digitalWrite(E, HIGH);
digitalWrite(F, HIGH);
digitalWrite(G, HIGH);
}
When the respective function is called, we write the exact segments of SSD that needs to be lit up to make the number. Those segments are made HIGH in the function body.
Complete Code
const int A = 7;
const int B = 6;
const int C = 4;
const int D = 3;
const int E = 2;
const int F = 8;
const int G = 9;
int random_int = 0;
void setup() {
pinMode(A, OUTPUT);
pinMode(B, OUTPUT);
pinMode(C, OUTPUT);
pinMode(D, OUTPUT);
pinMode(E, OUTPUT);
pinMode(F, OUTPUT);
pinMode(G, OUTPUT);
pinMode(12,INPUT_PULLUP);
}
void loop() {
int pusshed = digitalRead(12);
if (pusshed == LOW)
{
random_int = random(0,6);
one();
delay(20);
two();
delay(20);
three();
delay(20);
four();
delay(20);
five();
delay(20);
six();
delay(20);
}
else
{
switch(random_int)
{
case 1: one();
break;
case 2: two();
break;
case 3: three();
break;
case 4: four();
break;
case 5: five();
break;
case 6: six();
break;
}
delay(200);
}
}
void one(){
digitalWrite(A, LOW);
digitalWrite(B, HIGH);
digitalWrite(C, HIGH);
digitalWrite(D, LOW);
digitalWrite(E, LOW);
digitalWrite(F, LOW);
digitalWrite(G, LOW);
}
void two(){
digitalWrite(A, HIGH);
digitalWrite(B, HIGH);
digitalWrite(C, LOW);
digitalWrite(D, HIGH);
digitalWrite(E, HIGH);
digitalWrite(F, LOW);
digitalWrite(G, HIGH);
}
void three(){
digitalWrite(A, HIGH);
digitalWrite(B, HIGH);
digitalWrite(C, HIGH);
digitalWrite(D, HIGH);
digitalWrite(E, LOW);
digitalWrite(F, LOW);
digitalWrite(G, HIGH);
}
void four(){
digitalWrite(A, LOW);
digitalWrite(B, HIGH);
digitalWrite(C, HIGH);
digitalWrite(D, LOW);
digitalWrite(E, LOW);
digitalWrite(F, HIGH);
digitalWrite(G, HIGH);
}
void five(){
digitalWrite(A, HIGH);
digitalWrite(B, LOW);
digitalWrite(C, HIGH);
digitalWrite(D, HIGH);
digitalWrite(E, LOW);
digitalWrite(F, HIGH);
digitalWrite(G, HIGH);
}
void six(){
digitalWrite(A, HIGH);
digitalWrite(B, LOW);
digitalWrite(C, HIGH);
digitalWrite(D, HIGH);
digitalWrite(E, HIGH);
digitalWrite(F, HIGH);
digitalWrite(G, HIGH);
}
Output of the project:
When the push button is pressed, the SSD rolls the dice. When the push button is released, the SSD displays the rolled number.
A simple and fun project using Arduino, SSD is built which can be used to play games. Making an Electronic dice is fun and functional! Hope you’ve learnt something.