Shopping Cart

No products in the cart.

Digital Dice

Have you ever wanted to roll dice without using your hands? In this project, you’ll build a digital dice game using a touchless sensor and colorful Neo Matrix LEDs. Just wave your hand, and a random number will appear in lights — like magic!

Did you know:
In Las Vegas casinos, dice are tested with lasers to make sure they’re perfectly fair. Now you’re building your own electronic dice—with code instead of luck!

What Happens When You Shake It ?

When you shake the sensor, it closes a tiny metal contact inside for a split second. This is detected by your ESP chip as a quick “HIGH” signal. Your program waits for this shake signal. When it detects one, it generates a random number between 1 and 6, and then shows the matching dice pattern on the LED matrix. The dice is digital, but the shaking makes it feel like the real thing!

Connection Guide:

Code Logic

The vibration sensor is a digital input that triggers a number roll when moved.

The NeoMatrix displays the result using pre-set dot patterns for numbers 1 to 6.

We use random() to choose a number and display the correct pixels.

Code Examples

#include <Adafruit_NeoPixel.h>

#include <Adafruit_GFX.h>

#include <Adafruit_NeoMatrix.h>

#define PIN 6 #define SENSOR 15 // Matrix data pin

// Vibration sensor digital pin

Adafruit_NeoMatrix matrix = Adafruit_NeoMatrix(8, 8, PIN,

NEO_MATRIX_TOP + NEO_MATRIX_LEFT + NEO_MATRIX_ROWS + NEO_MATRIX_ZIGZAG,

NEO_GRB + NEO_KHZ800);

void setup() {

pinMode(SENSOR, INPUT); // Set up sensor pin as input

matrix.begin(); // Start matrix

matrix.setBrightness(40); // Adjust brightness

randomSeed(analogRead(0)); // Seed random number

}

void loop() {

if (digitalRead(SENSOR) == HIGH) { // If shake is detected

int num = random(1, 7); // Get random number 1–6

showDice(num); // Show number on matrix

delay(1000); // Delay so user sees result

matrix.fillScreen(0); // Clear screen

matrix.show();

}

}

// Shows a number on the matrix

void showDice(int n) {

matrix.fillScreen(0);

switch (n) {

case 1: matrix.drawPixel(3, 3, matrix.Color(255, 0, 0)); break;

case 2: matrix.drawPixel(1, 1, matrix.Color(255, 0, 0));

matrix.drawPixel(6, 6, matrix.Color(255, 0, 0)); break;

case 3: matrix.drawPixel(1, 1, matrix.Color(255, 0, 0));

matrix.drawPixel(3, 3, matrix.Color(255, 0, 0));

matrix.drawPixel(6, 6, matrix.Color(255, 0, 0)); break;

case 4: matrix.drawPixel(1, 1, matrix.Color(255, 0, 0));

matrix.drawPixel(6, 1, matrix.Color(255, 0, 0));

matrix.drawPixel(1, 6, matrix.Color(255, 0, 0));

matrix.drawPixel(6, 6, matrix.Color(255, 0, 0)); break;

case 5: matrix.drawPixel(1, 1, matrix.Color(255, 0, 0));

matrix.drawPixel(6, 1, matrix.Color(255, 0, 0));

matrix.drawPixel(3, 3, matrix.Color(255, 0, 0));

matrix.drawPixel(1, 6, matrix.Color(255, 0, 0));

matrix.drawPixel(6, 6, matrix.Color(255, 0, 0)); break;

case 6: for (int y = 1; y <= 6; y += 2) {

matrix.drawPixel(1, y, matrix.Color(255, 0, 0));

matrix.drawPixel(6, y, matrix.Color(255, 0, 0));

} break;

}

matrix.show();

}

Your turn

Now it’s your turn:

•EASY: Change the dice color from red to blue.

•MEDIUM: Add sound with a buzzer when the number is rolled.

•HARD: Store the last 5 dice rolls in memory and blink the matrix if the same

number appears twice.

•HARD: Make the NeoMatrix show real digits (1–6), not just light-up dots like on

a real dice.

Video Tutorial

Be the first to know about our special offers and product news.