Electronic Dice
Roll a digital dice using a push button and display a random number with LEDs.
Components Needed
- Arduino Uno
- 6 LEDs
- 6 × 220Ω Resistors
- Push Button
- Breadboard
- Jumper Wires
-
How It Works
When the button is pressed, the Arduino generates a random number between 1 and 6. The corresponding LED lights up to represent the dice result.
-
What You'll Learn
✅ Random Numbers
✅ Push Button Inputs
✅ Multiple Outputs
✅ Arrays
✅ Interactive Arduino Projects
-
Code
const int buttonPin = 2;
int leds[] = {3, 4, 5, 6, 7, 8};
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
for (int i = 0; i < 6; i++) {
pinMode(leds[i], OUTPUT);
}
randomSeed(analogRead(A0));
}
void loop() {
if (digitalRead(buttonPin) == LOW) {
int dice = random(1, 7);
for (int i = 0; i < 6; i++) {
digitalWrite(leds[i], LOW);
}
digitalWrite(leds[dice - 1], HIGH);
delay(300);
}
}