Ready to roll the dice on a fun new project? Today, we are building a classic Electronic LED Dice using an Arduino Uno and a pushbutton. Whenever you press the button, the Arduino generates a random number and lights up the LEDs. It takes ten times longer to build than just buying a piece of plastic, but at least this one requires batteries!

The circuit uses seven LEDs arranged in an “H” shape to mimic the pips on a real die. A pushbutton is connected to a digital input on the Arduino. When the button is pressed, the code uses a random() function to pick a number.
Through the use of If/Else logic or arrays, the Arduino determines which combination of LEDs needs to turn on to display that number. This project is a fantastic introduction to Variables and Loops.
Here is everything you will need for this build. All parts are standard components that you likely already have, or can grab cheaply online:
Upload this sketch to your Arduino. It initializes the pins, reads the button state, and generates the random display!
int buttonPin = 9;
int buttonState = 0;
// LED Pins array: {Top-L, Top-R, Mid-L, Center, Mid-R, Bot-L, Bot-R}
int ledPins[] = {2, 3, 4, 5, 6, 7, 8};
void setup() {
for (int i = 0; i < 7; i++) {
pinMode(ledPins[i], OUTPUT);
}
pinMode(buttonPin, INPUT);
// Seed the random number generator using an unconnected analog pin
randomSeed(analogRead(0));
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
// Show a rolling animation
for (int i = 0; i < 10; i++) {
showNumber(random(1, 7));
delay(50);
}
// Pick the final number
int result = random(1, 7);
showNumber(result);
// Wait so it doesn't trigger repeatedly
delay(2000);
}
}
void showNumber(int num) {
// Turn all off first
for (int i = 0; i < 7; i++) {
digitalWrite(ledPins[i], LOW);
}
// Turn on LEDs based on the rolled number
if (num == 1) {
digitalWrite(5, HIGH); // Center
}
if (num == 2) {
digitalWrite(2, HIGH); // Top-L
digitalWrite(8, HIGH); // Bot-R
}
if (num == 3) {
digitalWrite(2, HIGH);
digitalWrite(5, HIGH);
digitalWrite(8, HIGH);
}
if (num == 4) {
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
}
if (num == 5) {
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(5, HIGH);
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
}
if (num == 6) {
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(6, HIGH);
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
}
}
Once the code is uploaded, simply press the button! You should see the LEDs flicker like a rolling die, before settling on a random number.
If you want to take this project further, consider building an enclosure for it, or adding a Piezo buzzer to make a ticking sound while the dice is “rolling”!