Try Embedded

Eight LEDs from three pins (74HC595)

A 74HC595 shift register drives eight LEDs from three pins, with a light that runs back and forth.

Logic · Beginner

In a real product: Car turn signal (Automotive): Sequential lights from a shift register.

An Uno has 14 digital pins, and a project quickly wants more outputs than that. A 74HC595 shift register takes eight bits one at a time over three wires and puts them on eight output pins at once. Chain a second one and the same three wires drive sixteen.

What you'll learn

  • How a shift register turns serial data into parallel outputs
  • Why the latch (STCP) makes all eight outputs change at the same moment
  • Using shiftOut() and building patterns with bit shifts (1 << i)

How it works

shiftOut(DATA, CLOCK, MSBFIRST, pattern) puts each bit of pattern on DS and pulses SHCP: every rising edge moves the chip's shift register along one place and takes the new bit in. The outputs Q0 to Q7 do not change while this happens. Raising STCP (the latch) copies all eight bits to the outputs together, so the LEDs never flicker through the in-between patterns.

The last bit shifted in lands on Q0, so with MSBFIRST bit 0 of pattern drives Q0 and bit 7 drives Q7. 1 << i is a byte with only bit i set, which lights one LED.

OE (output enable) is tied to GND so the outputs are always on, and MR (reset) to 5V so the register is never cleared. The bar graph's cathodes are chained to GND.

Try this

  • Light a growing bar instead of a single LED: (1 << (i + 1)) - 1.
  • Wire OE to pin 9 instead of GND and analogWrite(9, ...) it: PWM on OE dims all eight LEDs at once (a low duty is bright, since OE is active LOW).
  • Add a second 74HC595: its DS to the first chip's Q7S, its clocks shared. Send two bytes before latching.

sketch.ino

// A 74HC595 drives eight LEDs of a bar graph from three pins: a light runs back and forth.
const int DATA = 11;   // to DS
const int CLOCK = 12;  // to SHCP, the shift clock
const int LATCH = 8;   // to STCP, the latch

void show(byte pattern) {
  shiftOut(DATA, CLOCK, MSBFIRST, pattern);
  digitalWrite(LATCH, HIGH);   // the outputs change here, all at once
  digitalWrite(LATCH, LOW);
}

void setup() {
  pinMode(DATA, OUTPUT);
  pinMode(CLOCK, OUTPUT);
  pinMode(LATCH, OUTPUT);
}

void loop() {
  for (int i = 0; i < 8; i++) {
    show(1 << i);
    delay(80);
  }
  for (int i = 6; i > 0; i--) {
    show(1 << i);
    delay(80);
  }
}

Lessons behind it