Skip to content
digiwleeaTools
LearnGlossaryAboutCheat sheetHow a CPU worksToolsOpen the lab →

Electronic dice circuit: a counter and seven LEDs, live

An electronic dice is a fast counter that runs through 1 to 6 while you hold a button and stops when you let go, plus a decoder that lights the seven LEDs of a die face. This live one counts on CLK while ROLL is 1 and holds while ROLL is 0; its seven outputs are the pips TL, TR, ML, C, MR, BL and BR.

The randomness comes from you, not the logic. A real electronic dice clocks its counter thousands of times a second, far too fast to time the moment you let go, and the counter visits all six faces equally often, so each face is equally likely. Here the clock is slow enough to watch. The counter is three D flip-flops counting 0 to 5 (shown as faces 1 to 6), like the mod-6 counter: Q0 toggles every step, Q1 = NOT Q2 AND (Q1 XOR Q0) and Q2 = Q1 Q0 OR Q2 NOT Q0, so 5 (101) is followed by 0. ROLL chooses, for each flip-flop, between that next count and its own present value, the same 2:1 choice a register with load makes, so the count simply holds while ROLL is 0. The decoder is smaller than it looks, because die pips light in pairs: the centre is on for the odd faces 1, 3 and 5; top left and bottom right for every face from 2 up; top right and bottom left from 4 up; and the middle pair only on 6. Four signals drive all seven LEDs: centre = NOT Q0, the first diagonal = Q2 OR Q1 OR Q0, the other diagonal = Q2 OR Q1 Q0, and the middle pair = Q2 Q0. Another display decoder works the same way for digits: the BCD to 7-segment decoder. The live circuit opens with CLR high, showing a 1 (just the centre): set CLR to 0 and ROLL to 1 to roll, then ROLL to 0 to stop. Before the page is built, 19 clock cycles are checked on all seven pips: clearing, rolling through every face and around again, holding, and rolling on.

Big circuit: pinch, scroll or tap + to zoom in, then drag to move around.

Live circuit: click the inputs to toggle them and watch the outputs update. It runs the real simulator, right here on the page.

Open this circuit in the lab →

What you need

Step by step

  1. Place a clock CLK, inputs ROLL and CLR, and seven probes for the pips: TL, TR, ML, C, MR, BL, BR.
  2. Build a counter that goes 0, 1, 2, 3, 4, 5 and back to 0 on each clock edge, cleared by CLR.
  3. Put a 2:1 choice in front of each flip-flop: the next count when ROLL is 1, its own Q when ROLL is 0.
  4. Decode the pips: C = NOT Q0, TL and BR = Q2 OR Q1 OR Q0, TR and BL = Q2 OR (Q1 AND Q0), ML and MR = Q2 AND Q0.

Faces and pips

Count (Q2 Q1 Q0)FaceTL and BRTR and BLML and MRC
00010001
00121000
01031001
01141100
10051101
10161110

TL, TR, ML, C, MR, BL and BR are top left, top right, middle left, centre, middle right, bottom left and bottom right. The counts 110 and 111 never come up in use; if one did, the next-state logic would send 110 to 101 and 111 to 100, both inside the loop, so the dice cannot get stuck.

You can also read the full theory in the lesson on counters. Prefer to build it yourself from scratch? Open the digiwleea lab and place the gates on the canvas, or start from the logic gate simulator.

Build more parts from gates

