Built-in project · Arduino Uno R3

Flowing light on a 74HC595 (pins 11, 8 and 12), on an Arduino Uno

Eight LEDs on a 74HC595 with DS on pin 11, SH_CP on 8 and ST_CP on 12 fill up one at a time and empty again, one byte shifted out at each step. The whole build, an Arduino Uno and 17 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.

The circuit runs here once the page has loaded.

What you'll learn

  • How a shift register adds outputs to a board
  • Data, clock and latch: the three wires of a 74HC595
  • Storing a pattern as a bytes table
  • Reading a byte as eight on or off LEDs
  • Difficulty: Intermediate
  • Time: About 25 minutes
  • Board: Arduino Uno R3
  • Parts: 19

How it works

A 74HC595 shift register turns three Arduino pins into eight outputs. The data goes in on pin 11 (DS), a clock pulse on pin 8 (SHCP) moves each bit along, and a pulse on pin 12 (STCP) latches all eight bits onto the outputs at once.

Each output Q0 to Q7 drives one red LED through a 220 ohm resistor. The sketch has nine bytes, each with one more 1 bit than the last, so the bar of light grows from Q0 to Q7.

A new byte goes out every 300 ms. After all eight LEDs are lit, the loop starts over with every LED off.

Parts list

19 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
LED, red (1) pin CArduino Uno R3 pin GND
LED, red (2) pin CArduino Uno R3 pin GND
LED, red (3) pin CArduino Uno R3 pin GND
LED, red (4) pin CArduino Uno R3 pin GND
LED, red (5) pin CArduino Uno R3 pin GND
LED, red (6) pin CArduino Uno R3 pin GND
LED, red (7) pin CArduino Uno R3 pin GND
LED, red (8) pin CArduino Uno R3 pin GND
74HC595 shift register pin STCPArduino Uno R3 pin 12
74HC595 shift register pin DSArduino Uno R3 pin 11
74HC595 shift register pin SHCPArduino Uno R3 pin 8
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
LED, red (1) pin AResistor, 220 Ω (1) pin 2
LED, red (2) pin AResistor, 220 Ω (2) pin 2
LED, red (3) pin AResistor, 220 Ω (3) pin 2
LED, red (4) pin AResistor, 220 Ω (4) pin 2
LED, red (5) pin AResistor, 220 Ω (5) pin 2
LED, red (6) pin AResistor, 220 Ω (6) pin 2
LED, red (7) pin AResistor, 220 Ω (7) pin 2
LED, red (8) pin AResistor, 220 Ω (8) pin 2

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 flowing light: eight LEDs on a 74HC595 fill up one by one.
//
//   74HC595 DS -> pin 11   SH_CP -> pin 8   ST_CP -> pin 12
//   VCC and MR -> 5V   OE and GND -> GND
//   Q0..Q7 -> 220 ohm -> an LED each -> GND
//
// shiftOut() clocks a byte into the chip one bit at a time; the latch pulse
// on ST_CP then puts all eight bits on the outputs at once. Each byte in
// STEPS has one more 1 than the last, so the bar grows from Q0 to Q7.

const int ST_CP = 12;
const int SH_CP = 8;
const int DS = 11;

const byte STEPS[9] = {
  0b00000000, 0b00000001, 0b00000011, 0b00000111, 0b00001111,
  0b00011111, 0b00111111, 0b01111111, 0b11111111,
};

void write595(byte value) {
  digitalWrite(ST_CP, LOW);
  shiftOut(DS, SH_CP, MSBFIRST, value);
  digitalWrite(ST_CP, HIGH);
}

void setup() {
  pinMode(ST_CP, OUTPUT);
  pinMode(SH_CP, OUTPUT);
  pinMode(DS, OUTPUT);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: flowing light on a 74HC595 (DS 11, SH_CP 8, ST_CP 12)");
}

void loop() {
  for (int i = 0; i <= 8; i++) {
    write595(STEPS[i]);
    delay(300);
  }
}

Code walkthrough

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

write595 sketch.ino

Sends one byte to the chip. It drops ST_CP LOW, calls shiftOut to clock in the eight bits on DS and SH_CP, then raises ST_CP HIGH. The rising edge copies the bits to the outputs, so the LEDs change together.

void write595(byte value) {
  digitalWrite(ST_CP, LOW);
  shiftOut(DS, SH_CP, MSBFIRST, value);
  digitalWrite(ST_CP, HIGH);
}

setup sketch.ino

Sets the latch, clock and data pins as outputs and starts the serial port at 9600. The chip needs no other setup because its OE pin is tied to ground and MR is tied to 5 V.

void setup() {
  pinMode(ST_CP, OUTPUT);
  pinMode(SH_CP, OUTPUT);
  pinMode(DS, OUTPUT);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: flowing light on a 74HC595 (DS 11, SH_CP 8, ST_CP 12)");
}

loop sketch.ino

Walks through the STEPS table from 0 to 8, sending each value with write595 and waiting 300 ms. Step 0 is all off. Each later step lights one more LED.

void loop() {
  for (int i = 0; i <= 8; i++) {
    write595(STEPS[i]);
    delay(300);
  }
}

Troubleshooting

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

  • No LEDs light. This project
    Check that OE is on GND, MR is on 5V and the chip has VCC and GND.
  • The LEDs light in a strange order. This project
    Check that DS is on pin 11, SHCP on pin 8 and STCP on pin 12, and that Q0 to Q7 go to the LEDs in order.
  • One LED is always dark. This project
    Check that LED's resistor and its direction: the longer leg, the anode, faces 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.
  • 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.
  • The LED does not light, or it lights once and then dies. LED
    LEDs have polarity. The longer leg (anode) goes toward the positive side. Always add a series resistor, about 220 ohms to 1 k ohm on a 5 V pin.

Change it and keep it

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