Built-in project · Arduino Uno R3

Four-digit display 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 the segments of a four-digit display whose digits are all switched on, stepping through 0 to F. The whole build, an Arduino Uno and 6 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 25 minutes to build and run. Runs in your browser. Free, and no account needed.

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

Four-digit display on a 74HC595 (pins 8, 10 and 9) · 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

  • Expanding outputs with a 74HC595 shift register
  • Data, clock and latch pins
  • Seven-segment patterns stored as bytes
  • Hexadecimal digits 0 to F
  • Why four digits sharing segment wires show the same thing
  • Difficulty: Intermediate
  • Time: About 25 minutes
  • Board: Arduino Uno R3
  • Parts: 8

How it works

A 74HC595 shift register turns three Uno pins into eight outputs. Those eight outputs go to the segments of a four-digit display, a to g and the decimal point. The Uno sends a pattern one bit at a time on pin 8, clocked by pin 10, and pin 9 latches it onto the outputs.

The four digits share their segment wires. Each digit's common pin goes through its own 220 Ω resistor to ground, all the time, so all four digits show the same symbol.

The sketch steps through the sixteen hex digits, 0 to F, twice a second, and prints each one.

Parts list

8 parts, plus the jumper wires. They retail for about $34; 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 Ω (1) pin 2Arduino Uno R3 pin GND
Resistor, 220 Ω (2) pin 2Arduino Uno R3 pin GND
Resistor, 220 Ω (3) pin 2Arduino Uno R3 pin GND
Resistor, 220 Ω (4) 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
Four-digit seven segment, multiplexed pin B74HC595 shift register pin Q1
Four-digit seven segment, multiplexed pin C74HC595 shift register pin Q2
Four-digit seven segment, multiplexed pin D74HC595 shift register pin Q3
Four-digit seven segment, multiplexed pin E74HC595 shift register pin Q4
Four-digit seven segment, multiplexed pin F74HC595 shift register pin Q5
Four-digit seven segment, multiplexed pin G74HC595 shift register pin Q6
Four-digit seven segment, multiplexed pin DP74HC595 shift register pin Q7
Four-digit seven segment, multiplexed pin A74HC595 shift register pin Q0
Resistor, 220 Ω (4) pin 1Four-digit seven segment, multiplexed pin DIG4
Resistor, 220 Ω (3) pin 1Four-digit seven segment, multiplexed pin DIG3
Resistor, 220 Ω (2) pin 1Four-digit seven segment, multiplexed pin DIG2
Resistor, 220 Ω (1) pin 1Four-digit seven segment, multiplexed pin DIG1

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 four-digit display on one 74HC595: every digit shows the same symbol.
//
//   74HC595 DS -> pin 8    SHCP -> pin 10    STCP -> pin 9
//   74HC595 VCC and MR -> 5V      GND and OE -> GND
//   Q0 to Q7 -> segments a to g and the decimal point
//   each digit's common cathode (DIG1 to DIG4) -> its own 220 ohm -> GND
//
// The four digits share their segment lines: segment a of every digit is one
// wire. Each digit lights only while its common pin is grounded. Here all four
// are grounded all the time, so all four show the same symbol. To show four
// different symbols a sketch switches the digits on one after another, faster
// than the eye can follow; the stopwatch circuits do exactly that.
//
// The sketch steps through the sixteen hexadecimal digits, 0 to F, twice a
// second.

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

//                   gfedcba
const byte HEX_DIGITS[16] = {
    0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07,
    0x7F, 0x6F, 0x77, 0x7C, 0x39, 0x5E, 0x79, 0x71,
};
const char SYMBOLS[] = "0123456789ABCDEF";

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: four digits on a 74HC595 (pins 8, 10 and 9)");
}

void loop() {
  for (int i = 0; i < 16; i++) {
    show(HEX_DIGITS[i]);
    Serial.println(SYMBOLS[i]);
    delay(500);
  }
}

Code walkthrough

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

show sketch.ino

Pulls the latch pin LOW, shifts one byte out with shiftOut, most significant bit first, then pulls the latch HIGH. The outputs only change at that last step, so the display never shows a half-shifted pattern.

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

setup sketch.ino

Sets the data, latch and clock pins as outputs and prints a hello over serial at 9600.

void setup() {
  pinMode(DATA_PIN, OUTPUT);
  pinMode(LATCH_PIN, OUTPUT);
  pinMode(CLOCK_PIN, OUTPUT);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: four digits on a 74HC595 (pins 8, 10 and 9)");
}

loop sketch.ino

Walks through the 16 entries of HEX_DIGITS. For each it calls show, prints the matching character from SYMBOLS and waits 500 ms.

void loop() {
  for (int i = 0; i < 16; i++) {
    show(HEX_DIGITS[i]);
    Serial.println(SYMBOLS[i]);
    delay(500);
  }
}

Troubleshooting

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

  • A segment looks wrong or is always off This project
    Check that Q0 to Q7 go to segments A to G and DP in order, one wire each.
  • Nothing lights This project
    The shift register's OE pin must go to ground and MR to 5 V.
  • 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.
  • Digits look dim, flicker, or show ghosts of other digits. Four-digit seven segment
    Refresh each digit quickly in a loop, at least 100 Hz for the whole set. Turn all segments off before switching digits. Do not use delay() for long pauses while displaying.
  • Segments burn out or the pins are overloaded. Four-digit seven segment
    Use a resistor of about 220 ohms on each segment line. Check whether the display is common cathode or common anode, since that sets which level turns a segment on.
  • 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.