Built-in project · Arduino Uno R3

LED bar chaser on an Arduino Uno

An Arduino Uno runs a bar and a bouncing dot along eight LEDs through a 74HC595. 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

  • Shift registers: eight outputs from three pins
  • Clock, data and latch lines
  • Bits and bit shifting to build LED patterns
  • Why each LED needs its own 220 ohm resistor
  • 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 sketch sends one byte down the data line, and each bit of that byte decides whether one LED is on.

The chip holds the new byte quietly until the latch pin pulses. Then all eight LEDs change at once, so you never see half-finished patterns.

Every pass of loop plays a bar that fills from the left and empties, then a single dot that runs to the right end and back.

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 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
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
// Chaser: eight LEDs, three pins, one 74HC595.
//
// Wiring, shift register to board:
//   DS   (pin 14, serial data)  -> pin 4
//   SHCP (pin 11, shift clock)  -> pin 5
//   STCP (pin 12, latch clock)  -> pin 6
//   MR   (pin 10) to 5 V, OE (pin 13) to GND
//   Q0..Q7 through 220 ohm resistors to eight LEDs to GND
//
// shiftOut with MSBFIRST sends bit 7 first, and the bit sent first ends up
// furthest along the register, so bit n lands on Qn. Q0 is the left-hand LED
// here, which is why a growing bar is (1 << n) - 1 and a running dot is 1 << n.
//
// The latch clock is pulsed only after all eight bits are in, which is the
// whole point of the chip: the outputs sit still while the byte is being
// clocked in and all change together at the end, so nothing flickers through
// the intermediate patterns.

const int DATA_PIN = 4;   // DS
const int SHIFT_PIN = 5;  // SHCP
const int LATCH_PIN = 6;  // STCP

const int BAR_MS = 80;
const int DOT_MS = 70;

static void show(uint8_t bits) {
  shiftOut(DATA_PIN, SHIFT_PIN, MSBFIRST, bits);
  digitalWrite(LATCH_PIN, HIGH);
  digitalWrite(LATCH_PIN, LOW);
}

void setup() {
  pinMode(DATA_PIN, OUTPUT);
  pinMode(SHIFT_PIN, OUTPUT);
  pinMode(LATCH_PIN, OUTPUT);
  digitalWrite(SHIFT_PIN, LOW);
  digitalWrite(LATCH_PIN, LOW);
  Serial.begin(115200);
  Serial.println("Mokxi Arduino Uno: eight LEDs on three pins");
}

void loop() {
  // the bar fills from the left, then empties
  for (int n = 1; n <= 8; n++) {
    show((uint8_t)((1u << n) - 1u));
    delay(BAR_MS);
  }
  for (int n = 7; n >= 0; n--) {
    show((uint8_t)((1u << n) - 1u));
    delay(BAR_MS);
  }
  // then a single dot runs to the right and back
  for (int n = 0; n < 8; n++) {
    show((uint8_t)(1u << n));
    delay(DOT_MS);
  }
  for (int n = 6; n >= 1; n--) {
    show((uint8_t)(1u << n));
    delay(DOT_MS);
  }
}

Code walkthrough

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

show sketch.ino

This is the whole trick. shiftOut clocks eight bits into the 74HC595 on DATA_PIN and SHIFT_PIN. Then LATCH_PIN goes HIGH and LOW, which copies the byte to the outputs in one step. Bit n lights the LED on output Qn.

static void show(uint8_t bits) {
  shiftOut(DATA_PIN, SHIFT_PIN, MSBFIRST, bits);
  digitalWrite(LATCH_PIN, HIGH);
  digitalWrite(LATCH_PIN, LOW);
}

setup sketch.ino

Sets the data, shift and latch pins as outputs and starts both clock lines LOW. It also opens the serial monitor at 115200 and prints a hello line.

void setup() {
  pinMode(DATA_PIN, OUTPUT);
  pinMode(SHIFT_PIN, OUTPUT);
  pinMode(LATCH_PIN, OUTPUT);
  digitalWrite(SHIFT_PIN, LOW);
  digitalWrite(LATCH_PIN, LOW);
  Serial.begin(115200);
  Serial.println("Mokxi Arduino Uno: eight LEDs on three pins");
}

loop sketch.ino

Builds patterns with shifts. (1u << n) - 1u makes n bits all on, which is a bar of n LEDs. 1u << n makes a single bit, which is one running dot. The delays are BAR_MS (80) and DOT_MS (70).

void loop() {
  // the bar fills from the left, then empties
  for (int n = 1; n <= 8; n++) {
    show((uint8_t)((1u << n) - 1u));
    delay(BAR_MS);
  }
  for (int n = 7; n >= 0; n--) {
    show((uint8_t)((1u << n) - 1u));
    delay(BAR_MS);
  }
  // then a single dot runs to the right and back
  for (int n = 0; n < 8; n++) {
    show((uint8_t)(1u << n));
    delay(DOT_MS);
  }
  for (int n = 6; n >= 1; n--) {
    show((uint8_t)(1u << n));
    delay(DOT_MS);
  }
}

Troubleshooting

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

  • No LEDs light at all This project
    Check that the chip's OE pin goes to GND and its MR pin goes to 5V. OE high turns the outputs off, and MR low clears them.
  • The LEDs show scrambled patterns This project
    Make sure data, shift and latch go to pins 4, 5 and 6 on the chip's DS, SHCP and STCP. Swapping two of them mixes up the bits.
  • One LED is dark while the rest work This project
    Check that LED's resistor and its direction. The long leg (anode) faces the resistor, and the short leg goes to ground.
  • 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.