This post may contain affiliate links. See the Affiliate Disclosure for more information.
Use of information on this site is at your own risk. View the Terms of Service.


Cool Arduino Projects is a new tutorial series we’ve started on the Learn Robotics blog. The goal is to show you how to create application-based projects using Arduino.  If you’re a teacher looking for a STEM project, you can use the full lesson plans in your class.

The Application: Ties to Math & Comp Sci

For today’s lesson, we’re going to create an Arduino dice. We’ll focus on wiring up LED’s and a push button as well as simple logic to “roll the dice.” In the next part of the lesson, we’ll connect an LCD and display information to our user. This project ties in nicely with topics in computer science, math/probability, and game design. Let’s get started!

Cool Arduino Projects Arduino Dice Simulation
  • Facebook
  • Twitter
  • Pinterest

Create a Prototype Arduino Dice

First, we’ll wire up the prototype circuit. You’ll need a breadboard, six LED’s, six 220Ω resistors, an assortment of jumper wires, an Arduino Uno, a push button, one 10KΩ resistor, and a USB printer cable. I recommend this Arduino kit because it includes a lot of components for the price.

Here’s the wiring diagram for the circuit:

You can also use the simulated model on Tinkercad. This is a great way for each student to complete the lesson without the need for 1:1 kits.

Connect the LED’s

Cool Arduino Projects Arduino Dice Simulation
  • Facebook
  • Twitter
  • Pinterest
To start off, connect the LED’s to the breadboard. Remember that LED’s are polarized, so be sure to plug the positive (curved) side into a resistor and the negative (flat) side into ground. Each LED will be connected to a digital pin on the Arduino. In the wiring diagram, we’ve used digital pins 3-8 for LED’s.

Wire up the Push Button

Next, connect the button to the breadboard. One leg is connected to the 10KΩ resistor and to ground. Another leg will be connected to 5V, and a third leg will be connected to digital pin 12 on the Arduino.

Power the Circuit

Finally, assemble the power rails. Connect both power rails of the breadboard to each other. Then run jumper wires from 5V on the Arduino to the red power rail and GND on the Arduino to the black power rail. We’ll power the circuit using the USB printer cable and a USB port on our computer. Verify that the components are correctly wired before moving to the next step.

Cool Arduino Projects Arduino Dice Simulation
  • Facebook
  • Twitter
  • Pinterest

Program the Arduino for the Simulated Dice

Now, we’ve reached the fun part! Let’s program the Arduino to simulate a dice roll. You’ll need to have a copy of the Arduino IDE downloaded on your computer.

If you’re teaching this lesson as part of a class, students can create a “flowchart” or sequence of events outlining their code.

The Setup() method

The setup() method runs once on start-up. Define all the LED pins as OUTPUTS inside this method. Then initialize the Serial Monitor using the command Serial.begin(9600);


Your code should look something like this:

int button = 12;
//led's are on pins 3-8

void setup()
    pinMode(3, OUTPUT);
    pinMode(4, OUTPUT);
    pinMode(5, OUTPUT);
    pinMode(6, OUTPUT);
    pinMode(7, OUTPUT);
    pinMode(8, OUTPUT);
    pinMode(button, INPUT);

    Serial.begin (9600);
    Serial.println("Welcome to Arduino Dice");
    Serial.println("Press the button to roll a number");

You’ll notice I’ve included a couple of print statements to show the user we’ve initialized the dice. This can be accessed in the Serial Monitor of the Arduino IDE.

Code to “roll a dice”

To simulate a dice roll, we’ll use the built-in Arduino method, random().

First, we’ll create our random number, then we’ll use that number to illuminate the correct pattern of LED’s. Here’s an example of the code you can use.

Next, we’ll call the displayNum() method in loop(). For now, we’ll run this method every 5 seconds (5000 milliseconds). Then, we’ll trigger the dice roll using a button. Verify that this works before moving to the next step.

View this post on Instagram

Playing around with my Arduino Dice Simulator. I designed this tutorial specifically for STEM teachers who want a fun way to teach math & computer science skills using electronics. 🤓🙋🏼‍♀️ : The full tutorial will be released on the Learn Robotics blog this afternoon. Check it out! : You can also download the Standards-based curriculum if you’d like the PowerPoint slides, project outline, rubric, and full code to use in your class! : The next part of the lesson will dive more into math & probability concepts. We’ll use an LCD to display information rather than the Serial Monitor. : Have any questions? Drop them below👇 : #learnrobotics #edtech #stemeducation #robotics #prototype #coding #cpp #arduino #tutorial #lessonplans #lessons #k12 #k12education #linkinbio #askmequestions #educator #teachersofinstagram #engineeratwork #engineer #robot #ilooklikeanengineer #innovator #creative #games #dice #simulation #appliedlearning #nerdy #nerdygirl

A post shared by Learn Robotics (@learnrobotics) on

Trigger Dice Roll with a Button

Congrats on making it this far! Now, let’s get the simulated dice to roll on a button press. In other words, when a user presses the button, the displayNum() method is triggered.

We’ll use a concept called “debouncing.” Push buttons often generate open/close states in a short amount of time. This is because of mechanical and wiring limitations. Therefore, in software we can account for this by checking twice in a short period. The code will ensure that the push button is definitely pressed.

Cool Arduino Projects Arduino Dice Simulation
  • Facebook
  • Twitter
  • Pinterest

Let’s modify the demo debouncing code to trigger the displayNum() method. I’ve created another method called rollDice() to store this code. Here’s what it looks like:

//additional global variables
int buttonState;           
int lastButtonState = LOW;    
unsigned long lastDebounceTime = 0;   
unsigned long debounceDelay = 50;
void rollDice(){
  int reading = digitalRead(button);
  if (reading != lastButtonState) {
    lastDebounceTime = millis();

  if ((millis() - lastDebounceTime) > debounceDelay) {
    if (reading != buttonState) {
      buttonState = reading;

      if (buttonState == HIGH) {
  lastButtonState = reading;

When you’re done, test it out by calling rollDice() in loop().

Further Learning

And that’s a wrap! I hope you enjoyed creating this Cool Arduino project! In the next lesson, we’ll add an LCD to display user information and probability calculations. If you enjoyed this project, be sure to leave a comment below.

You can also support Learn Robotics by downloading the full lesson plans including full code download, worksheets, PowerPoint slides, and rubrics here.


Learn Robotics Academy

Get Notified when Registration Begins!


Be the first to know when we open LR Academy! Plus gain FREE robotics tips to your inbox.

Success! We'll let you know when registration begins!

Pin It on Pinterest