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

How to build a 3-bit Gray code counter

A Gray code counter steps through 000, 001, 011, 010, 110, 111, 101, 100 and back to 000, so exactly one bit changes on every clock. This live one uses three D flip-flops and next-state logic read off K-maps: D2 = Q1 Q0' + Q2 Q0, D1 = Q2' Q0 + Q1 Q0' and D0 = Q2 XNOR Q1.

A binary counter can change several bits at once, as when 011 becomes 100, and in real hardware those bits never switch at exactly the same moment, so anything reading the count mid-change can see a value that was never there. A Gray code counter changes one bit per step, so a reading is always either the old value or the new one. That is why the read and write pointers of an asynchronous FIFO, which cross between two clock domains, are usually kept in Gray code, and why rotary encoders use it. The design is the standard one for any synchronous state machine: list each state and its successor, make a K-map for each next-state bit, and wire the minimized equations into the D inputs. The other way to build one is a binary counter followed by a binary to Gray converter, which costs two XOR gates but loses the one-bit-change guarantee at the converter's inputs. Two full laps of the eight codes are checked, every output at the end of every cycle, before the page is built.

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, an input CLR, three D flip-flops with clear, and probes G2, G1 and G0 on their Q outputs.
  2. Write the state table: 000 goes to 001, 001 to 011, 011 to 010, 010 to 110, 110 to 111, 111 to 101, 101 to 100 and 100 back to 000.
  3. Minimize each next-state bit with a K-map and wire it: D2 = Q1 Q0' + Q2 Q0, D1 = Q2' Q0 + Q1 Q0', D0 = Q2 XNOR Q1.
  4. Connect CLK and CLR to all three flip-flops, pulse CLR, then run the clock and watch one output change per edge.

The count, edge by edge

Edges since CLRG2 G1 G0Bit that changed
00 0 0(start)
10 0 1G0
20 1 1G1
30 1 0G0
41 1 0G2
51 1 1G0
61 0 1G1
71 0 0G0
80 0 0G2

Every row differs from the one before in exactly one bit, including the wrap from 100 back to 000. Read as binary the sequence is 0, 1, 3, 2, 6, 7, 5, 4; the Gray code converter turns any code back into its count.

You can also read the full theory in the lesson on Gray code. 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)Mealy 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 subtractor

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 3-bit Gray code counter in the lab →

Frequently asked

What is a Gray code counter?

A counter whose output follows the Gray code sequence, in which consecutive values differ in exactly one bit, instead of ordinary binary. A 3-bit one counts 000, 001, 011, 010, 110, 111, 101, 100 and repeats.

Why use a Gray code counter?

Because only one bit changes per step, a reader that samples the count while it is changing gets either the old or the new value, never a mixture. This makes Gray counters the usual choice for FIFO pointers that cross clock domains, and it also avoids decoding glitches.

How do you design a Gray code counter?

Treat it as a state machine: write each state and its next state, draw a K-map for each next-state bit, and use the minimized equations as the D inputs of the flip-flops. For 3 bits that gives D2 = Q1 Q0' + Q2 Q0, D1 = Q2' Q0 + Q1 Q0' and D0 = Q2 XNOR Q1.

Can I make a Gray counter from a binary counter?

Yes: feed a binary counter's outputs through a binary to Gray converter, where G_i = B_i XOR B_(i+1) and the top bit passes straight through. It is simpler to design, but the converter's inputs still change several bits at once, so the output can glitch briefly between steps.