Built-in project · Arduino Uno R3

Countdown digit on an Arduino Uno

A 74HC595 on three pins drives a one-digit seven-segment display, counting down from 9 to 0 one number a second. 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

  • Shift registers: many outputs from three pins
  • Data, clock and latch
  • Encoding a digit as a byte of segment bits
  • Common-cathode displays
  • Difficulty: Intermediate
  • Time: About 20 minutes
  • Board: Arduino Uno R3
  • Parts: 5

How it works

Driving a seven-segment digit directly takes seven pins. A 74HC595 shift register cuts that to three. The Uno sends one byte on DS (pin 4), clocked in by SHCP (pin 5). Then a pulse on STCP (pin 6) latches the byte so all eight outputs change at once.

Outputs Q0 to Q6 go to segments A to G of a common-cathode display. The common pin returns to ground through one 220 ohm resistor.

The sketch counts down from 9 to 0, one number a second, then starts again. Each number is a byte from a table, one bit per segment.

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 STCPArduino Uno R3 pin 6
74HC595 shift register pin SHCPArduino Uno R3 pin 5
74HC595 shift register pin DSArduino Uno R3 pin 4
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
// Countdown digit: a 74HC595 drives a one-digit seven-segment display.
//
//   74HC595   DS -> pin 4, SHCP (clock) -> pin 5, STCP (latch) -> pin 6
//             Q0 to Q6 -> segments A to G, each through its own 220 ohm
//   display   common cathode, COM -> GND
//
// Eight pins for one digit is a lot of an Uno. Through the 74HC595 it is three
// pins, however many segments there are: the sketch sends one byte, a bit per
// segment, and the latch shows all eight at once.
//
// The table holds one byte per digit, bit 0 for segment A up to bit 6 for G.
// The display counts down from 9 to 0, one number a second, and starts again.

const int DATA = 4;
const int CLOCK = 5;
const int LATCH = 6;

//                    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(int digit) {
  digitalWrite(LATCH, LOW);
  shiftOut(DATA, CLOCK, MSBFIRST, DIGITS[digit]);
  digitalWrite(LATCH, HIGH);
}

void setup() {
  pinMode(DATA, OUTPUT);
  pinMode(CLOCK, OUTPUT);
  pinMode(LATCH, OUTPUT);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: countdown digit");
}

void loop() {
  for (int digit = 9; digit >= 0; digit--) {
    show(digit);
    Serial.print("showing ");
    Serial.println(digit);
    delay(1000);
  }
}

Code walkthrough

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

show sketch.ino

Pulls LATCH low, uses shiftOut to send the byte for the digit from DIGITS most significant bit first, then pulls LATCH high. The register shifts bits in while the latch is low, and shows them all at once when the latch goes high.

void show(int digit) {
  digitalWrite(LATCH, LOW);
  shiftOut(DATA, CLOCK, MSBFIRST, DIGITS[digit]);
  digitalWrite(LATCH, HIGH);
}

setup sketch.ino

Sets the data, clock and latch pins as outputs and starts the serial port at 9600 baud with a short message.

void setup() {
  pinMode(DATA, OUTPUT);
  pinMode(CLOCK, OUTPUT);
  pinMode(LATCH, OUTPUT);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: countdown digit");
}

loop sketch.ino

Counts digit from 9 down to 0. For each, it calls show, prints "showing" and the digit, and waits one second. Then the loop starts over at 9.

void loop() {
  for (int digit = 9; digit >= 0; digit--) {
    show(digit);
    Serial.print("showing ");
    Serial.println(digit);
    delay(1000);
  }
}

Troubleshooting

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

  • The digit shows odd shapes This project
    Check that Q0 to Q6 go to segments A to G in order. The bits in DIGITS assume bit 0 is segment A.
  • Nothing lights This project
    OE must go to ground and MR to 5V, and the display's common pin must reach GND through the resistor.
  • 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.