Built-in project · Arduino Uno R3

Hex digits on a common-anode display (74HC595, pins 11, 12, 13), on an Arduino Uno

A 74HC595 (DS 11, ST_CP 12, SH_CP 13) drives a common-anode seven-segment display through 0 to F, a digit a second: on this display a LOW output lights a segment. 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 25 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

  • Shift registers: more outputs from fewer pins
  • Data, clock and latch, the three signals a 595 needs
  • Common-anode displays light a segment with a low output
  • Storing display patterns in a lookup table
  • Difficulty: Intermediate
  • Time: About 25 minutes
  • Board: Arduino Uno R3
  • Parts: 5

How it works

The 74HC595 shift register turns three Arduino pins into seven outputs. Pin 11 sends the data bits, pin 13 clocks each bit in, and pin 12 latches them onto the outputs Q0 to Q6, which go to segments a to g.

This is a common-anode display: all segments share a connection to 5 V through a 220 ohm resistor. A segment lights when the chip pulls its pin low, so a 0 bit means on.

The sketch steps through the digits 0 to F, one per second, and prints each one to serial.

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
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
74HC595 shift register pin MRArduino Uno R3 pin 5V
74HC595 shift register pin VCCArduino Uno R3 pin 5V
Resistor, 220 Ω pin 2Arduino 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 digit through a 74HC595: 0 to F on a common-anode display.
//
//   74HC595 DS -> pin 11   ST_CP -> pin 12   SH_CP -> pin 13
//   Q0..Q6 -> segments a..g; the display's common anode -> 220 ohm -> 5V
//
// On a common-anode display the segments share their 5 V side, so a segment
// lights when the chip pulls its other end LOW. That is why the patterns
// below look upside down next to a common-cathode table.

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};

void setup() {
  pinMode(LATCH, OUTPUT);
  pinMode(CLOCK, OUTPUT);
  pinMode(DATA, OUTPUT);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: 0 to F on a 74HC595 and a common-anode digit");
}

void loop() {
  for (int i = 0; i < 16; i++) {
    write595(DIGITS[i], MSBFIRST);
    Serial.println(i, HEX);
    delay(1000);
  }
}

Code walkthrough

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

write595 sketch.ino

Pulls LATCH low, shifts one byte out with shiftOut on DATA and CLOCK, then pulls LATCH high. The outputs change only at that last step, so the display never shows half-shifted bits.

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

setup sketch.ino

Sets the three control pins as outputs and opens serial at 9600 baud.

void setup() {
  pinMode(LATCH, OUTPUT);
  pinMode(CLOCK, OUTPUT);
  pinMode(DATA, OUTPUT);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: 0 to F on a 74HC595 and a common-anode digit");
}

loop sketch.ino

Goes through all 16 entries of DIGITS, sends each to the chip, prints its hex value, and waits one second.

void loop() {
  for (int i = 0; i < 16; i++) {
    write595(DIGITS[i], MSBFIRST);
    Serial.println(i, HEX);
    delay(1000);
  }
}

Troubleshooting

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

  • Digits look inverted or scrambled This project
    This display is common-anode, so a 0 bit lights a segment. A table made for a common-cathode display will not work.
  • The display stays blank This project
    Check OE goes to ground, MR goes to 5 V, and the common pin goes through the 220 ohm resistor to 5 V.
  • Segments light in the wrong places This project
    Q0 to Q6 must go to segments A to G in order.
  • 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.

Change it and keep it

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