Arduino Electronic Dice with 7 LEDs and a Button (Code)

Arduino ile Elektronik Zar Yapımı – LED Zar projesi kapak görseli

🇹🇷 Bu yazıyı Türkçe okuyun

What does this project do?

We build an electronic dice that shows a random number from 1 to 6 when you press a button, just like a real dice. Seven LEDs are arranged like the dots on the face of a real dice. When you press the button, the dice first “rolls” quickly, slows down and finally stops on a number. You can use it instead of a real dice in board games.

  • Age group: 10 and up
  • Level: Intermediate
  • Time: About 45 minutes
  • What you will learn: arrays, the for loop, random numbers with random(), showing patterns with code

Parts list

PartQty
Arduino Uno (or compatible)1
LED (all the same colour)7
220 Ω resistor7
Push button1
Breadboard1
Male-to-male jumper wires~16
USB cable1

Wiring

Arduino electronic LED dice wiring diagram

Place the LEDs on the breadboard like the face of a dice:

  A       B
  C   D   E
  F       G
ComponentPositionArduino pin
LED ATop leftD2
LED BTop rightD3
LED CMiddle leftD4
LED DCentreD5
LED EMiddle rightD6
LED FBottom leftD7
LED GBottom rightD8
All LEDsShort legsGND
ButtonOne legD12
ButtonDiagonally opposite legGND

The long leg of each LED is connected to its pin through a 220 Ω resistor.

Code

// Arduino Electronic Dice - ozandamci.com

const int ledPins[7] = {2, 3, 4, 5, 6, 7, 8}; // A, B, C, D, E, F, G
const int buttonPin = 12;

// Which LEDs light up for each face of the dice (A B C D E F G)
const bool patterns[6][7] = {
  {0, 0, 0, 1, 0, 0, 0}, // 1
  {1, 0, 0, 0, 0, 0, 1}, // 2
  {1, 0, 0, 1, 0, 0, 1}, // 3
  {1, 1, 0, 0, 0, 1, 1}, // 4
  {1, 1, 0, 1, 0, 1, 1}, // 5
  {1, 1, 1, 0, 1, 1, 1}  // 6
};

void setup() {
  for (int i = 0; i < 7; i++) {
    pinMode(ledPins[i], OUTPUT);
  }
  pinMode(buttonPin, INPUT_PULLUP);
  randomSeed(analogRead(A0));
  showDice(1);
}

void showDice(int number) {
  for (int i = 0; i < 7; i++) {
    digitalWrite(ledPins[i], patterns[number - 1][i]);
  }
}

void loop() {
  if (digitalRead(buttonPin) == LOW) {
    // The dice is rolling: random numbers that slow down
    for (int wait = 30; wait < 300; wait += 25) {
      showDice(random(1, 7));
      delay(wait);
    }

    // Final number
    int result = random(1, 7);
    showDice(result);

    // Wait for the button to be released
    while (digitalRead(buttonPin) == LOW) {
    }
    delay(200);
  }
}

No Arduino? Try this circuit in your browser with the Wokwi simulation → (free, nothing to install).

Step-by-step instructions

  1. Arrange the LEDs. Place the 7 LEDs on the breadboard in the dice layout above. Keep all the long legs facing the same way.
  2. Add the resistors. Connect a 220 Ω resistor to the long leg of each LED.
  3. Connect them to the pins. In the order shown in the table, connect A to D2, B to D3 … G to D8. Connect the short legs of the LEDs to the GND rail.
  4. Place the button. Connect one leg to D12 and the diagonally opposite leg to GND.
  5. Upload the code. Choose your board and port, then click Upload. At start-up, the dice should show 1.
  6. Roll the dice. Press the button; the LEDs should change quickly, slow down and stop on a number.

Troubleshooting

  • The patterns look wrong: Check that the LEDs are connected to the pins in the right order. For example, when you roll a 1, only the centre LED (D5) should light up.
  • You always get the same numbers: Make sure the randomSeed(analogRead(A0)) line is in the code and that the A0 pin is not connected to anything.

Try it yourself

  • Add a “click click” sound with a buzzer while the dice is rolling.
  • Add a second group of 7 LEDs to make a double dice for board games like backgammon.

For teachers: a two-page activity sheet for using this project in class: lesson plan, discussion questions, answer key and a student worksheet.

Video

The narration is in Turkish. English subtitles are available: click CC on the video.

“Arduino Electronic Dice with 7 LEDs and a Button (Code)” için 2 yorum

Bir Cevap Yazın