Built-in project · Arduino Uno R3

4x4x4 LED cube on an Arduino Uno

A 64 LED cube drawn as four labeled layers: two 74HC595s drive sixteen columns, four transistors switch the layers, and the Uno multiplexes rain, a plane sweep, a spiral and sparkle. The whole build, an Arduino Uno and 91 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.

Advanced About 1.5 hours to build and run. Runs in your browser. Free, and no account needed.

Build it as the LED Cube Kit: the parts in numbered bags and a booklet that checks every step. Retails for about $52, free on Mokxi.

The circuit runs here once the page has loaded.

What you'll learn

  • Multiplexing: lighting one layer at a time, very fast
  • Chaining outputs with 74HC595 shift registers
  • Switching a whole layer with a transistor
  • Storing a picture as bits in a number
  • Timing several jobs with millis instead of delay
  • Difficulty: Advanced
  • Time: About 1.5 hours
  • Board: Arduino Uno R3
  • Parts: 95

How it works

The cube has 64 LEDs but the Uno only uses about 20 pins. All LEDs in a vertical column share their anode, and all LEDs in a layer share their cathode. Two 74HC595 chips control the 16 columns, and four transistors connect one layer at a time to ground.

The sketch lights layer 0 for 2 ms, then layer 1, 2 and 3, and repeats. That is 125 full passes a second, so your eye sees all four layers lit at once.

The picture is four 16-bit numbers, one per layer. The animations only edit those numbers. There are four: rain, plane sweep, spiral and random. The button skips ahead, and they also change every 8 seconds.

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 (1) pin GNDArduino Uno R3 pin GND
74HC595 shift register (1) pin OEArduino Uno R3 pin GND
74HC595 shift register (2) pin GNDArduino Uno R3 pin GND
74HC595 shift register (2) pin OEArduino Uno R3 pin GND
Pushbutton pin 2aArduino Uno R3 pin GND
74HC595 shift register (1) pin SHCPArduino Uno R3 pin 13
74HC595 shift register (2) pin SHCPArduino Uno R3 pin 12
74HC595 shift register (1) pin DSArduino Uno R3 pin 11
74HC595 shift register (1) pin STCPArduino Uno R3 pin 10
74HC595 shift register (2) pin STCPArduino Uno R3 pin 9
74HC595 shift register (2) pin DSArduino Uno R3 pin 8
Pushbutton pin 1aArduino Uno R3 pin 7
74HC595 shift register (1) pin MRArduino Uno R3 pin 5V
74HC595 shift register (1) pin VCCArduino Uno R3 pin 5V
74HC595 shift register (2) pin MRArduino Uno R3 pin 5V
74HC595 shift register (2) pin VCCArduino Uno R3 pin 5V
Transistor (NPN or PNP) (1) pin EArduino Uno R3 pin GND
Transistor (NPN or PNP) (2) pin EArduino Uno R3 pin GND
Transistor (NPN or PNP) (3) pin EArduino Uno R3 pin GND
Transistor (NPN or PNP) (4) pin EArduino Uno R3 pin GND
Resistor, 1k Ω (4) pin 1Arduino Uno R3 pin A0
Resistor, 1k Ω (3) pin 1Arduino Uno R3 pin A1
Resistor, 1k Ω (2) pin 1Arduino Uno R3 pin A2
Resistor, 1k Ω (1) pin 1Arduino Uno R3 pin A3
Resistor, 220 Ω (2) pin 174HC595 shift register (1) pin Q1
Resistor, 220 Ω (3) pin 174HC595 shift register (1) pin Q2
Resistor, 220 Ω (4) pin 174HC595 shift register (1) pin Q3
Resistor, 220 Ω (5) pin 174HC595 shift register (1) pin Q4
Resistor, 220 Ω (6) pin 174HC595 shift register (1) pin Q5
Resistor, 220 Ω (7) pin 174HC595 shift register (1) pin Q6
Resistor, 220 Ω (8) pin 174HC595 shift register (1) pin Q7
Resistor, 220 Ω (1) pin 174HC595 shift register (1) pin Q0
Resistor, 220 Ω (10) pin 174HC595 shift register (2) pin Q1
Resistor, 220 Ω (11) pin 174HC595 shift register (2) pin Q2
Resistor, 220 Ω (12) pin 174HC595 shift register (2) pin Q3
Resistor, 220 Ω (13) pin 174HC595 shift register (2) pin Q4
Resistor, 220 Ω (14) pin 174HC595 shift register (2) pin Q5
Resistor, 220 Ω (15) pin 174HC595 shift register (2) pin Q6
Resistor, 220 Ω (16) pin 174HC595 shift register (2) pin Q7
Resistor, 220 Ω (9) pin 174HC595 shift register (2) pin Q0
LED, blue (1) pin AResistor, 220 Ω (1) pin 2
LED, blue (2) pin AResistor, 220 Ω (1) pin 2
LED, blue (3) pin AResistor, 220 Ω (1) pin 2
LED, blue (4) pin AResistor, 220 Ω (1) pin 2
LED, blue (5) pin CLED, blue (1) pin C
LED, blue (9) pin CLED, blue (1) pin C
LED, blue (13) pin CLED, blue (1) pin C
LED, blue (17) pin CLED, blue (1) pin C
LED, blue (21) pin CLED, blue (1) pin C
LED, blue (25) pin CLED, blue (1) pin C
LED, blue (29) pin CLED, blue (1) pin C
LED, blue (33) pin CLED, blue (1) pin C
LED, blue (37) pin CLED, blue (1) pin C
LED, blue (41) pin CLED, blue (1) pin C
LED, blue (45) pin CLED, blue (1) pin C
LED, blue (49) pin CLED, blue (1) pin C
LED, blue (53) pin CLED, blue (1) pin C
LED, blue (57) pin CLED, blue (1) pin C
LED, blue (61) pin CLED, blue (1) pin C
Transistor (NPN or PNP) (1) pin CLED, blue (1) pin C

