Introduction: Arduino Oled Dice

This instructable is about how you can built a very nice looking electronic dice using an oled display and an Arduino uno or similar.
At the very beginning of this project I decided that after the prototype was finish I wanted to build a custom made version, so in this inscrutable there is descriptions how to build the prototype version and useful tips if you want to build your own custom version.

The video shows the final custom made dice version and functions.

Step 1: Dice Functions

The dice have a select switch to choose between 1 or 2 dices., it also have a piezo element to generate sound when the dice is running trough random numbers and when it stops. As long as the roll switch is kept pressed the dice runs and show random numbers on the display. When the button is released it starts to slow down a random number of time until it finally stops and show the results. This is to simulate the behavior of a real rolling dice.

The Dice has an Auto Power Off circuit to save batteries.

If you don´t use the dice for 60 Sec the power automatically shuts off.

In the software there is a function to switch the sound On or Off by holding the select switch down for more than one second.

Step 2: The Auto Power Off Funtion

The dice has a function to shut off itself when not used to save batteries, see the schematics for the auto power off circuit.

This is how it works:

The circuit consist of a P FET transistor that is acting like a switch. The gate on the transistor is controlled by a standard momentary push button (S1). When the switch is pressed the voltage drops on the gate and the current starts to flow trough the transistor. On the gate there is an other transistor in parallel with the switch to ground. The transistor keeps the voltage low on the FETs gate as long as the voltage on the base is High. The base voltage is applied from the micro controller and one of the first thing the sketch do when the controller is powered is to set the digital pin 8 to High and by the software latch the circuit. The 7805 voltage regulator stabilise the voltage to 5V and the two diodes prevents the 9 volt from the battery to reach the micro controller. The same switch is also used to control the digital input on the controller,(pin 7).

In the sketch we measures the time elapse since the button was pressed and compare it with the determined ON time.

Before the power goes off the dice/ dices start to blink and a warning signal is emitted from the piezo so the user has time to push the switch again to reset the timer.

Just before the power goes off the microcontroller store the latest number into the EEPROM memory together with the selected number of dice/dices and the sound state. Those values is recalled upon the next start of the dice.

Step 3: The Prototype

Now it´s time to start building.

You need :

That´s it.

Follow the fritzing picture above carefully.

Pay extra attention to the diode behind the voltage regulator in the picture (hard to see), D1 in the schematic. The Anode side of the diode should be connected to the collector of the BC547 transitor.

The Piezo is connected to pin 6, Roll button to pin 7, Select button to pin 10 and the Power_ON control to pin 8.

Don´t forget to power your Arduino Uno through the 5V pin and the ground pin on the Arduino board and not trough the dc jacket on the side.

The sketch uses the U8g2lib.h for the display, you find it here, ,download and install before compiling the code.

How to install libraries?

Copy the code and paste it in Arduino IDE and upload the sketch.

Don´t forget to remove the USB cable from the Arduino when finish otherwise the auto power off function will not work because the USB/Computer is powering the controller.

Step 4: The Custom Version.

The rest of this instructable is about tips and trix, if you want to convert it to a more useful and custom version.

To draw the complete schematic for the custom version I used the free online schematic and PCB software EASYEDA You find it here

When ordering the components you need to be sure that the microcontroller has the Arduino bootloader on the chip, if not, you have to prepare the chip first. It ´s lots of tutorial on the web how to do it.

I added extra components that is not used in this project but is there for future projects. U4, U5, R4, S2.

The PGM Header in the schematics is used for programming the chip. If you want to program the chip using the PGM port you need an USB to Serial adaptor.

USB to serial UART Boards

You can of course upload the sketch to the controller using your Arduino board and then move the chip to the PCB instead.

EASYEDA also provide a function to fabricate the PCB for you.

Before I begun to convert the schematic into the PCB layout I was choosing a box that has the right size and room for a 9 volt battery that is changeable from the outside.

The reason for that was that I needed the dimensions and where to place the hole on the PCB for the screws before I started to do the layout, so the final PCB will fit perfectly into the box.

I measure the internal dimension off the box very carefully and then convert the design in to the custom size board using the same software and then click the fabricate button and placed an order.

Step 5: Soldering

Because I have to order more than one PCB to get a reasonable price I design it to be versatile so I could use the same board and box for future projects. I added extra pins for analogue and digital ports along with extra buttons.
In this project I use the S1 to power up the circuit and to roll the dice, and S3 as Select .
When you received the PCB it is time to soldering all the the components in the right place. On my PCB the display and buttons is mounted on the backside to shrinking the size and to be reachable from the outside.

When I was building my dice I realised that it would be nice if you could just shake the box to powered it up and roll the dice.
If you want that feature you need to do a small modification to the circuit.


Changed the roll switch (S1), to a tilt switch sensor and add a 100uF capacitor in parallel to the switch to hold the level on the FET gate low long enough so that the micro controller has time to start and set the digital out port HIGH and latch the “power on” circuit.

You need to mount the tilt sensor on extension pins so you can bend it and adjust the angle so the switch is off when the box is lying on the table.


Step 6: Cut Out the Holes Needed in the Box

When finished with the PCB it´s time to drill the holes into the box. To cut the square hole for the display I used a micro miller, but you can off course use a small jig saw or similar.

Step 7: The Frontpanel

Then you need a nice front panel.
I was drawing the panel in “smart-draw software” but you can use almost any drawing software that you like.

When you are finish with the drawing, print it out on a standard colour laser printer or similar, but on a little thicker paper than normal. Take a plastic sheet that has glue on both sides. Remove the protecting sheet of one side and carefully paste the panel. you can find this plastic film in most paper stores.

Step 8: Cutting Holes in the Panel.

Cutout the holes in the panel with a sharp paper knife.

For the round button holes, use a hole punch. Now the panel looks like a regular sticker, but before you paste it on to the box you need to spray coat it with a protecting layer of lacquer.

When the panel has dried, paste it carefully on to the box.

Step 9: The End of the Project

When I was reaching the end of this project I unfortunately find that the dice sometimes freezes when I shake it and needed to be restarted.

I've never had this issue during prototyping so I was a little confused but I found that this was due to noise projected on the SDA, SCL pins of the display.

The solution was to add to extra resistors 1k on each pin to 5V as pull-up,see the picture.After that the dice work perfectly as expected.

Step 10: Shake and Roll

Have Fun.