Built-in project · no microcontroller

Counting clock

A program counter built from NAND gates, counting one step clock tick at a time, with its LOAD tied to 0 by a constant bit. There is no microcontroller in it: the simulator solves the 6 parts as a circuit, so it runs the moment you press Run, and nothing needs compiling.

Teaching? Assign it to your class in one click.

Intermediate About 15 minutes to build and run. Runs in your browser. Free, and no account needed.

The circuit runs here once the page has loaded.

What you'll learn

  • A program counter holds a number and adds one per clock tick
  • Reset and load inputs control a counter
  • Binary numbers on 8 wires
  • A whole chip can be built from NAND gates
  • Difficulty: Intermediate
  • Time: About 15 minutes
  • Parts: 409

How it works

This is a circuit with no code. A program counter, the part of a CPU that remembers which instruction comes next, is built from 400 NAND logic gates inside one chip. It holds a number and adds one each time the clock ticks.

The step clock drives the CLK input. The bit switch on RESET starts at 1, so the first tick loads 0. Flip RESET to 0 and every click of the clock adds one to the 8 bit output, shown on the number display.

The LOAD input is tied to 0 with a constant bit, so the counter never jumps. Wire a switch there instead and a tick would copy the number switch into the counter. That is how a CPU jumps to another instruction.

Parts list

409 parts, plus the jumper wires. Every one is in the editor's parts bin.

  • 1 × Number switch
  • 5 × Constant bit
  • 1 × Bit switch
  • 1 × Step clock
  • 400 × Logic gateIn Logic Lab Kit.
  • 1 × Number display

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Part and pinConnects to
PC chip pin IN0Number switch pin D0
PC chip pin IN1Number switch pin D1
PC chip pin IN2Number switch pin D2
PC chip pin IN3Number switch pin D3
PC chip pin IN4Number switch pin D4
PC chip pin IN5Number switch pin D5
PC chip pin IN6Number switch pin D6
PC chip pin IN7Number switch pin D7
PC chip pin LOADConstant bit pin Q
PC chip pin RESETBit switch pin Q
PC chip pin CLKStep clock pin CLK
Number display pin D0PC chip pin OUT0
Number display pin D1PC chip pin OUT1
Number display pin D2PC chip pin OUT2
Number display pin D3PC chip pin OUT3
Number display pin D4PC chip pin OUT4
Number display pin D5PC chip pin OUT5
Number display pin D6PC chip pin OUT6
Number display pin D7PC chip pin OUT7

The notes that come with it

There is no microcontroller in this one, so nothing is compiled: the simulator solves the circuit itself. The file in the editor is the notes that come with it.

sketch.ino
// A program counter, built from NAND gates, counting on a step clock.
//
// Press Run. RESET starts at 1: click the clock once and the counter loads 0.
// Click RESET to 0, then click the clock: every click is one tick, and every
// tick adds one. Set the clock's mode to run and it counts by itself.
//
// LOAD is tied to 0 by a constant bit, so the counter never jumps. Wire a
// switch to LOAD instead and a tick with it on loads IN, which is how a CPU
// jumps to another instruction.
//
// The PC chip is a REG8, an INC8 and two MUX8s. Unpack it to look inside.

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • The display stays at its old number This project
    Click the step clock once. Nothing changes until a clock tick arrives.
  • It keeps showing 0 This project
    RESET is still 1. Switch it to 0, then click the clock.
  • The count looks reversed. The bit order is read the wrong way, with bit 0 as the least significant. Number switch
    Check which pin is bit 0 and wire it to the lowest input of the chip. Test with a value like 1, where only the least significant bit is high.
  • Chip inputs are left floating, or the switch signals are wired without a common ground. Number switch
    Connect the number switch output to every input you use, and tie the unused inputs high or low as the chip needs. Join the grounds.
  • An input is left unconnected and acts randomly. Constant bit
    Tie every unused input to a constant 0 or 1. A floating input on a logic chip can read either value or oscillate.
  • A constant is used where a different logic level is needed. Constant bit
    Check that the 1 matches the supply of the chip it connects to. A 1 here means the logic high, not a specific voltage by itself.
  • The chip input does not respond to the switch. Bit switch
    Check that the switch ground is shared with the chip ground, and that the chip is powered. Make sure the output goes to the right input pin.
  • Logic levels look wrong. Bit switch
    The switch gives a clean 0 or 1, but the chip only sees that if its supply matches. Check the supply voltage is in the chip's allowed range.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.