Built-in project · no microcontroller

Binary counter

Two 74HC74s divide a 4 Hz clock four times and count a four-bit number up a row of LEDs. There is no microcontroller in it: the simulator solves the 11 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.

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

The parts retail for about $6.40. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • How a D flip-flop wired to toggle divides a clock by two
  • How chaining dividers builds a binary counter
  • Reading a row of LEDs as a four-bit binary number
  • Why a ripple counter slows down at high clock speeds
  • Difficulty: Advanced
  • Time: About 30 minutes
  • Parts: 13

How it works

A clock part sends a 4 Hz square wave into the first of four flip-flops. Each flip-flop has its D input tied to its own inverted output, so every rising clock edge makes it flip to the opposite state. That cuts the frequency in half.

Four flip-flops in a row cut it in half four times: 2 Hz, 1 Hz, 0.5 Hz and 0.25 Hz. Each stage is clocked by the inverted output of the one before it. The outputs drive the red, yellow, green and blue LEDs through 220 ohm resistors.

Read the LEDs as a binary number and it counts up from 0001 to 1111, then starts over. One full cycle takes four seconds.

Parts list

13 parts, plus the jumper wires. They retail for about $6.40; on Mokxi they're free.

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
74HC74 dual flip-flop (1) pin 1RD5 V
74HC74 dual flip-flop (1) pin 1SD5 V
74HC74 dual flip-flop (1) pin 2SD5 V
74HC74 dual flip-flop (1) pin 2RD5 V
74HC74 dual flip-flop (1) pin VCC5 V
74HC74 dual flip-flop (2) pin 1RD5 V
74HC74 dual flip-flop (2) pin 1SD5 V
74HC74 dual flip-flop (2) pin 2SD5 V
74HC74 dual flip-flop (2) pin 2RD5 V
74HC74 dual flip-flop (2) pin VCC5 V
74HC74 dual flip-flop (1) pin GNDGround
74HC74 dual flip-flop (2) pin GNDGround
LED, red pin CGround
LED, yellow pin CGround
LED, green pin CGround
LED, blue pin CGround
74HC74 dual flip-flop (1) pin 1CPClock pin OUT
74HC74 dual flip-flop (1) pin 1QN74HC74 dual flip-flop (1) pin 1D
74HC74 dual flip-flop (1) pin 2CP74HC74 dual flip-flop (1) pin 1D
Resistor, 220 Ω (1) pin 174HC74 dual flip-flop (1) pin 1Q
74HC74 dual flip-flop (1) pin 2D74HC74 dual flip-flop (1) pin 2QN
74HC74 dual flip-flop (2) pin 1CP74HC74 dual flip-flop (1) pin 2QN
Resistor, 220 Ω (2) pin 174HC74 dual flip-flop (1) pin 2Q
74HC74 dual flip-flop (2) pin 1QN74HC74 dual flip-flop (2) pin 1D
74HC74 dual flip-flop (2) pin 2CP74HC74 dual flip-flop (2) pin 1D
Resistor, 220 Ω (3) pin 174HC74 dual flip-flop (2) pin 1Q
74HC74 dual flip-flop (2) pin 2D74HC74 dual flip-flop (2) pin 2QN
Resistor, 220 Ω (4) pin 174HC74 dual flip-flop (2) pin 2Q
LED, red pin AResistor, 220 Ω (1) pin 2
LED, yellow pin AResistor, 220 Ω (2) pin 2
LED, green pin AResistor, 220 Ω (3) pin 2
LED, blue pin AResistor, 220 Ω (4) pin 2

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 4-bit ripple counter: two dual flip-flop chips, all four stages used.
//
// Each stage is a 74HC74 D flip-flop wired to toggle: D is tied to its own
// Q-bar, so whatever it is holding, the next rising clock edge loads the
// opposite. That divides the clock by two.
//
//   clock 4 Hz  ->  Q0  2 Hz    (bit 0, changes every 250 ms)
//   Q0-bar     ->  Q1  1 Hz    (bit 1)
//   Q1-bar     ->  Q2  0.5 Hz  (bit 2)
//   Q2-bar     ->  Q3  0.25 Hz (bit 3)
//
// Read the four LEDs as a binary number and it steps 0001, 0010, 0011 ... 1111
// and round again, once every four seconds. Clocking each stage from Q-bar
// counts up; clocking it from Q instead counts down, and that one wire is the
// whole difference.
//
// Change the clock part's frequency in the properties panel to speed it up. At
// a few kHz you can no longer see the count, but the ripple is still there: the
// carry has to walk through every stage before the number is right, which is
// what limits how fast a counter like this can be read.

Troubleshooting

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

  • The LEDs stay on or stay off. This project
    Check that every set and reset pin (SD and RD) on both chips goes to 5 V, and that each chip has VCC and GND.
  • The count runs backward. This project
    Each stage must be clocked from the previous stage's inverted output (QN). Clocking from Q instead counts down.
  • A stage does not flip. This project
    Each D pin must connect to the same stage's QN pin.
  • The circuit does not behave as expected at the set frequency. Clock
    Pick a frequency slow enough to watch, such as 1 Hz, when learning. Fast clocks look like steady signals. Make sure the clock output and your circuit share a ground.
  • A counter or flip-flop skips steps. Clock
    Check that the clock edge matches the chip, rising or falling. Slow, clean edges are best. Do not connect a mechanical switch as a clock without debouncing it.
  • The flip-flop does not change or is stuck. 74HC74 dual flip-flop
    Preset and clear are active low and override the clock. Tie them to VCC for normal use. If one is low, the output is forced.
  • The clock divider does not divide by two. 74HC74 dual flip-flop
    Wire the inverted output (Q-bar) back to D, then take the clock input and the Q output. Each rising edge flips the output.
  • The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
    Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure.

Change it and keep it

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