Built-in project · Arduino Uno R3

A light runs along eight LEDs on a 74HC595 (pins 11, 12 and 13), on an Arduino Uno

A 74HC595 with DS on pin 11, ST_CP on 12 and SH_CP on 13 lights one of eight LEDs at a time and walks the light along them, one shifted bit at a time. 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
  • Moving a single bit with the shift operators
  • What bit order means: MSBFIRST and LSBFIRST
  • Difficulty: Intermediate
  • Time: About 25 minutes
  • Board: Arduino Uno R3
  • Parts: 19

How it works

A 74HC595 shift register gives three Arduino pins control over eight LEDs. Pin 11 sends the data (DS), pin 13 clocks each bit in (SHCP) and pin 12 latches the finished byte onto the outputs (STCP).

The sketch starts with the byte 0x01, which has one bit set, so one LED is lit. Each 100 ms it shifts that bit one place left, and the light moves from Q0 to Q7.

Then it sets the byte to 0x80 and shifts right, so the light runs back. The loop repeats, and the light bounces from end to end.

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 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 Ω (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 running light on a 74HC595: three pins drive eight LEDs.
//
//   74HC595 DS -> pin 11   ST_CP -> pin 12   SH_CP -> pin 13
//   VCC and MR -> 5V   OE and GND -> GND   Q0..Q7 -> 220 ohm -> LEDs -> GND
//
// The byte 0x01 has one bit set. Shifting it left one place each step moves
// the lit LED along: 00000001, 00000010, ... 10000000.

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

void setup() {
  pinMode(LATCH, OUTPUT);
  pinMode(CLOCK, OUTPUT);
  pinMode(DATA, OUTPUT);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: a running light on a 74HC595 (11, 12 and 13)");
}

void loop() {
  byte x = 0x01;
  for (int j = 0; j < 8; j++) {
    write595(x, LSBFIRST);
    x <<= 1;
    delay(100);
  }
  x = 0x80;
  for (int j = 0; j < 8; j++) {
    write595(x, LSBFIRST);
    x >>= 1;
    delay(100);
  }
}

Code walkthrough

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

write595 sketch.ino

Sends one byte. It sets LATCH LOW, calls shiftOut with the bit order you pass in, and sets LATCH HIGH. The rising edge puts all eight bits on the outputs at the same moment.

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

setup sketch.ino

Sets LATCH, CLOCK and DATA as outputs and starts the serial port at 9600. Nothing else is needed: OE is tied to ground and MR to 5 V on the breadboard.

void setup() {
  pinMode(LATCH, OUTPUT);
  pinMode(CLOCK, OUTPUT);
  pinMode(DATA, OUTPUT);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: a running light on a 74HC595 (11, 12 and 13)");
}

loop sketch.ino

Starts with x at 0x01 and runs eight steps: write it, shift left with x <<= 1, wait 100 ms. Then it starts at 0x80 and shifts right with x >>= 1 to run the light back.

void loop() {
  byte x = 0x01;
  for (int j = 0; j < 8; j++) {
    write595(x, LSBFIRST);
    x <<= 1;
    delay(100);
  }
  x = 0x80;
  for (int j = 0; j < 8; j++) {
    write595(x, LSBFIRST);
    x >>= 1;
    delay(100);
  }
}

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 light does not move in order. This project
    Pin 13 goes to SHCP and pin 12 to STCP. Swapping them scrambles the pattern.
  • The light runs the wrong way. This project
    The bit order is LSBFIRST here. Check that Q0 is the first LED in your row.
  • 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.