Projects / Binary counter on a shift register

Binary counter on a shift register

Beginner1 hGuided buildReviewed

Build it step by step

0 of 8 done
  1. NANO

    Wire the power

    VCC to 5V, GND to GND.

  2. NANO

    Wire the control pins

    DS to D11, SH_CP to D12, ST_CP to D8.

  3. NANO

    Wire the button

    D2 to GND.

  4. NANO

    Upload the sketch

    The LEDs count in binary.

  5. NANO

    Press briefly

    The count reverses.

  6. NANO

    Hold for a second

    The count pauses.

  7. NANO

    Press while paused

    One step at a time.

  8. NANO

    Compare with the monitor

    The binary digits match the LEDs.

In short

How do you build a binary counter on a shift register?

A shift register module shows a running binary count on eight LEDs from three Nano pins. You need Arduino Nano, 74HC595 shift register module (8 outputs), Tactile push button 6 mm and Breadboard 830 Tie Points. It takes 8 steps and about 60 minutes.

Level
Beginner
Time
About 60 min
Parts
4

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
Module VCCNano 5VPower
Module GNDNano GNDCommon ground
Module DS (data)Nano D11Serial data
Module SH_CP (shift clock)Nano D12Moves each bit along
Module ST_CP (latch)Nano D8Copies the bits to the outputs
Module OEGND if markedOutput enable, active low
ButtonNano D2 to GNDReverse, pause or step

Code

A 74HC595 shift register turns three output pins into eight. The sketch sends eight bits one at a time on the data pin, each with a pulse on the clock pin, and then a pulse on the latch pin copies all eight bits to the outputs at once. That is why the LEDs never flicker through half-loaded patterns. The count is held in a single byte, which counts from 0 to 255 and then wraps around to zero without any extra code, so the LEDs show the binary number: the lowest LED toggles every step, the next one every two steps and so on. The button has three jobs. A short press while counting reverses the direction, a long press of about three quarters of a second pauses or resumes the count, and when it is paused a short press moves one step. The serial monitor prints the same value as binary digits so you can check the LEDs. Chain a second module to make a sixteen-bit counter, which needs only a second byte to be sent before the latch.

Runs on a virtual Arduino Nano in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).

const int DATA = 11, CLOCK = 12, LATCH = 8, BTN = 2;
byte count = 0;
int dir = 1;                      // 1 counts up, -1 counts down
bool autoRun = true;
unsigned long lastStep = 0, pressedAt = 0;
bool wasDown = false;

void show(byte value) {
  digitalWrite(LATCH, LOW);
  shiftOut(DATA, CLOCK, MSBFIRST, value);
  digitalWrite(LATCH, HIGH);
  Serial.println(value, BIN);
}

void setup() {
  pinMode(DATA, OUTPUT); pinMode(CLOCK, OUTPUT); pinMode(LATCH, OUTPUT);
  pinMode(BTN, INPUT_PULLUP);
  Serial.begin(9600);
  show(count);
}

void loop() {
  bool down = (digitalRead(BTN) == LOW);
  if (down && !wasDown) pressedAt = millis();
  if (!down && wasDown) {
    if (millis() - pressedAt > 700) autoRun = !autoRun;       // long press: pause or run
    else if (autoRun) dir = -dir;                              // short press while running: reverse
    else { count += dir; show(count); }                        // short press while paused: one step
    delay(30);
  }
  wasDown = down;

  if (autoRun && millis() - lastStep >= 250) {
    lastStep = millis();
    count += dir;                                              // byte wraps from 255 to 0 by itself
    show(count);
  }
}

What the serial monitor should show

Open the serial monitor at the baud rate in the code. This is what our simulator prints for a virtual Arduino Nano with its sensors at their default values. Your numbers will follow your real readings, but the format should match.

0
1
10
11
100
101

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
Power onCount starts at 0 and goes up
Reaches 255Wraps to 0
Short pressDirection reverses
Long pressCount pauses
Short press while pausedMoves one step

If it does not work

No LEDs light

Check the OE pin and power.

Wrong order

Swap MSBFIRST for LSBFIRST.

Random pattern

Check the clock and latch wires are not swapped.

Count skips

Button bounce, keep the delay.

Module warm

Check for a short on the outputs.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the binary counter on a shift register?

Arduino Nano, 74HC595 shift register module (8 outputs), Tactile push button 6 mm and Breadboard 830 Tie Points.

How long does the binary counter on a shift register take to build?

About 60 minutes for a beginner maker, in 8 steps.

How do I know the binary counter on a shift register works?

Power on: Count starts at 0 and goes up. Reaches 255: Wraps to 0. Short press: Direction reverses.

No LEDs light: what should I check?

Check the OE pin and power.

Wrong order: what should I check?

Swap MSBFIRST for LSBFIRST.

Random pattern: what should I check?

Check the clock and latch wires are not swapped.

Count skips: what should I check?

Button bounce, keep the delay.