2                                                   3






   4                         5                         6





    7                                                   8

Using your Arduino to Make an Electronic Dice

An electronic dice does not sound a complicated project, but believe me it is. Your Arduino can make this very simple indeed. In this project we are going to use seventeen simple components and hook them to the Arduino board to make a dice which rolls at the push of a button and makes a sound like a dice being vigourously shaken as it does so. As the button is released, the number rolls out more slowly and stops.


Below are some photos of my breadboard prototype, and a schematic.

And yes, my breadboarding does look as though Hermann Goerring's declared it a target.

In keeping with the idea that you should be able to make these circuits with the parts supplied in your ARDX kit you will need the following:


7 LEDs (colour does not matter)


7 560 ohm resistors (for the LEDs)


1 Piezo Buzzer


1 Tactile Switch (pushbutton)


1 10k resistor.


As you know the pins on the Arduino board can suffer floating voltage and so for a pushbutton input we have to keep them reliably high or reliably low. In this case and for no particular reason I've chosen low, so the switch has to be set up like this:

Go Back



Pin 11

It's important that we know what LED is where too as we'll be addressing them individually.

Below is a chart of the LEDS and their pin positions.

The LED at pin 2 will be used to display 4, 5 and 6.  The one at 3 will be used to display 2,3,4,5 and 6. Pin 4 is used exclusively for 6, pin 5 for 1,3 and 5, pin 6 is used exclusively for 6, pin 7 for 2,3,4,5 and 6, and pin 8 for 4,5 and 6. The sense of this is outlined below:

So much for the hardware then, let's look at the code.


//////// Electronic Dice ////////

///////// circuit ard01 /////////


int (picker);

int (buttonstate);


void setup( ) {

    for (int j=2;j<9;j++){

    digitalWrite (j,HIGH);


    digitalWrite (j,LOW);} // these lines test LEDs

  digitalWrite (5,HIGH); // this line sets it to show a 1




void loop () {

    if (digitalRead (11)==HIGH){


randomgen ();

tone (12,65,10);

       delay (70);

     digitalWrite (12,LOW);}


  if ( buttonstate==0){}


for (int t=10;t<21;t++){

  randomgen ();

  delay (t*t);}

  buttonstate =0;



void randomgen (){

    for (int j=2;j<10;j++){

    digitalWrite (j,LOW);}


 picker= random (1,7);

 switch (picker){

   case 1:

one ();break;

case 2:

two ();break;

case 3:

one ();

two ();break;

case 4:

four ();break;

case 5:

one ();

four ();break;

case 6:

six  ();break;




void one (){

  digitalWrite (5,HIGH);}

  void two(){

    digitalWrite (3,HIGH);

  digitalWrite (7,HIGH);}

 void four (){

two ();

digitalWrite (2,HIGH);

digitalWrite (8,HIGH);}

void six (){

  four ();

  digitalWrite (4,HIGH);

  digitalWrite (6,HIGH);}




When you fire up the board with this code here is what you will see and hear:


First, the seven LEDs will flash on one by one. This is to test them. It will only do this once, when the power is applied.

Next the dice will display 1 in the manner outlined above. It is now ready for you to press the button.

If you now press the button, the lights will turn on and off in a seemingly random fashion. It isn't though. It's showing possible displays from 1 to 6, albeit not in order. The piezo buzzer makes a noise like a dice being rattled vigourously around in a shaker.

As you release the button, the lights slow in their flickering. Eventually they stop altogether and show a display between 1 and 6. This number is entirely random. The display will remain constant until either you press the button again or disconnect the power. This has been timed over 30 minutes and proven so.


Over the page we'll go through this code in bite-sized chunks.