Build an Electronic D6 Using a 74HC595 Shift Register


After playing around with the 74HC595 shift register, I got it into my head to see if I could make an electronic version of six sided dice. I didn’t do too much in the way of project specification, system requirements or anything like that… I actually just thought “I wonder if I can do this”. So I write it up (page one, page two).

The general concept was to make a pattern of seven LEDs so that I could display the value of any side of a D6, then use the Arduino random number generator to come up with the number to display. I knew already that it wouldn’t be as simple as just sending the D6 face value to the shift register, because sending 1-6 would display strangely, it would have to be some decimal equivalent of the binary pattern. That’s a lot of words, and I’m not sure they make sense to anyone but me, so, if you haven’t read the first two notebook entries, Shift Register Circuit Building 101 and Shift Register Circuit Building 102, it might help if you do.

Electronic D6 Schematic v1
Electronic D6 Schematic v1

Building this circuit doesn’t require any more parts than the 101 or 102 circuits do, in fact, it needs less parts, because you only have seven pips on a die, so that eighth LED and it’s accompanying resistor aren’t needed. However, for it to make visual sense, you do have to arrange the LED’s in the pattern of the dice pips. The first arrangement I made had the LEDs numbered in horizontal rows.

If you’re building this immediately after the 102 circuit, tie OE back to ground and SRCLR back to +5V.

I put together a table that I thought would correctly display the value of a number between 1 and 6 as a dice pattern. Unfortunately that table was dramatically incorrect. Here it is…

Electronic D6 Pattern

ValueLED07LED06LED05LED04LED03LED02LED01Decimal Equiv.

Now how my brain came up with this, I cannot say – it’s so wrong, it’s baffling. Basically, I had determined that I could simply take the position numbers for each of the LEDs that would light up for each dice pattern, add those position numbers together, then send that number to the shift register. So for the number 2, the LED in position 1 and the LED in position 7 would need to light, so I’d need to send the decimal number 7 to the shift register. This is as logical as thinking “I should just send the smell of frying bacon to the shift register.” When I first loaded up the program, the dice patterns look completely whacky. What you need to do is determine what the bit position is for each LED, and then add up the binary value for each bit instead.

Electronic D6 Pattern

ValueNO LEDLED07LED06LED05LED04LED03LED02LED01Decimal Equiv.

With the corrected values, the code worked a charm! Well, almost. The random number generator function provided for the Arduino is a little tricky. My original code was random(1,7) which I believed would generate a random number between 1 and 6. First go round, it seemed pretty damn random. However, every time you restart the sketch, it would follow the exact same pattern again, here are the first 10 rolls: 2, 2, 6, 3, 5, 3, 1, 3, 6, 2.

I don’t know how random number generators work because I’ve never bothered to care, really. So after doing a bit of research, it turns out you have to seed the generator with some value in order for it to be random from one reset to another, and this is done with the randomSeed() function. The easiest way is to pass randomSeed() some value that fluctuates, like the voltage level of one of the analog inputs if it’s disconnected, because those suckers float all over the place. Putting randomSeed(analogRead(0)); into your setup code makes the generator significantly less predictable.

The code for this layout is shown here…

Here is the fully assembled circuit on a breadboard. I’ve moved the LED’s off the primary breadboard onto their own, and bent the legs of LED-4 so that it will sit generally middle-ish.

The more I played with this though, the more I realized that using a different layout pattern for the LED’s would allow more elegant code to be used to do thinking patterns and what not. Instead of numbering them horizontally as above, I chose to reconnect them in a circle, clockwise from the top left LED. That positioning looks like this:

Electronic D6 Pattern v2

However, moving the LEDs to different pins on the shift register means redoing the table of values. Consequently, using the circle pattern, the new decimal equivalent values are…

ValueNO LEDLED07LED06LED05LED04LED03LED02LED01Decimal Equiv.

This also requires a change to both the schematic and the code.

Electronic Dice Roller Schematic Modification
Electronic Dice Roller Schematic Modification

Basically all you need to do is modify which outputs from the shift register you have leading to which LED.

In the new code, I’ve added a couple of commented out “thinking patterns” for you to play with. This new layout also corresponds to the schematic I used for the first version of the Project 1 circuit board, and the code should still be compatible.

If you feel like playing around further, note that the SRCLR and OE pins are tied to the rails again. You could use the OE pin to enable fading effects in the thinking and display pattern functions, by tying it to a PWM pin instead of the ground rail. The circuit is also screaming out to have the use of the serial monitor removed and replaced with a simple tactile button to initiate a roll. You could easily replace the LED array with a seven segment display too.

Click here for the files associated with this post at the Rheingold Heavy GitHub repo.