Lesson 7. An electronic dice
In picturesThere are no new chips in this lesson: everything you need is already in the library. The canvas gets a dice of seven lamps. Hold Roll — the faces flash; let go — a random one comes up.
7.1. The idea: a counter to six
A dice has six faces, so we need a counter that counts 1, 2, 3, 4, 5, 6 and back to 1. The divider from lesson 2 counts to 8 or 16 — the extra states would have to be cut off with a reset. There is a simpler way: a Johnson counter. Three flip-flops stand in a ring, each takes its neighbour's value on the edge, and the first takes the inverted value of the last. Ones fill the row from the left, then zeros push them out — six states, round and round:
- face 1:
000, face 2:100, face 3:110; - face 4:
111, face 5:011, face 6:001.
Neighbouring states differ in one bit, so the lamps do not flicker on a change.
7.2. The idea: face number → dots
A dice has only four groups of dots: the centre and three pairs. Each group is one wire:
- the centre is lit on 1, 3 and 5 — there the number has an even count of ones: two XORs from the 74HC86 and a NOT;
- pair A (a diagonal) is lit on every face except 1:
Q0ORQ2; - pair B (the other diagonal) — on 4, 5 and 6, and there
Q2= 1: the lamps go straight toQ2, no gate; - pair C (the middle) — only on 6:
Q2AND NOTQ1.
7.3. The idea: a fair roll
While you hold Roll, the 74HC08 lets the clock through to the counter. The clock is fast — the face changes hundreds of times a second, and where the counter stops is decided only by the moment you let go of the button. That is how electronic dice and roulettes in real toys work.
7.4. Building
The same project as in lesson 6: the library has all the chips of the kit. There are no new chips — the dice is built from what you already have.
Step 1. Clock and buttons
Clock — set a fast frequency: while you hold Roll, the faces flash so fast that you cannot guess. The Roll button is ROLL, Reset — RST.
| Part | Pin | Label |
|---|---|---|
| Clock | output | CLK |
| Button «Roll» | output | ROLL |
| Button «Reset» | output | RST |
Step 2. Reset and centre — 74HC04
nRST = NOT RST: the 74HC74 reset is active 0. The second NOT gives the centre dot DC (where PAR comes from — below). The other inputs go to GND.
| Part | Pin | Label |
|---|---|---|
| 74HC04 | 1A 1Y 2A 2Y 3A 4A 5A 6A | RST nRST PAR DC GND GND GND GND |
Step 3. Clock by the button — 74HC08
SPIN = CLK AND ROLL: the clock reaches the counter only while you hold Roll. The second AND is the pair of dots DD, lit only on a six.
| Part | Pin | Label |
|---|---|---|
| 74HC08 | 1A 1B 1Y 2A 2B 2Y 3A 3B 4A 4B | CLK ROLL SPIN Q2 Q1N DD GND GND GND GND |
Step 4. A counter to six — two 74HC74
Three flip-flops in a ring: Q0 takes the inverted Q2 (Q2N), Q1 takes Q0, Q2 takes Q1. The fourth flip-flop of the second 74HC74 is not needed: its CP and D go to GND, RD and SD to VCC.
| Part | Pin | Label |
|---|---|---|
| 74HC74 | 1RD 1D 1CP 1SD 1Q 1QN 2QN 2Q 2SD 2CP 2D 2RD | nRST Q2N SPIN VCC Q0 Q0N Q1N Q1 VCC SPIN Q0 nRST |
| 74HC74 | 1RD 1D 1CP 1SD 1Q 1QN 2SD 2CP 2D 2RD | nRST Q1 SPIN VCC Q2 Q2N VCC GND GND VCC |
Step 5. Centre — 74HC86
PAR = Q0 XOR Q1 XOR Q2 — 1 when the number of ones is odd. The centre is lit on faces 1, 3, 5, and there the number of ones is even, so DC = NOT PAR (that second NOT in the 74HC04).
| Part | Pin | Label |
|---|---|---|
| 74HC86 | 1A 1B 1Y 2A 2B 2Y 3A 3B 4A 4B | Q0 Q1 X01 X01 Q2 PAR GND GND GND GND |
Step 6. Pair A — 74HC32
DA = Q0 OR Q2 — lit on every face except one.
| Part | Pin | Label |
|---|---|---|
| 74HC32 | 1A 1B 1Y 2A 2B 3A 3B 4A 4B | Q0 Q2 DA GND GND GND GND GND GND |
Step 7. Seven dots
Two DA lamps, two Q2, two DD — these are pairs of dots, a pair has one wire. Put the lamps in a 3 × 3 square, like the dots of a dice (next step).
| Part | Pin | Label |
|---|---|---|
| LED | input | DC |
| LED | input | DA |
| LED | input | DA |
| LED | input | Q2 |
| LED | input | Q2 |
| LED | input | DD |
| LED | input | DD |
Step 8. Which dot goes where
Looking at the dice from above:
| Place | Lamp |
|---|---|
| ↖ top left | DA |
| ↗ top right | Q2 |
| ← middle left | DD |
| • centre | DC |
| → middle right | DD |
| ↙ bottom left | Q2 |
| ↘ bottom right | DA |
The paste puts the lamps in a column — drag them into a square, as in the picture:
7.5. Check
- Press Reset — one dot in the centre is lit: «1».
- Hold Roll and let go — the dice stops on a random face. Roll again: all faces from 1 to 6 come up.
- To see the faces in order, set
Clockto the slowest frequency and hold Roll: 1, 2, 3, 4, 5, 6, then 1 again.
| Face | Q0 Q1 Q2 | Lit |
|---|---|---|
| 1 | 0 0 0 | DC |
| 2 | 1 0 0 | DA DA |
| 3 | 1 1 0 | DA DA DC |
| 4 | 1 1 1 | DA DA Q2 Q2 |
| 5 | 0 1 1 | DA DA Q2 Q2 DC |
| 6 | 0 0 1 | DA DA Q2 Q2 DD DD |
Why the dice is fair: the clock runs hundreds of times while you hold the button — where it stops is decided only by the moment you let go.
Lesson 7 paste (Cmd+I)
On top of lesson 6.
# Lesson 7 — an electronic dice from the kit chips: a Johnson counter on 74HC74 (6 states), the dots are gates in packages.# Paste: Cmd+I on top of lesson 6 (all chips are in the library). clock(CLK)button{label="Roll"}(ROLL)button{label="Reset"}(RST)74HC04(1A=RST, 1Y=nRST, 2A=PAR, 2Y=DC, 3A=GND, 4A=GND, 5A=GND, 6A=GND)74HC08(1A=CLK, 1B=ROLL, 1Y=SPIN, 2A=Q2, 2B=Q1N, 2Y=DD, 3A=GND, 3B=GND, 4A=GND, 4B=GND)74HC74(1RD=nRST, 1D=Q2N, 1CP=SPIN, 1SD=VCC, 1Q=Q0, 1QN=Q0N, 2QN=Q1N, 2Q=Q1, 2SD=VCC, 2CP=SPIN, 2D=Q0, 2RD=nRST)74HC74(1RD=nRST, 1D=Q1, 1CP=SPIN, 1SD=VCC, 1Q=Q2, 1QN=Q2N, 2SD=VCC, 2CP=GND, 2D=GND, 2RD=VCC)74HC86(1A=Q0, 1B=Q1, 1Y=X01, 2A=X01, 2B=Q2, 2Y=PAR, 3A=GND, 3B=GND, 4A=GND, 4B=GND)74HC32(1A=Q0, 1B=Q2, 1Y=DA, 2A=GND, 2B=GND, 3A=GND, 3B=GND, 4A=GND, 4B=GND)led(DC)led(DA)led(DA)led(Q2)led(Q2)led(DD)led(DD)Stuck? Something doesn't work →