Built-in project · Arduino Uno R3

One-digit countdown on a 74HC595 (pins 8, 10 and 9), on an Arduino Uno

A 74HC595 with data on pin 8, clock on 10 and latch on 9 drives a common-cathode seven-segment digit, counting down from D to 0 in hexadecimal. 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.

The circuit runs here once the page has loaded.

What you'll learn

  • Expanding three pins into eight outputs with a shift register
  • Why a latch pin makes all outputs change at once
  • Storing a symbol as one byte, one bit per segment
  • Common cathode display wiring
  • Difficulty: Intermediate
  • Time: About 20 minutes
  • Board: Arduino Uno R3
  • Parts: 5

How it works

A 74HC595 shift register takes three Uno pins (data on 8, latch on 9, clock on 10) and gives back eight outputs. Outputs Q0 to Q6 go to segments a to g of a seven-segment display. One 220 ohm resistor joins the display's common pin to ground.

Each symbol is one byte, with a bit for each segment. The sketch sends a byte into the chip, then pulses the latch so all the segments change together. It counts down once a second from D to A, then 9 to 0, rests five seconds on 0, and starts again.

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 10
74HC595 shift register pin STCPArduino Uno R3 pin 9
74HC595 shift register pin DSArduino Uno R3 pin 8
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
// One seven-segment digit through a 74HC595, counting down from D to 0.
//
//   74HC595 DS (pin 14)    -> Uno pin 8    (data)
//   74HC595 STCP (pin 12)  -> Uno pin 9    (latch)
//   74HC595 SHCP (pin 11)  -> Uno pin 10   (clock)
//   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 -> GND
//
// A seven-segment display is eight LEDs in one package: segments a to g and
// the dot. Each symbol is a pattern of lit segments, one bit per segment, so
// a whole symbol fits in one byte and goes out with a single shiftOut().
//
// The count runs in hexadecimal, D, C, B, A, then 9 down to 0, one a second,
// and rests for five seconds at 0 before it starts again.

const int DATA_PIN = 8;
const int LATCH_PIN = 9;
const int CLOCK_PIN = 10;

//                     gfedcba, segment a in bit 0
const byte SYMBOLS[14] = {
    0b00111111,  // 0
    0b00000110,  // 1
    0b01011011,  // 2
    0b01001111,  // 3
    0b01100110,  // 4
    0b01101101,  // 5
    0b01111101,  // 6
    0b00000111,  // 7
    0b01111111,  // 8
    0b01101111,  // 9
    0b01110111,  // A
    0b01111100,  // b
    0b00111001,  // C
    0b01011110,  // d
};
const char NAMES[] = "0123456789ABCD";

void show(byte pattern) {
  digitalWrite(LATCH_PIN, LOW);
  shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, pattern);  // bit 0 lands on Q0
  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 (data 8, latch 9, clock 10)");
}

void loop() {
  for (int i = 13; i >= 0; i--) {
    show(SYMBOLS[i]);
    Serial.println(NAMES[i]);
    delay(1000);
  }
  delay(5000);
}

Code walkthrough

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

show sketch.ino

Drops the latch pin LOW, sends the pattern byte with shiftOut, then raises the latch HIGH. While the latch is low, the bits slide in without changing the display. The rising edge copies them to the outputs. Bit 0 ends up on Q0, which is segment a.

void show(byte pattern) {
  digitalWrite(LATCH_PIN, LOW);
  shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, pattern);  // bit 0 lands on Q0
  digitalWrite(LATCH_PIN, HIGH);
}

setup sketch.ino

Makes the data, latch and clock pins outputs and opens serial at 9600 with a short hello 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 (data 8, latch 9, clock 10)");
}

loop sketch.ino

Counts i down from 13 to 0, showing SYMBOLS[i] and printing its name, with a 1000 ms delay each time. The table holds 0 to 9 followed by A, b, C and d, so the count reads D, C, B, A, 9 and so on to 0. After 0 it waits 5000 ms.

void loop() {
  for (int i = 13; i >= 0; i--) {
    show(SYMBOLS[i]);
    Serial.println(NAMES[i]);
    delay(1000);
  }
  delay(5000);
}

Troubleshooting

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

  • Segments light in the wrong shape, like a scrambled digit. This project
    Check Q0 to Q6 go to segments a to g in order: Q0 to A, Q1 to B, and so on up to Q6 to G.
  • The display stays dark. This project
    The chip's OE pin must go to GND and MR to 5V. Also check the common pin has its 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.