Built-in project · Arduino Uno R3

One-digit countdown on a 74HC595 (pins 2, 4 and 3), on an Arduino Uno

A 74HC595 with data on pin 2, clock on 4 and latch on 3 drives a common-cathode seven-segment digit, counting down from 9 to 0. The whole build, an Arduino Uno and 3 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

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

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

One-digit countdown on a 74HC595 (pins 2, 4 and 3) · Arduino Uno R3live0.000 s 0.00x
Click to open it in the editor
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

What you'll learn

  • Wiring a shift register with data, clock and latch pins
  • A seven-segment digit as eight LEDs in one package
  • Bit patterns for digits
  • Choosing pins and naming them as constants
  • Difficulty: Intermediate
  • Time: About 20 minutes
  • Board: Arduino Uno R3
  • Parts: 5

How it works

A 74HC595 shift register lets three Uno pins drive a whole seven-segment digit. Data goes on pin 2 (DS), the clock on pin 4 (SHCP) and the latch on pin 3 (STCP). The chip's outputs Q0 to Q6 feed segments A to G, and the display's common cathode goes to ground through a 220 ohm resistor.

A digit is one byte. Each bit stands for a segment, with segment A in the lowest bit. The sketch sends the byte with shiftOut, then raises the latch so the segments change together.

It counts down from 9 to 0, one number a second, waits an extra second and repeats.

Parts list

5 parts, plus the jumper wires. They retail for about $33; 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
74HC595 shift register pin GNDArduino Uno R3 pin GND
74HC595 shift register pin OEArduino Uno R3 pin GND
Resistor, 220 Ω pin 2Arduino Uno R3 pin GND
74HC595 shift register pin SHCPArduino Uno R3 pin 4
74HC595 shift register pin STCPArduino Uno R3 pin 3
74HC595 shift register pin DSArduino Uno R3 pin 2
74HC595 shift register pin MRArduino Uno R3 pin 5V
74HC595 shift register pin VCCArduino Uno R3 pin 5V
Seven segment display pin B74HC595 shift register pin Q1
Seven segment display pin C74HC595 shift register pin Q2
Seven segment display pin D74HC595 shift register pin Q3
Seven segment display pin E74HC595 shift register pin Q4
Seven segment display pin F74HC595 shift register pin Q5
Seven segment display pin G74HC595 shift register pin Q6
Seven segment display pin A74HC595 shift register pin Q0
Resistor, 220 Ω pin 1Seven segment display pin COM1

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// A seven-segment digit counting down from 9, through a 74HC595.
//
//   74HC595 DS -> pin 2    SHCP -> pin 4    STCP -> pin 3
//   74HC595 VCC and MR -> 5V      GND and OE -> GND
//   Q0 to Q6 -> segments a to g, Q7 -> the decimal point
//   the display's common cathode -> 220 ohm -> GND
//
// A seven-segment display is eight LEDs in one package: segments a to g and
// the dot. Each digit is a pattern of lit segments, one bit per segment, so
// the whole digit fits in one byte and goes out through the shift register
// with a single shiftOut().
//
// The patterns below are written with segment a in the lowest bit.

const int DATA_PIN = 2;
const int LATCH_PIN = 3;
const int CLOCK_PIN = 4;

//                   gfedcba
const byte DIGITS[10] = {
    0b00111111,  // 0
    0b00000110,  // 1
    0b01011011,  // 2
    0b01001111,  // 3
    0b01100110,  // 4
    0b01101101,  // 5
    0b01111101,  // 6
    0b00000111,  // 7
    0b01111111,  // 8
    0b01101111,  // 9
};

void show(byte pattern) {
  digitalWrite(LATCH_PIN, LOW);
  shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, pattern);
  digitalWrite(LATCH_PIN, HIGH);
}

void setup() {
  pinMode(DATA_PIN, OUTPUT);
  pinMode(LATCH_PIN, OUTPUT);
  pinMode(CLOCK_PIN, OUTPUT);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: one digit on a 74HC595 (pins 2, 4 and 3)");
}

void loop() {
  for (int n = 9; n >= 0; n--) {
    show(DIGITS[n]);
    Serial.println(n);
    delay(1000);
  }
  delay(1000);
}

Code walkthrough

The key functions of the sketch, and what each one does.

show sketch.ino

Takes a pattern byte. It lowers LATCH_PIN, sends the byte out with shiftOut on DATA_PIN and CLOCK_PIN, most significant bit first, and raises LATCH_PIN. The raised latch is what makes the display change.

void show(byte pattern) {
  digitalWrite(LATCH_PIN, LOW);
  shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, pattern);
  digitalWrite(LATCH_PIN, HIGH);
}

setup sketch.ino

Sets the three control pins as outputs, starts serial at 9600 baud and prints a startup message.

void setup() {
  pinMode(DATA_PIN, OUTPUT);
  pinMode(LATCH_PIN, OUTPUT);
  pinMode(CLOCK_PIN, OUTPUT);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: one digit on a 74HC595 (pins 2, 4 and 3)");
}

loop sketch.ino

Counts n from 9 down to 0. It shows DIGITS[n], prints the number and waits one second. After 0 it waits one more second and the countdown starts again.

void loop() {
  for (int n = 9; n >= 0; n--) {
    show(DIGITS[n]);
    Serial.println(n);
    delay(1000);
  }
  delay(1000);
}

Troubleshooting

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

  • The count runs but the digits look scrambled This project
    Pins 3 and 4 are easy to swap. Latch is pin 3 and clock is pin 4 here, so check LATCH_PIN and CLOCK_PIN.
  • The display stays dark This project
    Tie OE to GND and MR to 5V, and run the common pin through the 220 ohm resistor to ground.
  • Outputs show garbage, or the register never clears. 74HC595 shift register
    Tie OE low and SRCLR (pin 10) high. Left floating, these pins make the chip behave randomly. Then pulse the latch pin after shifting the byte.
  • The bits come out in the wrong order, or LEDs are dim. 74HC595 shift register
    Choose MSBFIRST or LSBFIRST in shiftOut to flip the order. Give each LED its own resistor, since each output can source only about 6 mA safely.
  • Segments stay dark, or light when they should be off. Seven segment display
    Check whether the display is common cathode or common anode. Common cathode: common pin to GND, drive segments HIGH. Common anode: common pin to VCC, drive segments LOW.
  • One segment is very bright or the LEDs burn out. Seven segment display
    Each segment is an LED, so it needs its own resistor, about 220 ohm to 330 ohm on 5 V. One resistor on the common pin makes brightness change with the number of lit segments.
  • 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.
  • A pull-up or pull-down resistor is missing, so the input floats and reads random values. Resistor
    Tie the input to 5 V or ground through about 10k. Or use INPUT_PULLUP and wire the switch to ground.

Change it and keep it

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