Lesson 7. An electronic dice

In pictures

There 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.

D Q074HC74D Q174HC74D Q274HC74Q2N — Q2 inverted000100110111011001
A Johnson counter: three flip-flops in a ring, the first gets the last one inverted. Ones fill the row, then zeros push them out — six states (labelled Q0 Q1 Q2), six faces, and back to 1.

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: Q0 OR Q2;
  • pair B (the other diagonal) — on 4, 5 and 6, and there Q2 = 1: the lamps go straight to Q2, no gate;
  • pair C (the middle) — only on 6: Q2 AND NOT Q1.
DC · centreNOT (Q0 XOR Q1 XOR Q2)faces 1, 3, 5DA · pair ↖↘Q0 OR Q2faces 2, 3, 4, 5, 6Q2 · pair ↗↙Q2 itselffaces 4, 5, 6DD · pair ← →Q2 AND Q1Nfaces 6
The seven dots of the dice are only four wires: the centre and three pairs. Each wire is one or two gates from Q0 Q1 Q2.

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.

PartPinLabel
ClockoutputCLK
Button «Roll»outputROLL
Button «Reset»outputRST

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.

PartPinLabel
74HC041A
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.

PartPinLabel
74HC081A
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.

PartPinLabel
74HC741RD
1D
1CP
1SD
1Q
1QN
2QN
2Q
2SD
2CP
2D
2RD
nRST
Q2N
SPIN
VCC
Q0
Q0N
Q1N
Q1
VCC
SPIN
Q0
nRST
74HC741RD
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).

PartPinLabel
74HC861A
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.

PartPinLabel
74HC321A
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).

PartPinLabel
LEDinputDC
LEDinputDA
LEDinputDA
LEDinputQ2
LEDinputQ2
LEDinputDD
LEDinputDD

Step 8. Which dot goes where

Looking at the dice from above:

PlaceLamp
↖ top leftDA
↗ top rightQ2
← middle leftDD
• centreDC
→ middle rightDD
↙ bottom leftQ2
↘ bottom rightDA

The paste puts the lamps in a column — drag them into a square, as in the picture:

DAQ2DDDDQ2DADC
This is how the lamps stand on the canvas: a 3 × 3 square, the left column labels on the left, the right column labels on the right; the centre DC label can go below. Face 5 is lit: DA, Q2 and the centre DC.

7.5. Check

  1. Press Reset — one dot in the centre is lit: «1».
  2. Hold Roll and let go — the dice stops on a random face. Roll again: all faces from 1 to 6 come up.
  3. To see the faces in order, set Clock to the slowest frequency and hold Roll: 1, 2, 3, 4, 5, 6, then 1 again.
FaceQ0 Q1 Q2Lit
10 0 0DC
21 0 0DA DA
31 1 0DA DA DC
41 1 1DA DA Q2 Q2
50 1 1DA DA Q2 Q2 DC
60 0 1DA 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)

¿No te sale? Algo no funciona →