LED Arduino Dice With Servo Motor
This tutorial will teach you how to make an LED dice using an Arduino Uno. This circuit uses 7 LEDs to emulate a physical dices layout and display the numbers from 1-6, as well as a servo motor with a picture of a dice on top to imitate the dice rolling experience. The LED dice display has 7 LEDs arranged with 3 on each side of the breadboard and one in the middle to display numbers in the same pattern as original dice. When a button is pressed, a random number is selected and the LEDs will light up accordingly, and the servo motor will "roll" the dice. There will be a short delay before you can roll the dice again to allow u to play your turn in your desired board game. This project is perfect for board game enthusiasts. It helps prevents the problem of losing your dice, making for easier gameplay. Additionally, LED dice are also visually appealing and can add to the aesthetic of a board game.
Supplies
Parts/materials:
7 LEDs - any colour: $0.09/each
Jumper wires: $9.99
8 Resistors - both 330 Ω and 550 Ω will work: $0.87/each
Servo motor: $3.00/each
Arduino Uno: $26.30/each
Attach LEDs and a Button to Breadboard
Attach 3 LEDs to each side of the breadboard and one in the middle. Add a button the breadboard away from the LEDs
Attach Resistors
Attach 330 Ω or 550 Ω resistors from the negative pin of each LED to the ground and from the button to the ground.
Connect the Wires
Connect all the wires from the breadboard to the Arduino:
From the positive rail on the breadboard to the 5V
Negative rail to the GND
Connect the negative rails on both sides of the breadboard
From the upper right LED to pin 3
Upper middle LED to pin 4
Upper left LED to pin 5
Middle LED to pin 6
Lower left LED to pin 7
Lower middle LED to pin 8
Lower right LED to pin 10
Button to pin 2
Button to positive rail
Connect the Servo Motor
Connect the ground and power.
Connect the signal to pin 11 on the Arduino.
You can tape an image/drawing of a dice onto the servo motor to show the dice rolling.
The Code
Here is the code I used. I added comments explaining each step for clarity.
//Include the servo library
#include <Servo.h>
//Declare the variables and pins
Servo myServo;
const int servoPin = 11;
int servoPosition = 0;
int button = 2;
int bottomLeft = 3;
int middleLeft = 4;
int upperLeft = 5;
int middle = 6;
int bottomRight = 7;
int middleRight = 8;
int upperRight = 10;
int sensorPin = A0;
int state = 0; //Tracks if the dice is pressed
long randNumber; //Stores a random number
//Configure each component as an input or output
void setup(){
pinMode(bottomLeft, OUTPUT);
pinMode(middleLeft, OUTPUT);
pinMode(upperLeft, OUTPUT);
pinMode(middle, OUTPUT);
pinMode(bottomRight, OUTPUT);
pinMode(middleRight, OUTPUT);
pinMode(upperRight, OUTPUT);
pinMode(button, INPUT);
Serial.begin(9600);
randomSeed(analogRead(0));
myServo.attach(servoPin);
myServo.write(servoPosition);
}
void loop(){
//Check if the button is high and not already processing
if (digitalRead(button) == HIGH && state == 0){
state = 1; //Changes state to 1 to indicate the button has been pressed
randNumber = random(1, 7); //Sets the scope of the random number to be from 1-6
delay(100); //Sets a short delay
Serial.println(randNumber);
//Make the servo motor move
myServo.write(90);
delay(500);
myServo.write(0);
//Create a function for each number
if (randNumber == 6){
six();
}
if (randNumber == 5){
five();
}
if (randNumber == 4){
four();
}
if (randNumber == 3){
three();
}
if (randNumber == 2){
two();
}
if (randNumber == 1){
one();
}
delay(5000);
clearAll();
state = 0;
}
}
//Set which LEDs will light up for each dice roll to emulate a dice face
void six()
{
digitalWrite(bottomLeft, HIGH);
digitalWrite(middleLeft, HIGH);
digitalWrite(upperLeft, HIGH);
digitalWrite(bottomRight, HIGH);
digitalWrite(middleRight, HIGH);
digitalWrite(upperRight, HIGH);
}
void five()
{
digitalWrite(upperLeft, HIGH);
digitalWrite(bottomLeft, HIGH);
digitalWrite(middle, HIGH);
digitalWrite(upperRight, HIGH);
digitalWrite(bottomRight, HIGH);
}
void four()
{
digitalWrite(upperLeft, HIGH);
digitalWrite(bottomLeft, HIGH);
digitalWrite(upperRight, HIGH);
digitalWrite(bottomRight, HIGH);
}
void three()
{
digitalWrite(upperLeft, HIGH);
digitalWrite(middle, HIGH);
digitalWrite(bottomRight, HIGH);
}
void two()
{
digitalWrite(bottomRight, HIGH);
digitalWrite(upperLeft, HIGH);
}
void one(){
digitalWrite(middle, HIGH);
}
//Turn off all the LEDs
void clearAll(){
digitalWrite(bottomLeft, LOW);
digitalWrite(middleLeft, LOW);
digitalWrite(upperLeft, LOW);
digitalWrite(middle,LOW);
digitalWrite(bottomRight, LOW);
digitalWrite(middleRight, LOW);
digitalWrite(upperRight, LOW);
}