Home / Projects

Project: make an electronic dice with an Arduino

Seven LEDs laid out like the dots on a dice, top left, top right, middle left, centre, middle right, bottom left and bottom right, showing the number 5, with the push button that rolls it
Beginner

Press the button and seven LEDs tumble through random numbers, slow down like a real dice, and stop on a number from 1 to 6. It's a fun first project, and it teaches arrays, binary patterns, and how a computer that can only follow instructions makes random numbers.

Difficulty: beginner. Time: about 45 minutes. Works on: Arduino Uno R3 and R4, Nano and most other Arduino boards.

What you need

  • An Arduino Uno and its USB cable
  • Seven LEDs of the same colour
  • Seven 470 Ω resistors (yellow, violet, brown, or yellow, violet, black, black, brown on 5-band metal film)
  • One tactile push button
  • A breadboard and about a dozen jumper wires
Shopping list (6 items)

Some shop links are affiliate links (Amazon UK, Tooled Up): if you buy through one we may earn a small commission, at no extra cost to you. As an Amazon Associate I earn from qualifying purchases. It never changes which products we recommend, and no shop has paid to be included.

Everything for this build, in one place. One resistor kit and one LED pack cover this project and the others on the site.

  • 1×
    Arduino UNO R4 Minima or UNO R3, plus a USB cableBoth run this project unchanged. Official boards come without a cable: the R4 needs a USB-C data cable and the R3 a USB-B (printer-style) cable.
  • 1×
    5 mm LEDs, assorted coloursStandard 5 mm (or 3 mm) LEDs, not the 'built-in resistor' kind.
  • 1×
    Resistor kit, 1/4 W (0.25 W) assorted valuesOne kit covers every resistor in our projects, and is cheaper than buying values one at a time.
  • 1×
    Tactile push button, 6 mm, four legsThe small square breadboard kind. Packs of 10 or more are the cheapest way to buy them.
  • 1×
    Half-size breadboard (400 points)
  • 1×
    Male-to-male jumper wires (a pack)
  • Free
    Arduino IDEThe free Arduino editor. Nothing to buy.

Each button opens that shop's page for the part, checked when we wrote the list; if one shop is out of stock, try the next. Any equivalent part with the same value works.

Starting from nothing? A starter kit gives you the board, a breadboard and many of these parts in one box.

Why seven LEDs?

Every face of a dice uses the same seven dot positions: three down each side and one in the middle. Number 1 is the centre dot, 6 is both side columns, and the rest are combinations. With one LED in each position, the Uno can show any number by lighting the right ones.

With 470 Ω resistors each LED gets about (5 − 2) ÷ 470 ≈ 6.4 mA. That keeps every pin under the 8 mA per pin Arduino gives for the UNO R4 (the R3 allows 20 mA), and even showing 6, with six LEDs lit, the total is only about 38 mA. That's plenty bright for a dice.

The circuit

Lay the LEDs out on the breadboard in the dice pattern, as in the picture above. Each one connects from its Arduino pin through a 470 Ω resistor to its long leg, with the short leg to GND.

Dot positionArduino pin
Top left (TL)2
Top right (TR)3
Middle left (ML)4
Centre (C)5
Middle right (MR)6
Bottom left (BL)7
Bottom right (BR)8
Push button, one leg12
Push button, diagonally opposite legGND
Analogue pin A0leave unconnected

Leave A0 unconnected. The sketch reads its random electrical noise to start the random numbers somewhere different each time.

Setting up the Arduino IDE

Install the free Arduino IDE from arduino.cc, plug in the Uno, choose Arduino Uno and its port from the board menu at the top, paste the sketch into a new window and click the arrow (Upload). If the upload fails, check you picked the right port, and that the cable carries data rather than only charging.

The sketch

// Electronic dice for an Arduino Uno: press the button, the dots
// tumble, slow down and stop on a number from 1 to 6.

// The seven dots, laid out like a dice:
//   TL     TR
//   ML  C  MR
//   BL     BR
const byte DOT_PINS[7] = {2, 3, 4, 5, 6, 7, 8};   // TL, TR, ML, C, MR, BL, BR
const byte BUTTON = 12;                           // to GND, reads LOW when pressed

// Which dots light for each number. Bit 0 is TL, bit 6 is BR.
const byte FACES[7] = {
  0b0000000,   // 0: all off
  0b0001000,   // 1: centre
  0b1000001,   // 2: TL, BR
  0b1001001,   // 3: TL, C, BR
  0b1100011,   // 4: the four corners
  0b1101011,   // 5: corners and centre
  0b1110111    // 6: both side columns
};

void show(byte number) {
  for (byte i = 0; i < 7; i++) {
    digitalWrite(DOT_PINS[i], bitRead(FACES[number], i));
  }
}

void setup() {
  for (byte i = 0; i < 7; i++) pinMode(DOT_PINS[i], OUTPUT);
  pinMode(BUTTON, INPUT_PULLUP);
  randomSeed(analogRead(A0));      // A0 left unconnected picks up noise: a different start each time
  Serial.begin(9600);
  show(0);
}

void loop() {
  if (digitalRead(BUTTON) == LOW) {
    // Tumble: show random faces, slowing down like a real dice
    for (int k = 0; k < 15; k++) {
      show(random(1, 7));          // random(1, 7) gives 1 to 6
      delay(30 + k * k * 2);
    }
    byte result = random(1, 7);
    show(result);
    Serial.println(result);

    while (digitalRead(BUTTON) == LOW) {}   // wait for the button to be let go
    delay(50);                              // ignore contact bounce
  }
}

All the LEDs start off. Press the button: the dots tumble for about two and a half seconds, slowing down, then stop. The result also appears in the Serial Monitor at 9600 baud.

How it works

  • Arrays of pins. DOT_PINS lists the seven pins in order, so one for loop can set them all up and switch them all.
  • Faces as binary. Each number in FACES is a 7-bit pattern, one bit per dot. 0b0001000 has only bit 3 set, the centre dot, so it's a 1. bitRead() picks out each bit in turn. Our guide to bitwise operations explains binary patterns like these.
  • Random numbers. random(1, 7) gives a whole number from 1 up to 6: the upper limit is never included. The Arduino's random numbers come from a formula, so they would repeat the same series every time it starts. randomSeed(analogRead(A0)) starts the formula from the noise on an unconnected pin, which is different each time. Arduino's own reference suggests this trick.
  • Is it fair? We ran this sketch on a simulated Uno for 6,000 rolls. Each number came up between 981 and 1,018 times, close to the 1,000 you'd expect, and every face lit the right number of dots.
  • Waiting for release. After a roll the sketch waits until you let go of the button, then 50 ms more for the contacts to stop bouncing, so one press always gives exactly one roll.

Troubleshooting

  • One dot never lights: that LED is backwards or its resistor is in the wrong row. Roll a few times and note which position is always dark, then check that LED.
  • It rolls on its own: pin 12 is connected to GND all the time. Check the button uses diagonally opposite legs.
  • The numbers look wrong: check each LED goes to the pin in the table. A swapped pair shows the right number of dots in the wrong places.

Take it further

  • Save pins: the dots always light in pairs (TL with BR, TR with BL, ML with MR), so four pins are enough. Rewrite FACES for four groups.
  • Add a tone() click for each tumble, as in the parking sensor project.
  • Make two dice and add them up, for board games that need a total of 2 to 12.

Sources

The main references behind this guide. Numbers we worked out ourselves, such as the code outputs, come from running the code.