Repository navigation
Cool Coding Projects — Neopixel Matrix
JPMichon edited this page Oct 31, 2026
·
2 revisions
Bring the iconic "Matrix Digital Rain" falling code effect to life on an 8x8 NeoPixel RGB LED matrix. This optimized version implements multi-column simultaneous drops, fluid physics via floating-point speed increments, global brightness scaling, and proper memory offsets to account for the uCompute onboard status LED.
-
The PCB Pixel 0 Offset: The uCompute hardware architecture routes the onboard status LED directly to index
0of the NeoPixel chain, immediately preceding the 8x8 matrix expansion rail. To prevent blinding layout flashes or component overheating, the rendering pipeline explicitly isolatesnp[0] = (0, 0, 0)during refresh cycles. -
Procedural Physics Control: Drop tracking uses floating-point additions (
drops[x] += speeds[x] * 0.4) paired with an implicit vertical down-casting (int(drops[x])). This guarantees organic speed variations between columns without creating array-bound allocation panics. -
Safe Power Mitigation: Driving multiple NeoPixels at maximum current can overload common voltage regulators. The integration of a software-defined
BRIGHTNESS = 0.1dampening factor guarantees safe electrical current draw during long-running pedagogical demos.
import machine
import neopixel
import time
import random
# --- HARDWARE CONFIGURATION & PARAMETERS ---
PIN_NEOPIXEL = 23
NB_COLS = 8
NB_ROWS = 8
# --- CONFIGURATION OPTIONS ---
# Set to True if using uCompute with an onboard status LED at index 0.
# Set to False if your 8x8 matrix starts directly at index 0.
# if you run this code on the RetroPico with neopixel onboard
SKIP_PCB_LED = False
# Calculate total pixel count based on the hardware layout configuration
TOTAL_PIXELS = (1 if SKIP_PCB_LED else 0) + (NB_COLS * NB_ROWS)
# Global brightness dampening factor (0.0 to 1.0)
BRIGHTNESS = 0.1
# Initialize standard MicroPython NeoPixel library
np = neopixel.NeoPixel(machine.Pin(PIN_NEOPIXEL), TOTAL_PIXELS)
# --- MATRIX DROP MATRIX STRUCTURE ---
drops = [0] * NB_COLS
speeds = [random.randint(1, 3) for _ in range(NB_COLS)]
def init_drops():
"""Initializes start positions for all columns above the visible viewport"""
for i in range(NB_COLS):
drops[i] = random.randint(-4, 0)
def set_matrix_pixel(x, y, color):
"""
Renders an XY pixel on the 8x8 matrix while dynamically handling the
PCB LED hardware offset configuration and applying the brightness coefficient.
"""
if 0 <= x < NB_COLS and 0 <= y < NB_ROWS:
# Dynamic index calculation based on layout rules
if SKIP_PCB_LED:
pixel_index = 1 + (y * NB_COLS + x) # +1 skips the onboard PCB status LED
else:
pixel_index = y * NB_COLS + x # Matrix layout starts straight at index 0
# Safe brightness scaling (integer conversion is mandatory under MicroPython)
r = int(color[0] * BRIGHTNESS)
g = int(color[1] * BRIGHTNESS)
b = int(color[2] * BRIGHTNESS)
np[pixel_index] = (r, g, b)
def update_matrix():
"""Calculates real-time Matrix rain physics and flushes data to the hardware"""
# Hardware Override Safety: Lock down the PCB diagnostic LED if active
if SKIP_PCB_LED:
np[0] = (0, 0, 0)
# Fast flush the rest of the matrix data stream
start_index = 1 if SKIP_PCB_LED else 0
for i in range(start_index, TOTAL_PIXELS):
np[i] = (0, 0, 0)
for x in range(NB_COLS):
head_y = int(drops[x])
# Generate the falling trail profile (graduated green values up the Y-axis)
for length in range(4):
current_y = head_y - length
if 0 <= current_y < NB_ROWS:
if length == 0:
# Drop Head (Ultra bright green/white blend)
color = (50, 250, 50)
elif length == 1:
# Shiny upper body
color = (0, 255, 0)
elif length == 2:
# Standard body midtone
color = (0, 140, 0)
else:
# Fading dark green tail
color = (0, 60, 0)
set_matrix_pixel(x, current_y, color)
# Advance the position vector using the column speed variable
drops[x] += speeds[x] * 0.4
# Reset column state vectors once the tail fully clears the bottom edge
if drops[x] - 4 >= NB_ROWS:
drops[x] = random.randint(-3, 0)
speeds[x] = random.randint(1, 3)
# Perform low-level bitstream flush down the GP23 rail
np.write()
# --- MAIN RUNTIME ENTRY POINT ---
init_drops()
while True:
update_matrix()
time.sleep_ms(90) # Adjust animation fluidness and processing cycle pacing