About a year ago my son and I built an electronic dice with an Arduino and a few LEDs. This is still an ideal project for beginners. We have learned a lot this year, so it's high time for an update.
The dice is still a perfect starter project. The function is clear to everyone and it is easy to understand the different areas: microcontroller, simple electronics and coding.
Version 2.0The dice is also fantastic, to go one step further: linking other areas.
Today there is a ready-built component for almost everything. This is best seen when looking for a starter kit for an Arduino or Raspberry Pi. Components such as LEDs, pushbuttons and any type of sensor are built on a circuit board and can thus be connected and used with just a few wires to GPIOs.
Simple, but sufficient, 3D printers are available on ebay for less than 140 €. Almost all bracket, mounting or casing variations can be created with it.
There are a number of more opportunities to develop your own projects. An example of this is our new dice 2.0.
Tilt SensorsWe implement the dice in a completely different way. Inside, an Arduino Nano is working for space reasons. There is still an on-off switch, but no button. The dicing is done by shaking the whole cube.
For this purpose, tilt sensors or vibration sensors are used. They work just like a button. In a glass tube, a ball moves. If it hits the connection contacts, the circuit is closed. This contact, sometimes very short, is used for a signal.
The movement of the metal ball can be heard quite well if you shake the sensor.
Normally the installation position is decisive for a tilt sensor. It is supposed to trigger a signal at a certain angular position. In our case, this happens by shaking the case. Here the situation is not so important, because when shaking in any direction a signal is triggered. To be on the safe side, we use two sensors in the project, which are arranged offset by 90 ° to each other. So we always get a reliable shake signal.
InterruptsIn order to be able to recognize when shaking, the input pins of the tilt sensors would have to be queried in the sketch. However, depending on when this happens in time and what else the sketch has to do, there is always the possibility that one or more events go unnoticed.
A better solution is to use hardware interrupts. This is defined by the function attachInterrupt. As a parameter a subprogram is specified which is to be called on the signal.
The Arduino provides two hardware interrupts: pins D2 and D3.
DisplayOf course, the display of the dice image could again be done with 7 individual LEDs. But it is also more interesting to use a finished component here.
Our choice for this project is much on an 8x8 LED matrix with MAX7219 driver IC. It takes up very little space, costs only a small amount of money and is easy to code.
Depending on where you buy it, you have to solder together and assemble the single parts. That is normally no problem. The only mistake that can be made is to put the LED matrix twisted into the jacks.
This is shown by the above picture. Here it is sufficient to carefully pull the matrix out of the sockets and turn it through 180 °.
Some PhysicsThe LED matrix should not just show the dice image as a result. It should offer some show effects. The six dice eyes move on the matrix: they bounce off the edges and slowly lose speed.
Through the two tilt sensors, we can see how much shaking was done. This information we give the dice eyes as "speed" for their movement.
We also use a similar effect when displaying the dice result. The eyes roll from their random position to their correct position in the cube image.
Power SupplyFirst, we have installed two 3V CR2032 button cells for space reasons. At the beginning it looked pretty good too. The Arduino and LED matrix played along and everything worked. After a few minutes of operation, however, the power of the button cells break down.
If you turn off the Arduino after every dice, you could use it that way. That's not perfect, so we install better two AAA batteries. However, these only deliver together 3V. So we still need a step-up converter, which raises the voltage to 5V. The connection still takes place at the VIN pin of the Arduino.
Housing DesignThe best option for a suitable housing is a own design and production via 3D printing. There are many apps for the construction. We have used Autodesk Fusion 360 for this project. It has great features and 3D printing is well integrated with the Print Studio software. If you are interested in Autodesk Fusion 360, you will find some suggestions in the blog article Parametric Enclosures (only German).
Our housing consists of 3 parts:
- Upper case
- Lower case with battery holder for AAA cells
- Plexiglas cover (does not necessarily have to be)
The LED matrix looks even better with a milky plexiglasscover in front of it. This makes it impossible to recognize the off LEDs and the picture is clearer.
SketchTo control the LED Matrix, the LedControl library is required. If it is not already installed, it can be downloaded from arduino.cc https://playground.arduino.cc/Main/LedControl.
Then we do the other variable definitions. First, we determine how the dice eyes are displayed on the matrix. A dice-eye consists of 4 LED-points. In the array, the upper left corner is specified as the X / Y coordinate (-1 means outside the display).
int DicePic[8][6][2] = { …
{ //1:
{4,4}, //1. Punkt
{-1,-1}, //2. Punkt
{-1,-1}, //3. Punkt
{-1,-1}, //4. Punkt
{-1,-1}, //5. Punkt
{-1,-1} //6. Punkt },
{ //2:
{2,2}, //1. Punkt
{6,6}, //2. Punkt
{-1,-1}, //3. Punkt
{-1,-1}, //4. Punkt
{-1,-1}, //5. Punkt
{-1,-1} //6. Punkt }, …
Of course, everything is possible here. Let your imagination run wild. It does not always have to be the typical eye image.
Other remarks you'll find direct in the code.
AssemblyWe put our Arduino on a socket strip, as a kind of shield. The five contacts of the LED matrix are connected in the same way. If the Arduino is centered, the space is best used. Therefore it is best to use pins 5, 6, 7 for CS, CLK and DIN for the matrix. Here then only the solder points must be connected. The connection to 5V and GND is via a small jumper.
We also connect the two tilt sensors with short wires. These are connected to pin 2 and 3 and 5V and GND.
Now everything can be assembled. The plexiglass is first fixed with hot glue. We do the same with the LED matrix. A small point of hotglue is enough.
Next, the on / off switch is installed, secured with hotglue and connected to the appropriate wires.
The power supply is a bit tinkered. Depending on which options and components you have, the contacts for plus and minus can bead justed accordingly.
We use the springs and pins from a used battery compartment. To attach to the bottom of the housing we use wires and a little hotglue again.
The step-up boost converter is best adjusted to the approx. 5V before installation. For that the resistance has to be twisted a little bit.
Then upper and lower case are put together. Due to the fit of 0.1 mm, plugging together without further securing is sufficient. And they can still open again for a battery change.
And then it's finished!
The dice fun 2.0 can begin!
What You Need- Arduino Nano (or ESP8266)
- 2 x tilt sensors (with integrated resistors)
- 8x8 LED Matrix with MAX7219 IC, SPC
- Socket strip
- On / Off switch
- PCB
- 2 x AAA batteries
If you want do read more, check our website techpluscode.de/arduino-wuerfel-2-0.
Sorry this extended blog is available only in German ;-)
Comments