112 more connections: open it in the simulator to see them all.

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
// LED cube: a 4x4x4 cube of 64 LEDs, multiplexed one layer at a time.
//
//   pins 11, 13, 10   DS, SHCP and STCP of the first 74HC595 (columns 0-7)
//   pins 8, 12, 9     DS, SHCP and STCP of the second (columns 8-15)
//   A0-A3          the four layer transistors' bases, through 1 k each
//   pin 7          the button, other side to GND (internal pull-up)
//
// # Sixteen columns, four layers
//
// Every LED in a vertical column shares its anode, and every LED in a layer
// shares its cathode. Two 74HC595s, each on three pins of its own, drive
// the sixteen columns through 220 ohm each. Four NPN transistors switch the layers to ground.
// Light column 5 and switch on layer 2 and exactly one LED glows.
//
// # Multiplexing
//
// Only one layer is ever on. The sketch shows layer 0 for 2 ms, then layer
// 1, then 2, then 3, and starts again: 125 times a second, far faster than
// your eye can follow, so all four look lit at once. That is how 64 LEDs
// run from 20 pins.
//
// # The frame
//
// The picture is four 16-bit numbers, one per layer, bit n for column n.
// The animations only ever change those numbers; refresh() keeps showing
// whatever they hold. The button skips to the next animation, and they also
// change on their own every eight seconds.

// Data, shift clock and latch for each chip.
const int DATA[2] = {11, 8};
const int CLOCK[2] = {13, 12};
const int LATCH[2] = {10, 9};
const int LAYERS[4] = {A0, A1, A2, A3};
const int BUTTON = 7;

const char *const NAMES[] = {"rain", "plane sweep", "spiral", "random"};
const int ANIMATIONS = 4;

uint16_t frame[4];
int layer = 0;
int animation = 0;
int stepCount = 0;
unsigned long refreshAt = 0;
unsigned long stepAt = 0;
unsigned long changedAt = 0;
bool wasDown = false;
unsigned long pressedAt = 0;

