Introduction: Digital Dice With Arduino

Have you ever tried to play a game only to find out you don't have any dice? That's not a problem anymore! Now you can create your very own digital dice using LEDs and your Arduino board. This is a beginner-level project. This project will allow you to push a button and randomly generate a number between 1 and 6, shown with the number of LED lights.

Credits to:

EvdS. (n.d.). Led dice. Arduino Project Hub. Retrieved May 14, 2022, from https://create.arduino.cc/projecthub/EvdS/led-dice-885cf1?ref=tag&ref_id=led&offset=8 

Supplies

6 LEDs

6 220 ohm resistors

1 push button

1 k-ohm resistor

1 Arduino Uno

1 breadboard

jumper wires

Step 1: Set Up the LEDs and Button

  1. Place the 6 LEDs in the breadboard as shown. They should go in rail E with two spaces in between each LED. You should place the Anode end (long end) of the LED in the first of the two spaces and the Cathode end (short end) in the second space. Place your LED Anode ends in rows 3, 7, 11, 15, 19, and 23.
  2. Place your button in the breadboard as shown. The left side should go in rail E, space 59 and 61, and the right side should go in rail F, space 59 and 61.

Step 2: Attach the Resistors

  1. Place a 220 ohm resistor in the row with the Cathode end of the LED. It should reach from rail D to the ground rail (-, blue) in each of the LEDs (rows 4, 8, 12, 16, 20, and 24).
  2. Place a 1 k-ohm resistor near your button. It should go from the bottom left of the button (row 61) to row 64.

Step 3: Wire the LEDs

  1. Take one jumper wire and attach it from the Anode end of the first LED (row 3) and attach it to the digital output on the Arduino at space 2.
  2. Take the next jumper wire and attach it from the Anode end of the second LED (row 7) and attach it to the digital output on the Arduino at 3.
  3. Take the next jumper wire and attach it from the Anode end of the third LED (row 11) and attach it to the digital output on the Arduino at space 4.
  4. Take the next jumper wire and attach it from the Anode end of the fourth LED (row 15) and attach it to the digital output on the Arduino at space 5.
  5. Take the next jumper wire and attach it from the Anode end of the fifth LED (row 19) and attach it to the digital output on the Arduino at space 6.
  6. Take the next jumper wire and attach it from the Anode end of the sixth LED (row 23) and attach it to the digital output on the Arduino at space 7.


Step 4: Wire the Button

  1. Take one jumper wire and attach it on the bottom right (row 61, rail G) by the button. Attach the other end of the wire to the digital output on the Arduino at space 12.
  2. Take another jumper wire and attach it next to the resistor in the space at row 64, rail C. Attach the other end to the ground rail (-, blue).
  3. Take another jumper wire and attach it to the top left side of the button (row 59, rail D). Attach the other end to the positive rail.

Step 5: Attach the Ground and 5V Wire

  1. Take a jumper wire and attach it to the Ground port on the Arduino. Attach the other end to the ground rail on the breadboard.
  2. Take another jumper wire and attach it to the 5V port on the Arduino. Attach the other end to the positive rail.

Step 6: Load the Sketch Into Arduino Create and Connect to Computer

  1. Load the following sketch into the Arduino Create software.

// set to 1 if we're debugging

#define DEBUG 0


// 6 consecutive digital pins for the LEDs

int first = 2;

int second = 3;

int third = 4;

int fourth = 5;

int fifth = 6;

int sixth = 7;


// pin for the button switch

int button = 12;

// value to check state of button switch

int pressed = 0;


void setup() {

// set all LED pins to OUTPUT

for (int i=first; i<=sixth; i++) {

pinMode(i, OUTPUT);

}

// set buttin pin to INPUT

pinMode(button, INPUT);

// initialize random seed by noise from analog pin 0 (should be unconnected)

randomSeed(analogRead(0));


// if we're debugging, connect to serial

#ifdef DEBUG

Serial.begin(9600);

#endif


}


void buildUpTension() {

// light LEDs from left to right and back to build up tension

// while waiting for the dice to be thrown

// left to right

for (int i=first; i<=sixth; i++) {

if (i!=first) {

digitalWrite(i-1, LOW);

}

digitalWrite(i, HIGH);

delay(100);

}

// right to left

for (int i=sixth; i>=first; i--) {

if (i!=sixth) {

digitalWrite(i+1, LOW);

}

digitalWrite(i, HIGH);

delay(100);

}

}


void showNumber(int number) {

digitalWrite(first, HIGH);

if (number >= 2) {

digitalWrite(second, HIGH);

}

if (number >= 3) {

digitalWrite(third, HIGH);

}

if (number >= 4) {

digitalWrite(fourth, HIGH);

}

if (number >= 5) {

digitalWrite(fifth, HIGH);

}

if (number == 6) {

digitalWrite(sixth, HIGH);

}

}


int throwDice() {

// get a random number in the range [1,6]

int randNumber = random(1,7);

#ifdef DEBUG

Serial.println(randNumber);

#endif

return randNumber;

}


void setAllLEDs(int value) {

for (int i=first; i<=sixth; i++) {

digitalWrite(i, value);

}

}


void loop() {

// if button is pressed - throw the dice

pressed = digitalRead(button);


if (pressed == HIGH) {

// remove previous number

setAllLEDs(LOW);

buildUpTension();

int thrownNumber = throwDice();

showNumber(thrownNumber);

}


}

  1. Then attach the Arduino board to your computer and load the sketch onto the board.

Step 7: Test Your Digital Dice Roller

  1. Try out the dice roller by pressing the button. The LEDs will blink and then stay steady when the roll has been decided.