Built-in project · Arduino Uno R3

Eight LEDs on a 74HC595 (pins 2, 5 and 4), on an Arduino Uno

A 74HC595 drives eight LEDs from three Uno pins, data on 2, clock on 5 and latch on 4: one light runs along the row, a quarter second per LED. 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 20 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

  • Shift registers: more outputs from fewer pins
  • Data, clock and latch roles
  • One bit in a byte as one LED
  • Wrapping a counter with the % operator
  • Difficulty: Intermediate
  • Time: About 20 minutes
  • Board: Arduino Uno R3
  • Parts: 19

How it works

A 74HC595 shift register turns three Uno pins into eight outputs: data on pin 2, latch on pin 4 and clock on pin 5. Q0 to Q7 each light one yellow LED through a 220 ohm resistor.

The sketch sends a byte with exactly one bit set. Every 250 ms that bit moves up one place, so a single light runs from Q0 to Q7 and then starts over. The latch makes each move appear all at once, with no flicker.

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, yellow (1) pin CArduino Uno R3 pin GND
LED, yellow (2) pin CArduino Uno R3 pin GND
LED, yellow (3) pin CArduino Uno R3 pin GND
LED, yellow (4) pin CArduino Uno R3 pin GND
LED, yellow (5) pin CArduino Uno R3 pin GND
LED, yellow (6) pin CArduino Uno R3 pin GND
LED, yellow (7) pin CArduino Uno R3 pin GND
LED, yellow (8) pin CArduino Uno R3 pin GND
74HC595 shift register pin SHCPArduino Uno R3 pin 5
74HC595 shift register pin STCPArduino Uno R3 pin 4
74HC595 shift register pin DSArduino Uno R3 pin 2
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, yellow (1) pin AResistor, 220 Ω (1) pin 2
LED, yellow (2) pin AResistor, 220 Ω (2) pin 2
LED, yellow (3) pin AResistor, 220 Ω (3) pin 2
LED, yellow (4) pin AResistor, 220 Ω (4) pin 2
LED, yellow (5) pin AResistor, 220 Ω (5) pin 2
LED, yellow (6) pin AResistor, 220 Ω (6) pin 2
LED, yellow (7) pin AResistor, 220 Ω (7) pin 2
LED, yellow (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
// Eight LEDs on a 74HC595: one light runs along the row.
//
//   74HC595 DS (pin 14)    -> Uno pin 2   (data)
//   74HC595 STCP (pin 12)  -> Uno pin 4   (latch)
//   74HC595 SHCP (pin 11)  -> Uno pin 5   (clock)
//   74HC595 VCC and MR -> 5V      GND and OE -> GND
//   Q0 to Q7 -> eight LEDs, each through 220 ohm to GND
//
// A shift register turns three pins into eight outputs. shiftOut() clocks a
// byte into it one bit at a time, and a pulse on the latch copies all eight
// bits to the outputs at once, so the LEDs never flicker while bits move.
//
// One bit is set in the byte, and it moves up one place every quarter
// second, so one LED lights at a time from Q0 to Q7, then starts again.

const int DATA_PIN = 2;
const int LATCH_PIN = 4;
const int CLOCK_PIN = 5;

int current = 0;

void show(byte value) {
  digitalWrite(LATCH_PIN, LOW);
  shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, value);  // bit 0 lands on Q0
  digitalWrite(LATCH_PIN, HIGH);
}

void setup() {
  pinMode(DATA_PIN, OUTPUT);
  pinMode(LATCH_PIN, OUTPUT);
  pinMode(CLOCK_PIN, OUTPUT);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: eight LEDs on a 74HC595 (data 2, latch 4, clock 5)");
}

void loop() {
  byte leds = 0;
  bitSet(leds, current);
  show(leds);
  Serial.print("LED ");
  Serial.println(current);
  current = (current + 1) % 8;
  delay(250);
}

Code walkthrough

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

show sketch.ino

Pulls the latch LOW, sends the byte with shiftOut, and sets the latch HIGH. The bits move in one at a time while the latch is low, then jump to the outputs together when it rises.

void show(byte value) {
  digitalWrite(LATCH_PIN, LOW);
  shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, value);  // bit 0 lands on Q0
  digitalWrite(LATCH_PIN, HIGH);
}

setup sketch.ino

Makes pins 2, 4 and 5 outputs and opens serial at 9600.

void setup() {
  pinMode(DATA_PIN, OUTPUT);
  pinMode(LATCH_PIN, OUTPUT);
  pinMode(CLOCK_PIN, OUTPUT);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: eight LEDs on a 74HC595 (data 2, latch 4, clock 5)");
}

loop sketch.ino

Builds a byte of zeros, sets the bit numbered current with bitSet, and shows it. It prints which LED is on, then moves current forward with % 8 so it wraps from 7 back to 0. A 250 ms delay sets the speed.

void loop() {
  byte leds = 0;
  bitSet(leds, current);
  show(leds);
  Serial.print("LED ");
  Serial.println(current);
  current = (current + 1) % 8;
  delay(250);
}

Troubleshooting

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

  • The light jumps in a strange order. This project
    Check Q0 to Q7 go to LEDs 1 to 8 in order along the row.
  • The LEDs do not light. This project
    Check the wires: DS to pin 2, STCP to pin 4 and SHCP to pin 5. Also check the LED cathodes all go to GND.
  • 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.
  • A blue or white LED stays dark on 3.3 V. LED
    Blue and white LEDs drop about 3 V, so 3.3 V leaves almost nothing for the resistor. Use a 5 V supply, or a smaller resistor and a red or green LED.

Change it and keep it

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