Intro: How to Make an Electronic Dice Roller
This post details a project I finished last summer, an electronic dice roller. In this post, I'll go over how and why I created it, and how you can make one too!
Step 1: Why Build an Electronic Dice Roller?
My goal for this device was to display the result of a simulated "dice roll", allowing for easy and quick generation of the sum of random numbers from 1 to n, where n is chosen by the user. This can be helpful in many role-playing games such as Dungeons and Dragons, where there are many times you need to roll something like two six sided dice (2d6), one twenty-sided dice (1d20), or three eight-sided dice for example (3d8).
So, why don't you get your nerd on and keep reading the instructable!!
Step 2: Planning Your Design
Here, you can see some of my planning process. I originally planned on using some red 7-segment LED displays, but opted for the greater versatility of a small OLED display in the end.
Please note, that you can do whatever you want!
My finalized design employed the use of a 128x64 pixel OLED display, mainly because of its flexibility: I could easily change my graphical interface as opposed to, say, seven-segment LED displays which severely limit the number of digits that can be displayed. They also are more complicated to use because you need many output pins and therefore would be forced to add extra components such as shift registers to make it work with a microcontroller of a limited number of pins.
The screen I got on Amazon is also is dual color, in a sense. The first 16 rows are yellow, and the rest blue. This was perfect for my application because it fits my graphical layout (number of dice rolled at top in yellow, and blue for the larger text result). Plus, it just looks nice.
Step 3: Finalize Your Design and Make Electrical Schematic
My final sketched schematic and plan for laying out components.
Apart from viewing the screen, the user interacts with the dice roller by pressing the "roll" button (the red upper button) which rolls the selected number of dice with the selected number of sides, or the "add" button (the yellow lower button) which adds a new roll to the number already displayed on the screen. The number of dice and modifier can be seen at the top yellow portion of the screen, and the roll result in a blue larger font at the bottom. If the result is greater than three digits, an error message is shown, as only three digits can fit on the screen.
Step 4: Materials
- 5V Linear Voltage Regulator (LM317)
- 2 x 22pF Capacitors
- 16Mhz Crystal Oscillator
- 10 uF Capacitor
- 2 x Momentary push buttons
- Atmega 328 Microcontroller
- 1 x Rotary Encoder with integrated pushbutton
- Knob for Rotary Encoder
- 3 x 10 kOhm Resistors
- 128x64 pixel OLED display
- Solder and wire for connections
- Prototype Board
- 9V Battery snap leads
- On/Off switch (SPST) Case:
- Hot glue
- Dremel for cutting protoboard and creating case
- Scrap metal for case
- Black tape
- Thin tubing for spacers
Step 5: Prototyping on a Breadboard
Note: this image doesn't show my full setup while testing, just when I was testing the screen and single pushbutton only. This is actually a good way to go about things when starting a project from scratch. Make sure that you can use a rotary encoder on its own, a screen on its own, and buttons on its own. At that point you can add them all together into your code when you're sure each part on its own works.
Breadboarding is a very important step. Most of the mistakes (at least that I make) are worked out at this point in the project. With a breadboard, you can easily make electrical connections and hook up all your components in an organized fashion and test that everything works as expected. I connected my rotary encoder, screen, and buttons to my Arduino Uno.
Step 6: Programming the Arduino
Now that you've got everything connected up to a breadboard, you're ready to put your program together.
What I needed it to do: interface with a screen, read the input from two buttons, and adjust values based on a rotary encoder.
Luckily, communicating with the screen was made quick and simple because of Adafruit's Libraries. (SSD1306 for interfacing with the driver chip for the screen, and their GFX library for quickly and easily adding text or shapes to the display.)
The rotary encoder used external interrupts, which are a very useful feature that the ATMega328 has and the Arduino IDE has support for. It allows code to be ran only when a pin changes, and otherwise will not slow down the main loop() program by polling with the digitalRead() function. You can read more about this useful feature here: http://playground.arduino.cc/Code/Interrupts
I started with getting the OLED display to work. Luckily adafruit has a library (link) to easily interface with the display I was using (the SSD1305) and adafruit has a lot of information about it here: https://learn.adafruit.com/monochrome-oled-breakou...
After laying out my text and roll numbers and results where I wanted to, I made it so the user could be in different selection modes to change the number of dice to roll and the number of sides. I used a random seed of the number of milliseconds since the device was on, and generated a random number each time they pressed the roll button. I also had the screen refresh only when a number was changed, to reduce unnecessary CPU overhead.
Feel free to find and use my code on my github repository at: https://github.com/tedmyers/Dice_Roller
For this project, I didn't want to spend much time setting up a breadboard and plugging in an in-system programmer. So I just popped out the Atmega328p microcontroller in my Arduino Uno, and placed it in my final circuit. If you do this, remember to include a 16MHz crystal oscillator with the necessary 22pF capacitors on each pin.
Step 7: Placing Components
After prototyping my design with my Arduino Uno and a breadboard, I arranged each part on a proto board. I also decided to use a bare ATMega328 chip because of its ease of use from prototyping on my Uno and integrating it into my final circuit. Because it is the chip that runs my Arduino, putting it in my final circuit is as easy as popping the chip out, soldering the connections in place, adding a 16Mhz crystal oscillator and 22pF capacitors, and adding a pull-up resistor to stop the reset from activating.
From the beginning, I wanted this project to run on an external battery, and a 9V battery seemed perfect for this task. I used a 5V linear voltage regulator to supply the microcontroller and display with the correct voltage. Note: this is not the most efficient method possible, as energy is lost in the form of heat in the regulator. However, it was good enough for me in the project.
I added a 10uF capacitor on the output because the regulator needs it for stability reasons, and a 0.1uF decoupling capacitor, though the second one may not be necessary.
Finally, I soldered everything in place and tested my connections before plugging in a battery.
Step 8: Solder Components and Cutting to Size
Finally I added the screen, which partially covers the voltage regulator and microchip to keep the device as compact as possible. After that was done (and after testing to make sure it all actually worked), I cut the extra protoboard to size, to make it fit better inside a case.
I also connected a power switch in series with the battery source to allow it to be turned on and off. And finally it works! A small, compact, self-contained package that now does exactly what I want: to roll dice. All I need to do is build a case to house my newly-soldered electronics.
Step 9: Assembling the Case
Instead of using a generic black box housing, I wanted to make my own unique case. I also liked the idea of making it out of metal and luckily I had some scrap laying around from taking apart an old fax machine.
The scrap metal I used can be seen in the bottom right of the image (the long grey rectangle with slots cut out) To shape the metal to the case, I started by using calipers to measure my electronics, and made a 2d CAD image of the screen, knob, and button layout in SolidWorks to aid in cutting in the right spots. Next, I printed this on a piece of paper at 1:1 scale, cut the paper to shape, and taped my image onto the metal sheet. After using a marker to transfer the cutout shapes over, I had a template to cut holes for the case that were placed in the exact right spots.
I then used a dremel with a cutting wheel attachment to make the hole for the buttons and screen, and a large drill bit to make the circle for the knob. If I were to redo this step, I would have drilled out my button holes as well, as it is very difficult to make square holes of the size I needed, and I ended up using round LEDs to reach the buttons anyway. Oh well, now I know how to do it better next time. ¯\_(ツ)_/¯
After I cut the holes in the front plate of the case, I cut a piece for the back and used my dremel with a different attachment to sand away the metal and change the finish from a matte look to a snazzier brushed metal look.
Next I needed to find something to reach the buttons, and needed some sort of spacer to stop the whole thing from squishing together too much and shorting the electrical connections. I made the spacers out of fairly small aluminum tubing (1/8in I think), and hot glued them in place.
I ended up using red and yellow LEDs not as a source of light, but to reach the momentary buttons soldered to the board. These actually worked astonishingly well, after some modifications. First I cut off their leads and added a thin ring of hot glue around their base as a way to prevent the LEDs from sliding out of the case when it was fully assembled. One thing I actually could have done is make the buttons themselves light up when pressed, but at this point in the project I just wanted to have a finished project. Maybe I'll make some cool light-up buttons on the next thing I make.
This isn't the most rugged case, but it should do for my purposes. I spaced the back far enough to easily fit a 9V battery, hot glued the switch to the front side of the case, and closed it all up with black gaffer's tape.
Step 10: Final Product!
The device in use: first rolling 1d20 (the default value), then the user changes it to 2d8-3, rolls a few times, and then presses the "add" button to add a number of rolls together.
The values for number of dice rolled, number of sides, and the modifier added or subtracted can be easily changed by using the knob: when pressed, it cycles which area is highlighted, and the value can be modified by turning the knob clockwise or counterclockwise. This makes changing settings quick and intuitive, and allows for very quick rolling of various amounts of dice.
Thanks for reading, and I hope you have a great time building one of your own!
Edit: I also made a blog post about this project on my website, which you can find at the following link: https://tedfmyers.com/2015/08/20/electronic-dice-roller/