Built-in project · Arduino Uno R3

Four digits from one 74HC595 (pins 11, 12, 13 and 7 to 4), on an Arduino Uno

A four-digit common-anode display: a 74HC595 (DS 11, ST_CP 12, SH_CP 13) sets the segments and pins 7, 6, 5 and 4 pick the digit, fast enough that all four look lit. It counts up in hexadecimal. The whole build, an Arduino Uno and 10 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.

Advanced About 40 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.

The circuit runs here once the page has loaded.

What you'll learn

  • Multiplexing: sharing segment lines between digits
  • Shifting a byte into a 74HC595 with shiftOut
  • Common-anode displays, where a 0 bit lights a segment
  • Splitting a number into hex digits with shifts and masks
  • Difficulty: Advanced
  • Time: About 40 minutes
  • Board: Arduino Uno R3
  • Parts: 12

How it works

A four-digit display shares its eight segment lines across all digits, so only one digit can show at a time. A 74HC595 shift register drives the segments through eight 220 ohm resistors, using pins 11, 12 and 13. Pins 7, 6, 5 and 4 pick which digit is on.

The sketch cycles through the four digits, spending 4 ms on each. For each one it blanks the segments, selects the digit, and sends its pattern. Done fast, all four look lit together. The number shown is the time in tenths of a second, counting up in hexadecimal.

Parts list

12 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
74HC595 shift register pin SHCPArduino Uno R3 pin 13
74HC595 shift register pin STCPArduino Uno R3 pin 12
74HC595 shift register pin DSArduino Uno R3 pin 11
Four-digit seven segment, multiplexed pin DIG1Arduino Uno R3 pin 7
Four-digit seven segment, multiplexed pin DIG2Arduino Uno R3 pin 6
Four-digit seven segment, multiplexed pin DIG3Arduino Uno R3 pin 5
Four-digit seven segment, multiplexed pin DIG4Arduino Uno R3 pin 4
74HC595 shift register pin MRArduino Uno R3 pin 5V
74HC595 shift register pin VCCArduino Uno R3 pin 5V
Resistor, 220 Ω (2) pin 174HC595 shift register pin Q1
Resistor, 220 Ω (3) pin 174HC595 shift register pin Q2
Resistor, 220 Ω (4) pin 174HC595 shift register pin Q3
Resistor, 220 Ω (5) pin 174HC595 shift register pin Q4
Resistor, 220 Ω (6) pin 174HC595 shift register pin Q5
Resistor, 220 Ω (7) pin 174HC595 shift register pin Q6
Resistor, 220 Ω (8) pin 174HC595 shift register pin Q7
Resistor, 220 Ω (1) pin 174HC595 shift register pin Q0
Resistor, 220 Ω (5) pin 2Four-digit seven segment, multiplexed pin E
Resistor, 220 Ω (4) pin 2Four-digit seven segment, multiplexed pin D
Resistor, 220 Ω (8) pin 2Four-digit seven segment, multiplexed pin DP
Resistor, 220 Ω (3) pin 2Four-digit seven segment, multiplexed pin C
Resistor, 220 Ω (7) pin 2Four-digit seven segment, multiplexed pin G
Resistor, 220 Ω (2) pin 2Four-digit seven segment, multiplexed pin B
Resistor, 220 Ω (6) pin 2Four-digit seven segment, multiplexed pin F
Resistor, 220 Ω (1) pin 2Four-digit seven segment, multiplexed pin A

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
// Four digits from one shift register: multiplexing.
//
//   74HC595 DS -> pin 11   ST_CP -> pin 12   SH_CP -> pin 13
//   Q0..Q7 -> 220 ohm each -> segments a..g and dp
//   digit 1..4 common anodes -> pins 7, 6, 5 and 4
//
// The segments are shared by all four digits, so the sketch shows one digit
// at a time: blank the segments, choose a digit by raising its anode, send
// its pattern, wait a few milliseconds, and move to the next. Round and round
// fast enough, all four look lit at once.

const int LATCH = 12;  // ST_CP
const int CLOCK = 13;  // SH_CP
const int DATA = 11;   // DS

void write595(byte value, int order) {
  digitalWrite(LATCH, LOW);
  shiftOut(DATA, CLOCK, order, value);
  digitalWrite(LATCH, HIGH);
}

// The digits 0 to F for a common-anode display: a 0 bit lights a segment.
// Bit 0 is segment a, bit 6 is g and bit 7 is the decimal point.
const byte DIGITS[16] = {0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8,
                         0x80, 0x90, 0x88, 0x83, 0xC6, 0xA1, 0x86, 0x8E};

const int DIGIT_PINS[4] = {7, 6, 5, 4};

void choose(int digit) {
  for (int i = 0; i < 4; i++) digitalWrite(DIGIT_PINS[i], i == digit ? HIGH : LOW);
}

void setup() {
  pinMode(LATCH, OUTPUT);
  pinMode(CLOCK, OUTPUT);
  pinMode(DATA, OUTPUT);
  for (int i = 0; i < 4; i++) pinMode(DIGIT_PINS[i], OUTPUT);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: four digits counting up in hex");
}

void loop() {
  unsigned int value = millis() / 100;
  for (int i = 0; i < 4; i++) {
    int nibble = (value >> (4 * (3 - i))) & 0x0F;
    write595(0xFF, LSBFIRST);
    choose(i);
    write595(DIGITS[nibble], LSBFIRST);
    delay(4);
  }
}

Code walkthrough

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

write595 sketch.ino

Pulls the latch pin low, shifts one byte out with shiftOut, and raises the latch to show it. The outputs of the 595 only change on that rising edge, so the segments never flicker mid-shift.

void write595(byte value, int order) {
  digitalWrite(LATCH, LOW);
  shiftOut(DATA, CLOCK, order, value);
  digitalWrite(LATCH, HIGH);
}

choose sketch.ino

Sets one of the pins in DIGIT_PINS high and the other three low. Each pin feeds a digit's common anode, so only the chosen digit can light.

void choose(int digit) {
  for (int i = 0; i < 4; i++) digitalWrite(DIGIT_PINS[i], i == digit ? HIGH : LOW);
}

loop sketch.ino

Takes the time in tenths of a second as value. For each of the four positions, it picks that digit's 4-bit nibble, blanks the segments with 0xFF, chooses the digit, sends its pattern from DIGITS and waits 4 ms. Blanking first stops ghosting.

void loop() {
  unsigned int value = millis() / 100;
  for (int i = 0; i < 4; i++) {
    int nibble = (value >> (4 * (3 - i))) & 0x0F;
    write595(0xFF, LSBFIRST);
    choose(i);
    write595(DIGITS[nibble], LSBFIRST);
    delay(4);
  }
}

Troubleshooting

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

  • Digits show the wrong number or order This project
    Check that DIG1 to DIG4 go to pins 7, 6, 5 and 4 in that order.
  • Segments look scrambled This project
    Check that Q0 to Q7 go through resistors to segments A to G and DP in order.
  • Faint ghost segments appear on neighbors This project
    Keep the blanking step before choose in the loop.
  • 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.

Change it and keep it

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