// Column n is at x = n % 4, y = n / 4, as seen from above.
uint16_t cell(int x, int y) { return (uint16_t)1 << (y * 4 + x); }

// One byte into each chip, then both latches together, so the sixteen
// columns change at the same moment.
void shift16(uint16_t columns) {
  for (int chip = 0; chip < 2; chip++) {
    digitalWrite(LATCH[chip], LOW);
    shiftOut(DATA[chip], CLOCK[chip], MSBFIRST, chip == 0 ? columns & 0xFF : columns >> 8);
  }
  for (int chip = 0; chip < 2; chip++) digitalWrite(LATCH[chip], HIGH);
}

// Show the next layer: all layers off, new columns, then this layer on.
// Turning the old layer off first stops a faint ghost of its pattern
// appearing on the new one while the bits shift in.
void refresh() {
  digitalWrite(LAYERS[layer], LOW);
  layer = (layer + 1) % 4;
  shift16(frame[layer]);
  digitalWrite(LAYERS[layer], HIGH);
}

void clearFrame() {
  for (int z = 0; z < 4; z++) frame[z] = 0;
}

// Rain: drops start at the top layer and fall one layer a step.
void rain() {
  for (int z = 0; z < 3; z++) frame[z] = frame[z + 1];
  frame[3] = 0;
  for (int i = 0; i < 2; i++) frame[3] |= (uint16_t)1 << random(16);
}

// Plane sweep: a full wall of light moves through the cube, front to back,
// then a floor moves bottom to top.
void sweep() {
  clearFrame();
  int pos = stepCount % 8;
  if (pos < 4) {
    uint16_t row = 0;
    for (int x = 0; x < 4; x++) row |= cell(x, pos);
    for (int z = 0; z < 4; z++) frame[z] = row;
  } else {
    frame[pos - 4] = 0xFFFF;
  }
}

// Spiral: one column walks the outer ring of twelve, a layer higher each
// lap, with a tail of three behind it.
const uint8_t RING[12] = {0, 1, 2, 3, 7, 11, 15, 14, 13, 12, 8, 4};
void spiral() {
  clearFrame();
  for (int t = 0; t < 4; t++) {
    int s = stepCount - t;
    if (s < 0) continue;
    int z = (s / 12) % 4;
    frame[z] |= (uint16_t)1 << RING[s % 12];
  }
}

// Random: every LED has a one in four chance of being lit, new each step.
void sparkle() {
  for (int z = 0; z < 4; z++) {
    frame[z] = (uint16_t)(random(65536) & random(65536));
  }
}

void next() {
  animation = (animation + 1) % ANIMATIONS;
  stepCount = 0;
  changedAt = millis();
  clearFrame();
  Serial.print("animation: ");
  Serial.println(NAMES[animation]);
}

void setup() {
  for (int chip = 0; chip < 2; chip++) {
    pinMode(DATA[chip], OUTPUT);
    pinMode(CLOCK[chip], OUTPUT);
    pinMode(LATCH[chip], OUTPUT);
  }
  pinMode(BUTTON, INPUT_PULLUP);
  for (int z = 0; z < 4; z++) {
    pinMode(LAYERS[z], OUTPUT);
    digitalWrite(LAYERS[z], LOW);
  }
  Serial.begin(115200);
  randomSeed(analogRead(A5));
  Serial.println("LED cube ready");
  Serial.print("animation: ");
  Serial.println(NAMES[animation]);
}

void loop() {
  unsigned long now = millis();

  if (now - refreshAt >= 2) {
    refreshAt = now;
    refresh();
  }

  // A press counts once: contacts bounce, so ignore another for 250 ms.
  bool down = digitalRead(BUTTON) == LOW;
  if (down && !wasDown && now - pressedAt > 250) {
    pressedAt = now;
    next();
  }
  wasDown = down;
  if (now - changedAt > 8000) next();

  if (now - stepAt >= 120) {
    stepAt = now;
    stepCount++;
    if (animation == 0) rain();
    if (animation == 1) sweep();
    if (animation == 2) spiral();
    if (animation == 3) sparkle();
  }
}

