I can remember building an electronic die soldering kit as a child. I haven't seen that kit in ages, I assume its gone for good. But I can try to build a new one with a RPI Pico...
If you look at the dot pattern on the faces of a six sided die, you will notice there are a few common pairs of dots:
Take for example the dots on the 2 side, neither of those dots are every displayed individually, so we can group them together as one segment. Similarly, we can group the other 2 opposing corner dots as a segment. Here is how I wired up 7 LEDs to represent the six faces of the die:
I soldered the LEDs to a small piece of perfboard and connected it to a RPI Pico in a solderless breadboard:
Here is the micropython code I put together:
from rp2 import PIO, StateMachine, asm_pio from machine import Pin import time import rp2 import random pb = Pin(22,Pin.IN,Pin.PULL_UP) @rp2.asm_pio(out_shiftdir=PIO.SHIFT_RIGHT,sideset_init=[],out_init=([PIO.OUT_LOW]*4),fifo_join=PIO.JOIN_TX) def PIOLED(): # Actual program body follows wrap_target() pull(block) [0] out(pins,4) [0] wrap() sm = rp2.StateMachine(0, PIOLED, freq=2000000, out_base=Pin(16)) sm.active(1) ledLUT = {1:0x01,2:0x04,3:0x05,4:0x0C,5:0x0D,6:0x0E } while True: if pb.value() == 0: # display a die rolling for i in range(12): sm.put(ledLUT[i%6+1]) time.sleep(0.4) sm.put(ledLUT[random.randint(1,6)])
I am not sure how easy or difficult this code would be for a beginner. The use of a RP2040 state machine makes writing a bitfield across a few gpio pins easy.
It definitely took me longer to solder the LEDs, resistors, and wires than to write the code. The pico is pretty neat.
In the second third version, I will try using an old pic microcontroller to display the numeric value of a die on a 7-segment display.
7-Segment Display with Pi Pico
Just like in the first example of where I created an electronic die face, with a 7-segment display there exists a mapping between the visible numeric value and active display segments. I resued the existing 7-segment code table on wikipedia, but it is easy enough to work out for yourself. The 7-degment display I used in this example has the segments arranged in a common-anode configuration. So the common pin is tied to VDD. A segment is illumined by sinking current out of the segment pin, this is done by driving a gpio pin low with a series ballast resistor. Here is the schematic I used:
The code:
from machine import Pin, mem32 import time import random # Configure GP26 as input with internal pull-up pb = Pin(26,Pin.IN,Pin.PULL_UP) # Configure GP14 - GP21 as outputs PinBus = [ Pin(x,Pin.OUT) for x in range(14,22) ] def BusWrite( pbus, v ): for i in range(len(pbus)): pbus[i].value( v & (1<<i)) # Seven segment LUT table for numerical readout # bit0 - segA, bit1 - segB,... bit7 - segDP SGLUT = [ ~x for x in [0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71]] def displaySevenSeg(v): BusWrite(PinBus,SGLUT[v])#14,21,SGLUT[v]) BusWrite(PinBus,0xff) while True: if pb.value() == 0: # display a die rolling for i in range(30): BusWrite(PinBus,~(1 << (i%6))) time.sleep(0.035) displaySevenSeg(random.randint(1,6))
The electronic die in action:
Thanks for reading :)