January 4, 2026
Description
This is a machine that throws 3 dice in a tray, with a stepper motor, and then reads the results using OpenCV. All programming is made in Python, code is mostly copied from older projects and ChatGPT.
Code works by using Blobdetection to find dots on the dice, and dbscan to cluster them into separate dice. The "separators" in the tray is needed for this to work.
Hardware used is a ordinary stepper motor, and a DRV8825 driver circuit. A Raspberry Pi 4 and a Pi Camera 3. Besides from a couple of M4/M2.5 screws most other parts are 3d printed using PLA and PETG.
Stepper motor
Im using a Bipolar stepper motor, NEMA 17. It is rated for 1,5 A but im limiting it to 500 mA with the DRV8825. The drive circuit is powered with 12 V.
Construction
I chose this version as the final machine, since it is quicker and gives better resutls than other machines i have built. It doesn't require any specialty parts like glass or bearings.
The machine is put together with CA glue and hot melt adhesive on a piece of bookshelf board.
Improvements to be made
The camera needs a bit wider angle, like the Pi Camera 3 Wide
To reduce noise levels, rubber bottom should be used in tray.
List dice after what lane it is placed in.
Random number generation
To get good random numbers from dice throws that give a value of 1-6 you can use modulo, but there are things to avoid. If you need a random value from 0 to 27 you should throw one three dice(d1-d3) in an order, and do the following:
(d1 - 1) * 36 + (d2 - 1) * 6 + (d3 -1) * 1If the value is above 196 throw the dice again and remove the numbers, other wise there will be a modulo bias in the results. After that you modulo the number with 28. Example:
First dice thrown: 4
Second dice thrown: 3
Third dice thrown: 2
(4-1)*36 + (3-1)*6 + (2-1)*1 = 121121 modulo 28 = 9
License:
Creative Commons — Public Domain