Towers of Hanoi — RGB LED matrix display


An alternative output for the Kosmos CP1 Hanoi solver: instead of (or alongside) the robot, the CP1 streams each move to an Arduino Mega 2560 that animates the disks on a 64×32 RGB LED matrix.

The CP1 does the real work — the recursion and the move sequence. This sketch is just the renderer: it keeps the three-peg tower model, receives each from→to move, and animates the disk lift → traverse → drop. Disks are rainbow-colored by size; three blue pegs rise from a bright blue base line. The display auto-resets for each new run.

The real rig — a Kosmos CP1 (right) driving the 64×32 panel (left) mid-solve over the four-bit move protocol; the CP2 cassette module (top) loads the program — click to watch on YouTube

▶ Watch the full solve on YouTube

Starting layout — five rainbow disks on three blue pegs over a blue base

A full four-disk solve animated on the panel

It reuses the exact 4-bit move protocol from the robot extension, so the CP1 program (robot/HANOIC-CP1-ROBOT.txt) needs no changes.

Hardware


ItemNotes
Arduino Mega 25605 V (matches the CP1 ports → no level shifters); 8 KB RAM holds a 64×32 framebuffer (an Uno's 2 KB cannot)
Adafruit RGB Matrix ShieldHUB75 adapter + panel-power terminal (needs a small reroute on the Mega, below)
64×32 HUB75 RGB panel1/16 scan, A–D address lines
5 V / ≥2 A supplypowers the panel only, common ground with the Mega

Library: RGBmatrixPanel + Adafruit GFX. (Adafruit's newer Protomatter has no 8-bit AVR support, so on the Mega RGBmatrixPanel is the library to use — deprecated but stable.)

Build / flash


arduino-cli core install arduino:avr
arduino-cli lib install "RGB matrix Panel" "Adafruit GFX Library"
arduino-cli compile --fqbn arduino:avr:mega matrix/hanoi_matrix
arduino-cli upload  --fqbn arduino:avr:mega -p /dev/ttyACM0 matrix/hanoi_matrix

Set NUM_DISKS at the top of the sketch to match the CP1 program's disk count (CP1 address 008). 5–7 is the sweet spot for the exhibit (clear disks, 2ᴺ−1 moves ≈ ¾–3 min); up to ~8 still reads clearly, ~13 is the screen/CP1 max but the disks blur and a full solve takes hours.

CP1 ↔ Mega link — 5 V direct, no level shifters


CP1signalMega
Port 2 pin 1D0 (move bit 0)D30
Port 2 pin 2D1 (move bit 1)D31
Port 2 pin 3D2 (move bit 2)D32
Port 2 pin 4STROBE (toggles per move)D33
Port 1 pin 1BUSY (Mega → CP1, HIGH while busy)D34
GNDGND (must be common)

Protocol (same as robot/): the CP1 waits for BUSY low, sets D0–D2, toggles STROBE; the Mega latches the code, raises BUSY, animates, drops BUSY. Move codes: 1=0→1 2=0→2 3=1→0 4=1→2 5=2→0 6=2→1. Code 7 forces a reset (optional — the display also auto-resets when a finished solve is followed by a new move).

Matrix wiring — Mega + RGB Matrix Shield


RGBmatrixPanel hardwires the six RGB data pins to PORTA (Mega D24–D29) and needs the clock on PORTB (pin 11). The shield is laid out for the Uno, so reroute two groups of signals:

Matrix signalShield defaultWire to (Mega)Mod
R1, G1, B1, R2, G2, B2D2–D7D24, D25, D26, D27, D28, D29cut from D2–D7, jumper to D24–D29 (keep order)
CLKD8D11cut from D8, jumper to D11
LATD10D10no change
OED9D9no change
A, B, C, DA0–A3A0–A3no change

Power the panel from its own 5 V supply via the shield screw terminals (common ground with the Mega) — not from the Mega.

Credit


The Mega sketch and this document were written by Claude (Opus 4.8), directed by LambdaMikel — same collaboration as the paper and the robot extension. The recursive CP1 Hanoi program itself is LambdaMikel's hand-written work.