half adderfull adderhalf subtractorfull subtractor2:1 multiplexer4:1 multiplexerXOR gate from NAND gates2-to-4 decoderSR latchD flip-flopJK flip-flopT flip-flop4-bit shift register4-bit ripple carry adderD latch1-to-4 demultiplexer4-bit magnitude comparator4-bit ring counter4-bit Johnson counter4-to-2 priority encoder4-bit synchronous counter3-to-8 decoder8-to-1 multiplexer4-bit carry lookahead adderhalf adder from NAND gates2-bit comparatorBCD to 7-segment decoder4-bit up/down counterdecade counter2-bit multiplier4-bit ALU4-bit array multiplierNOT, AND, OR and NOR from NANDNOT, OR, AND and XOR from NORfull adder from NAND gatesSR latch from NAND gatesparity generator and checker4-bit adder/subtractorBCD adder4-bit universal shift registermod-6 counter4-bit ripple counterbinary to Gray code converterhalf adder from NOR gatesfull adder from NOR gates4-bit register with parallel load1011 sequence detectorbinary to BCD converterD flip-flop from NAND gates1-bit ALUCMOS inverterCMOS NAND gateCMOS NOR gatePISO shift registerSR flip-flop from NAND gatesBCD to excess-3 code converterHamming (7,4) code encoder and correctortraffic light controller4-bit barrel shifterCMOS AND gateCMOS OR gateCMOS XOR gate1-bit magnitude comparatorhalf subtractor from NAND gatesfull subtractor from NAND gates8-to-3 encoderGray to binary converter4-to-16 decoderfull adder using 4:1 multiplexersedge detectorserial adder4-bit carry-select adder4-bit Kogge-Stone adder4-bit carry-skip adder4x4 Wallace tree multiplier8-bit adderfrequency divider (by 2, 4 and 3)switch debouncer (SR latch)3-bit Gray code counterMealy machine (1011 detector)master-slave JK flip-flop (NAND)half subtractor from NOR gates1-to-8 demultiplexerexcess-3 to BCD code convertervending machine FSM4-bit carry-save adder3-bit synchronous counter (JK)CMOS transmission gatetri-state buffer (CMOS)2-to-1 mux from transmission gatesmod-5 counterflip-flop conversion (JK to D)8-bit ALUdecimal to BCD encoderBCD to decimal decoder4-to-1 mux from 2-to-1 muxesXOR gate from NOR gates4-bit LFSR74LS138 decoderBCD subtractor74181 ALUCMOS AOI gate (AOI21)D latch vs D flip-flop74LS47 BCD to 7-segment decoder74HC595 shift register7400 quad NAND gate chip7402 quad NOR gate chip7404 hex inverter chip7408 quad AND gate chip7432 quad OR gate chip7486 quad XOR gate chip7474 dual D flip-flop74HC165 shift registerCD4017 decade counter74LS283 4-bit adder74LS90 decade counter74HC161 4-bit counter74HC151 8-to-1 multiplexer74HC157 quad 2-to-1 multiplexer74HC139 dual 2-to-4 decoderCD4511 BCD to 7-segment decoder7492 divide-by-12 counter7493 4-bit binary counter74HC244 octal bufferRace-around condition (JK latch)74HC373 octal latch74HC374 octal D flip-flopTurnstile state machine1-to-2 demultiplexerhalf adder using 2:1 multiplexersregister file (4 x 4-bit)digital PWM generatorCD4013 dual D flip-flop74LS76 dual JK flip-flop7410 triple 3-input NAND chip7411 triple 3-input AND chip7420 dual 4-input NAND chipCD4011 quad 2-input NAND chipCD4001 quad 2-input NOR chipCD4081 quad 2-input AND chipCD4069 hex inverter chip74HC164 8-bit shift register7485 4-bit magnitude comparator74HC125 quad tri-state bufferCD4040 12-stage binary counterCD4060 14-stage counter and oscillator74LS193 4-bit up/down countermemory address register (MAR) and MDRhardware stack with a stack pointerdirect-mapped cache8-bit Brent-Kung adder4x4 Dadda multiplierbinary clock circuit

Each of these is one step from a transistor to a working CPU. Build every gate from CMOS transistors and work all the way up in the free digiwleea lab, with the free theory reference alongside.

Open the electronic dice circuit in the lab →

Frequently asked

How does an electronic dice work?

A counter cycles through six states while a button is held, and a decoder lights the LEDs of the matching die face. Releasing the button stops the counter, and whichever face it stopped on is the roll.

Why is an electronic dice random?

Because the counter runs far faster than anyone can time a button release, and it spends equal time on each of the six faces. The roll depends on exactly when you let go, which you cannot control to a thousandth of a second.

How many signals does a dice display need?

Four. A die's seven pips light in groups: the centre alone, two diagonal pairs and the middle pair, so four decoded signals drive all seven LEDs.

Why does the counter go from 0 to 5 instead of 1 to 6?

Starting at 0 means a clear (all flip-flops 0) gives a valid face, and the decoder simply maps counts 0 to 5 onto faces 1 to 6.