Code walkthrough

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

shift16 sketch.ino

Sends 16 column bits to the two chips. Each chip has its own data, clock and latch pin. It shifts the low byte into chip 0 and the high byte into chip 1, then raises both latches together so all 16 columns change at the same moment.

// One byte into each chip, then both latches together, so the sixteen
// columns change at the same moment.
void shift16(uint16_t columns) {
  for (int chip = 0; chip < 2; chip++) {
    digitalWrite(LATCH[chip], LOW);
    shiftOut(DATA[chip], CLOCK[chip], MSBFIRST, chip == 0 ? columns & 0xFF : columns >> 8);
  }
  for (int chip = 0; chip < 2; chip++) digitalWrite(LATCH[chip], HIGH);
}

refresh sketch.ino

Called every 2 ms. It turns the current layer off first, moves to the next layer, loads that layer's columns, then switches the layer on. Turning the old layer off first stops a faint ghost of its pattern from showing on the new one.

// Show the next layer: all layers off, new columns, then this layer on.
// Turning the old layer off first stops a faint ghost of its pattern
// appearing on the new one while the bits shift in.
void refresh() {
  digitalWrite(LAYERS[layer], LOW);
  layer = (layer + 1) % 4;
  shift16(frame[layer]);
  digitalWrite(LAYERS[layer], HIGH);
}

rain sketch.ino

Each animation step copies every layer down one level, so drops fall. The top layer is cleared, then two random columns are lit there as new drops. Because refresh keeps showing whatever is in frame, the animation only ever edits numbers.

// Rain: drops start at the top layer and fall one layer a step.
void rain() {
  for (int z = 0; z < 3; z++) frame[z] = frame[z + 1];
  frame[3] = 0;
  for (int i = 0; i < 2; i++) frame[3] |= (uint16_t)1 << random(16);
}

loop sketch.ino

Runs three timers. Every 2 ms it calls refresh. A button press calls next, ignoring bounces for 250 ms, and next also fires after 8000 ms. Every 120 ms the current animation takes one step.

void loop() {
  unsigned long now = millis();

  if (now - refreshAt >= 2) {
    refreshAt = now;
    refresh();
  }

  // A press counts once: contacts bounce, so ignore another for 250 ms.
  bool down = digitalRead(BUTTON) == LOW;
  if (down && !wasDown && now - pressedAt > 250) {
    pressedAt = now;
    next();
  }
  wasDown = down;
  if (now - changedAt > 8000) next();

  if (now - stepAt >= 120) {
    stepAt = now;
    stepCount++;
    if (animation == 0) rain();
    if (animation == 1) sweep();
    if (animation == 2) spiral();
    if (animation == 3) sparkle();
  }
}

Troubleshooting

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

  • The cube flickers or looks dim This project
    Check that refresh runs every 2 ms and that nothing in loop blocks for long, such as a long delay.
  • Patterns show up on the wrong layer or column This project
    Check that each transistor base is on the right A0 to A3 pin and each 74HC595 output goes to the right resistor.
  • Nothing lights up This project
    Both chips need OE and ground tied low and MR tied to 5V, and each LED needs its anode toward the column.
  • 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 input reads random HIGH and LOW when the button is not pressed. Pushbutton
    A floating pin picks up noise. Add a 10 k pull-down or pull-up resistor, or use INPUT_PULLUP and wire the button between the pin and GND. Then pressed reads LOW.
  • The button seems always pressed, or never changes. Pushbutton
    A 4-leg tactile switch has legs joined in pairs. Place it across the breadboard center gap so the pairs sit on different rows. Wired along one side, the legs are already connected.
  • 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.

Change it and keep it

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