This project demonstrates how to build a simple electronic dice using an Arduino and a 7-segment display. The system generates a random number between 1 and 6 when the push button is pressed, similar to a real dice used in games. The generated number is displayed on the 7-segment display. This project helps in understanding basic Arduino programming, digital input from a push button, and controlling a display device.
Here we will show the 7-segment display and explain all of its pins with their names, so we can clearly demonstrate how the connections are made and which pin is connected to each pin on the Arduino.
Components Use
Arduino Uno
7-Segment Display
Push Button
Breadboard
Resistors 1k
Jumper Wires
USB Cable
Now, we will provide a detailed demonstration of the complete circuit. This includes showing all the pins of the 7-segment display, the push button, and all other components, as well as explaining exactly how each pin is connected to the corresponding pins on the Arduino. This comprehensive overview allows anyone reading the report to clearly understand the wiring and the assembly of the project.
Circuit Connections Description
First, we will connect the 7-segment display to the Arduino as follows:
Connect pin C of the 7-segment display to pin 2 on the Arduino.
Connect the COM (common cathode) pin of the 7-segment display to the GND pin on the Arduino.
Connect pin D of the 7-segment display to pin 3 on the Arduino.
Connect pin E of the 7-segment display to pin 4 on the Arduino.
Connect pin B of the 7-segment display to pin 5 on the Arduino.
Connect pin A of the 7-segment display to pin 6 on the Arduino.
Connect pin F of the 7-segment display to pin 7 on the Arduino.
Connect pin G of the 7-segment display to pin 8 on the Arduino.
Next, we will set up the push button and resistor circuit:
Use a 1kΩ resistor along with the push button.
Connect one terminal of the push button to GND on the Arduino.
Connect the other terminal of the push button to one end of the 1kΩ resistor.
Connect the other end of the 1kΩ resistor to COM of the 7-segment display.
The push button is now ready to control the display when pressed
This complete wiring ensures that each segment of the display is properly connected to the Arduino pins, and the push button works correctly to trigger the display of random numbers. As you all can see .
Now, I will show you a video demonstrating the circuit operation and how it works.
As seen in the video, every time we press the push button, a new random number between 1 and 6 is generated. Some numbers may repeat, and occasionally a number might not appear. This behavior is normal and simply reflects the way the random number generator works.
At this point, I will show the Arduino code that was implemented to make the circuit function as intended, including generating random numbers and displaying them on the 7-segment display.
It is recommended to use Arduino IDE version 2.3.4. I went to the top menu, selected Sketch, then Include Library, and from there, clicked on Manage Libraries. In the Library Manager, we can search for the library that we want to install.
Then, in the search bar, type SevSeg by Dean Reading to find the library we need.
At the beginning, the code did not work because I discovered that I did not have the SevSeg library installed. Therefore, I had to download this library. I will now provide its name along with a screenshot and explain the steps to install the library.
the code c++
#include <SevSeg.h>
SevSeg sevseg;
long randNumber; // Variable to store random number
const int buttonPin = 10; // Button pin
int buttonState = 0;
void setup()
{
Serial.begin(9600);
byte numDigits = 1;
byte digitPins[] = {}; // Not used because we have one digit
byte segmentPins[] = {6, 5, 2, 3, 4, 7, 8, 9};
bool resistorsOnSegments = true;
byte hardwareConfig = COMMON_CATHODE;
sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments);
sevseg.setBrightness(100);
pinMode(buttonPin, INPUT);
randomSeed(analogRead(0)); // Generate random seed
}
void loop()
{
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH)
{
randNumber = random(1, 7); // Random number from 1 to 6
Serial.println(randNumber);
sevseg.setNumber(randNumber);
delay(3000); // Display number for 3 seconds
}
sevseg.refreshDisplay();
}
That brings us to the end of today’s demonstration. I hope everyone was able to benefit from it, and I wish you success in applying this knowledge in your projects.