Introduction: 6-Sided Die With Arduino

This project simulates a six-sided die. When the button is pressed, a random number 1-6 is displayed.

Background:

I wanted to create a project that was both relatable and understandable for an upper elementary student or a middle school student. In the classroom, we use dice to play games, complete activities, and randomly choose students to respond. It is also a simple concept -- give me a number 1-6, with an equal chance of getting each number. In the code, a random number 1-6 is chosen, and the number chosen is displayed by lighting up the various segments needed.

Furthermore, I wanted to create a project that was easily expanded upon. What else can we have the display do when we push the button? Do we need to limit our die to six sides? In the final step of the project, I provided some examples of challenge activities that build upon the code and concepts covered in this project.

Skill Level: Beginner

Step 1: Assemble Your Materials

Materials:

  • Arduino UNO
  • Breadboard
  • Button
  • 10-pin 7-segment LED display
  • 8 220-ohm resistors
  • Male-male jumper wires

Step 2: Connect the LED Display

Place the LED display on the breadboard. Use four 220-ohm resistors to connect pins e, d, c, and DP (so skip the middle pin) on the LED display to Pins 13, 12, 11, and 10 respectively on the Arduino UNO.

Use the other four 220-ohm resistors to connect with a different part of the breadboard. Use jumper wires to connect the resistors to pins g, f, a, and b on the LED display (skip the middle pin again). Display pins g, f, a, and b should be connected to Pins 9, 8, 7 and 6 respectively on the Arduino UNO.

Use jumper wires to connect pins cc on either end of the LED display to the blue rail. The cc pins are the middle of the five pins on either end of the display. They are common cathodes and will ground the LED display.

Understanding the code:

The display is made up of seven LED segments. In order to form numbers or letters, only certain LED segments must light up. When the code generates a random number, the code must take that number and find which segments to illuminate in order to show that number on the display.

The number 1 is created by LED segments b and c in the image.

2 is created by segments a, b, d, e, and g.

3 is created by segments a, b, c, d, and g.

4 is created by segments b, c, f, and g.

5 is created by segments a, c, d, f, and g.

6 is created by segments a, c, d, e, f, and g.

The ten pins of the display connect to the seven segments, the decimal point in the lower right corner, and the two common cathodes to ground the display.

For more information:

Hettinga, S. Arduino 7 Segment Display. Retrieved May 04, 2017, from https://www.instructables.com/id/Arduino-7-segmen...

How to Display any Character on a 7 Segment LED Display. Retrieved May 04, 2017, from http://www.learningaboutelectronics.com/Articles/How-to-display-any-character-on-a-7-segment-LED-display.php

Step 3: Connect the Button

Place the button on the breadboard. Connect one end to Pin 5 on the Arduino UNO using a jumper wire, and ground the other to the blue line using another jumper wire.

Step 4: The Code

Run this code. The main loop has it picking a random number 1-6, calling on the code to tell it how to display that number, displaying that number, and then erasing the display to show that it is ready for another "roll."

Functions used:

To make the code cleaner, the main loop calls on other functions.

The number functions (one, two, three, four, five, and six) provide the code for which LED segments should light up to display the "rolled" number.

The showNumber function provides the code explaining which random number should call up which number function. For example, if the random number is 1, then the number function one will be called up to light the appropriate LED segments to show the digital number 1 on the 7-segment display.

Finally, the resetNumber function turns all seven LED segments to low, erasing the displayed number from the 7-segment display.

Hettinga, S. Arduino 7 Segment Display. Retrieved May 04, 2017, from https://www.instructables.com/id/Arduino-7-segmen...

Step 5: Can You Simulate a 10-Sided Die? Can You Spell "HELLO"?

You did it! You simulated a six-sided die using the Arduino UNO.

Challenges to deepen your understanding:

  • How would you go about creating a 10-sided die using digits 0-9? What code would need to be added to the code in Step 3?
  • How would you use two 7-segment displays to create a die with double-digits?
  • How could you change the code to write letters? Can you make it spell "HELLO"?
  • Is our code really giving us random numbers? Tally the numbers "rolled" after one hundred "rolls." Each number should be approximately 1/6th of the total rolls (about 17 rolls).