Skip to main content

SK6812 Matrix – Erweiterte Beispiele (MicroPython)

Sammlung von Beispielcodes für fortgeschrittene Animationen und Effekte mit der SK6812 RGBW LED-Matrix.


#Grundgerüst

Dieses Grundgerüst wird für alle folgenden Beispiele benötigt:

from machine import Pin
from neopixel import NeoPixel
from time import sleep_ms
import math
import random
# Konfiguration
LED_PIN = 14
NUM_LEDS = 64
MATRIX_WIDTH = 8
MATRIX_HEIGHT = 8
# NeoPixel initialisieren (bpp=4 für RGBW)
np = NeoPixel(Pin(LED_PIN), NUM_LEDS, bpp=4)

#Hilfsfunktionen

Diese Funktionen werden von mehreren Beispielen verwendet:

def clear():
"""Schaltet alle LEDs aus."""
for i in range(NUM_LEDS):
np[i] = (0, 0, 0, 0)
np.write()
def fill(r, g, b, w=0):
"""Füllt alle LEDs mit einer Farbe."""
for i in range(NUM_LEDS):
np[i] = (r, g, b, w)
np.write()
def xy_to_index(x, y):
"""Konvertiert X/Y-Koordinaten zu LED-Index."""
if 0 <= x < MATRIX_WIDTH and 0 <= y < MATRIX_HEIGHT:
return y * MATRIX_WIDTH + x
return -1
def set_pixel(x, y, r, g, b, w=0):
"""Setzt eine einzelne LED an Position (x, y)."""
index = xy_to_index(x, y)
if index >= 0:
np[index] = (r, g, b, w)
def hsv_to_rgb(h, s, v):
"""Konvertiert HSV (0-1) zu RGB (0-255)."""
if s == 0:
return int(v * 255), int(v * 255), int(v * 255)
i = int(h * 6)
f = (h * 6) - i
p = int(255 * v * (1 - s))
q = int(255 * v * (1 - s * f))
t = int(255 * v * (1 - s * (1 - f)))
v = int(255 * v)
i = i % 6
if i == 0: return v, t, p
if i == 1: return q, v, p
if i == 2: return p, v, t
if i == 3: return p, q, v
if i == 4: return t, p, v
if i == 5: return v, p, q

#Beispiel 1: Grundfarben anzeigen

def demo_farben():
"""Zeigt nacheinander die Grundfarben."""
farben = [
(255, 0, 0, 0), # Rot
(0, 255, 0, 0), # Grün
(0, 0, 255, 0), # Blau
(0, 0, 0, 255), # Weiß (W-Kanal)
(255, 255, 0, 0), # Gelb
(255, 0, 255, 0), # Magenta
(0, 255, 255, 0), # Cyan
]
for farbe in farben:
fill(*farbe)
sleep_ms(1000)
clear()
# Test
demo_farben()

#Beispiel 2: Regenbogen-Animation

def demo_regenbogen():
"""Regenbogen-Animation über alle LEDs."""
for durchlauf in range(3): # 3 Durchläufe
for offset in range(256):
for i in range(NUM_LEDS):
# Farbton basierend auf LED-Position und Offset
hue = ((i * 256 // NUM_LEDS) + offset) % 256 / 255
r, g, b = hsv_to_rgb(hue, 1.0, 0.3) # Helligkeit 30%
np[i] = (r, g, b, 0)
np.write()
sleep_ms(20)
clear()
# Test
demo_regenbogen()

#Beispiel 3: Matrix-Scan

def demo_matrix_scan():
"""Scannt zeilenweise durch die Matrix."""
for y in range(MATRIX_HEIGHT):
for x in range(MATRIX_WIDTH):
clear()
set_pixel(x, y, 0, 0, 255, 0) # Blau
np.write()
sleep_ms(50)
clear()
# Test
demo_matrix_scan()

#Beispiel 4: Welleneffekt

def demo_welle():
"""Sinuswellen-Animation über die Matrix."""
for frame in range(200):
for y in range(MATRIX_HEIGHT):
for x in range(MATRIX_WIDTH):
# Sinuswelle basierend auf Position und Zeit
wert = math.sin((x + y + frame * 0.3) * 0.5)
helligkeit = int((wert + 1) * 127) # 0-255
set_pixel(x, y, 0, helligkeit, helligkeit // 2, 0)
np.write()
sleep_ms(50)
clear()
# Test
demo_welle()

#Beispiel 5: Formen zeichnen

def zeichne_linie(x0, y0, x1, y1, r, g, b, w=0):
"""Zeichnet eine Linie (Bresenham-Algorithmus)."""
dx = abs(x1 - x0)
dy = -abs(y1 - y0)
sx = 1 if x0 < x1 else -1
sy = 1 if y0 < y1 else -1
err = dx + dy
while True:
set_pixel(x0, y0, r, g, b, w)
if x0 == x1 and y0 == y1:
break
e2 = 2 * err
if e2 >= dy:
err += dy
x0 += sx
if e2 <= dx:
err += dx
y0 += sy
def zeichne_rechteck(x, y, breite, hoehe, r, g, b, w=0, gefuellt=False):
"""Zeichnet ein Rechteck."""
for py in range(y, y + hoehe):
for px in range(x, x + breite):
if gefuellt or px == x or px == x + breite - 1 or py == y or py == y + hoehe - 1:
set_pixel(px, py, r, g, b, w)
def zeichne_kreis(cx, cy, radius, r, g, b, w=0):
"""Zeichnet einen Kreis (Midpoint-Algorithmus)."""
x = radius
y = 0
err = 0
while x >= y:
set_pixel(cx + x, cy + y, r, g, b, w)
set_pixel(cx + y, cy + x, r, g, b, w)
set_pixel(cx - y, cy + x, r, g, b, w)
set_pixel(cx - x, cy + y, r, g, b, w)
set_pixel(cx - x, cy - y, r, g, b, w)
set_pixel(cx - y, cy - x, r, g, b, w)
set_pixel(cx + y, cy - x, r, g, b, w)
set_pixel(cx + x, cy - y, r, g, b, w)
y += 1
err += 1 + 2 * y
if 2 * (err - x) + 1 > 0:
x -= 1
err += 1 - 2 * x
def demo_formen():
"""Zeigt verschiedene Formen."""
# Linie
clear()
zeichne_linie(0, 0, 7, 7, 255, 0, 0) # Rot
np.write()
sleep_ms(1000)
# Rechteck (Rahmen)
clear()
zeichne_rechteck(1, 1, 6, 6, 0, 255, 0) # Grün
np.write()
sleep_ms(1000)
# Rechteck (gefüllt)
clear()
zeichne_rechteck(2, 2, 4, 4, 0, 0, 255, gefuellt=True) # Blau
np.write()
sleep_ms(1000)
# Kreis
clear()
zeichne_kreis(3, 3, 3, 255, 255, 0) # Gelb
np.write()
sleep_ms(1000)
clear()
# Test
demo_formen()

#Beispiel 6: Schachbrett-Muster

def demo_schachbrett():
"""Zeigt ein animiertes Schachbrett-Muster."""
for frame in range(20):
for y in range(MATRIX_HEIGHT):
for x in range(MATRIX_WIDTH):
# Schachbrett mit wechselnder Phase
if (x + y + frame) % 2 == 0:
set_pixel(x, y, 255, 255, 255, 0) # Weiß
else:
set_pixel(x, y, 0, 0, 0, 0) # Aus
np.write()
sleep_ms(300)
clear()
# Test
demo_schachbrett()

#Beispiel 7: Feuerwerk-Effekt

def demo_feuerwerk():
"""Simuliert einen Feuerwerk-Effekt."""
for _ in range(5): # 5 Explosionen
# Zufälliger Startpunkt
cx = random.randint(2, 5)
cy = random.randint(2, 5)
# Zufällige Farbe
r = random.randint(100, 255)
g = random.randint(100, 255)
b = random.randint(100, 255)
# Explosion expandieren
for radius in range(1, 5):
clear()
zeichne_kreis(cx, cy, radius, r, g, b)
np.write()
sleep_ms(100)
# Verblassen
for helligkeit in range(255, 0, -30):
faktor = helligkeit / 255
clear()
zeichne_kreis(cx, cy, 4, int(r * faktor), int(g * faktor), int(b * faktor))
np.write()
sleep_ms(50)
clear()
sleep_ms(200)
# Test
demo_feuerwerk()

#Beispiel 8: Pulsierendes Herz

# Herz-Bitmap (8x8)
HERZ = [
0b01100110,
0b11111111,
0b11111111,
0b11111111,
0b01111110,
0b00111100,
0b00011000,
0b00000000,
]
def demo_herz():
"""Zeigt ein pulsierendes Herz."""
for _ in range(10): # 10 Pulse
# Aufhellen
for helligkeit in range(0, 256, 10):
for y in range(MATRIX_HEIGHT):
for x in range(MATRIX_WIDTH):
if HERZ[y] & (1 << (7 - x)):
set_pixel(x, y, helligkeit, 0, helligkeit // 4, 0)
else:
set_pixel(x, y, 0, 0, 0, 0)
np.write()
sleep_ms(20)
# Abdunkeln
for helligkeit in range(255, -1, -10):
for y in range(MATRIX_HEIGHT):
for x in range(MATRIX_WIDTH):
if HERZ[y] & (1 << (7 - x)):
set_pixel(x, y, helligkeit, 0, helligkeit // 4, 0)
else:
set_pixel(x, y, 0, 0, 0, 0)
np.write()
sleep_ms(20)
clear()
# Test
demo_herz()

#Beispiel 9: Text-Scrolling

# Minimaler 3x5 Pixel Font
FONT = {
'A': [0b010, 0b101, 0b111, 0b101, 0b101],
'B': [0b110, 0b101, 0b110, 0b101, 0b110],
'C': [0b011, 0b100, 0b100, 0b100, 0b011],
'D': [0b110, 0b101, 0b101, 0b101, 0b110],
'E': [0b111, 0b100, 0b110, 0b100, 0b111],
'F': [0b111, 0b100, 0b110, 0b100, 0b100],
'G': [0b011, 0b100, 0b101, 0b101, 0b011],
'H': [0b101, 0b101, 0b111, 0b101, 0b101],
'I': [0b111, 0b010, 0b010, 0b010, 0b111],
'J': [0b111, 0b001, 0b001, 0b101, 0b010],
'K': [0b101, 0b110, 0b100, 0b110, 0b101],
'L': [0b100, 0b100, 0b100, 0b100, 0b111],
'M': [0b101, 0b111, 0b101, 0b101, 0b101],
'N': [0b101, 0b111, 0b111, 0b111, 0b101],
'O': [0b010, 0b101, 0b101, 0b101, 0b010],
'P': [0b110, 0b101, 0b110, 0b100, 0b100],
'Q': [0b010, 0b101, 0b101, 0b110, 0b011],
'R': [0b110, 0b101, 0b110, 0b101, 0b101],
'S': [0b011, 0b100, 0b010, 0b001, 0b110],
'T': [0b111, 0b010, 0b010, 0b010, 0b010],
'U': [0b101, 0b101, 0b101, 0b101, 0b010],
'V': [0b101, 0b101, 0b101, 0b010, 0b010],
'W': [0b101, 0b101, 0b101, 0b111, 0b101],
'X': [0b101, 0b101, 0b010, 0b101, 0b101],
'Y': [0b101, 0b101, 0b010, 0b010, 0b010],
'Z': [0b111, 0b001, 0b010, 0b100, 0b111],
'0': [0b010, 0b101, 0b101, 0b101, 0b010],
'1': [0b010, 0b110, 0b010, 0b010, 0b111],
'2': [0b110, 0b001, 0b010, 0b100, 0b111],
'3': [0b110, 0b001, 0b010, 0b001, 0b110],
'4': [0b101, 0b101, 0b111, 0b001, 0b001],
'5': [0b111, 0b100, 0b110, 0b001, 0b110],
'6': [0b011, 0b100, 0b110, 0b101, 0b010],
'7': [0b111, 0b001, 0b010, 0b010, 0b010],
'8': [0b010, 0b101, 0b010, 0b101, 0b010],
'9': [0b010, 0b101, 0b011, 0b001, 0b110],
' ': [0b000, 0b000, 0b000, 0b000, 0b000],
'!': [0b010, 0b010, 0b010, 0b000, 0b010],
}
def scrolle_text(text, r=255, g=100, b=0):
"""Scrollt Text vertikal über die Matrix."""
# Text zu Bitmap konvertieren
bitmap = []
for zeichen in text.upper():
if zeichen in FONT:
for zeile in FONT[zeichen]:
bitmap.append(zeile)
bitmap.append(0) # Leerzeile zwischen Zeichen
# Scrollen
for scroll in range(len(bitmap) + MATRIX_HEIGHT):
clear()
for y in range(MATRIX_HEIGHT):
bitmap_y = scroll - MATRIX_HEIGHT + y + 1
if 0 <= bitmap_y < len(bitmap):
zeile = bitmap[bitmap_y]
for x in range(3):
if zeile & (1 << (2 - x)):
set_pixel(x + 2, y, r, g, b, 0)
np.write()
sleep_ms(150)
clear()
# Test
scrolle_text("HALLO")

#Beispiel 10: Spirale

def demo_spirale():
"""Zeichnet eine Spirale von außen nach innen."""
# Richtungen: rechts, unten, links, oben
dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]
x, y = 0, 0
richtung = 0
besucht = [[False] * MATRIX_WIDTH for _ in range(MATRIX_HEIGHT)]
for schritt in range(NUM_LEDS):
# Zufällige Farbe für jeden Schritt
hue = schritt / NUM_LEDS
r, g, b = hsv_to_rgb(hue, 1.0, 0.5)
set_pixel(x, y, r, g, b, 0)
np.write()
besucht[y][x] = True
sleep_ms(50)
# Nächste Position berechnen
nx = x + dx[richtung]
ny = y + dy[richtung]
# Richtung ändern wenn nötig
if not (0 <= nx < MATRIX_WIDTH and 0 <= ny < MATRIX_HEIGHT and not besucht[ny][nx]):
richtung = (richtung + 1) % 4
nx = x + dx[richtung]
ny = y + dy[richtung]
x, y = nx, ny
sleep_ms(1000)
clear()
# Test
demo_spirale()

#Beispiel 11: Alle Demos nacheinander

def alle_demos():
"""Führt alle Demo-Effekte nacheinander aus."""
print("Demo 1: Grundfarben")
demo_farben()
sleep_ms(500)
print("Demo 2: Regenbogen")
demo_regenbogen()
sleep_ms(500)
print("Demo 3: Matrix-Scan")
demo_matrix_scan()
sleep_ms(500)
print("Demo 4: Welle")
demo_welle()
sleep_ms(500)
print("Demo 5: Formen")
demo_formen()
sleep_ms(500)
print("Demo 6: Schachbrett")
demo_schachbrett()
sleep_ms(500)
print("Demo 7: Feuerwerk")
demo_feuerwerk()
sleep_ms(500)
print("Demo 8: Herz")
demo_herz()
sleep_ms(500)
print("Demo 9: Spirale")
demo_spirale()
clear()
print("Alle Demos abgeschlossen!")
# Hauptprogramm
while True:
alle_demos()
sleep_ms(2000)

#Vollständiges Programm

Hier alle Funktionen in einer Datei zum direkten Verwenden:

"""
SK6812 RGBW Matrix - Alle Demos (MicroPython)
Vollständiges Beispiel für das Lernboard
"""
from machine import Pin
from neopixel import NeoPixel
from time import sleep_ms
import math
import random
# === Konfiguration ===
LED_PIN = 14
NUM_LEDS = 64
MATRIX_WIDTH = 8
MATRIX_HEIGHT = 8
np = NeoPixel(Pin(LED_PIN), NUM_LEDS, bpp=4)
# === Hilfsfunktionen ===
# (Hier alle Hilfsfunktionen von oben einfügen)
# === Demo-Funktionen ===
# (Hier alle Demo-Funktionen von oben einfügen)
# === Hauptprogramm ===
print("SK6812 Matrix - Demo gestartet")
print("Druecke Ctrl+C zum Beenden")
while True:
alle_demos()
sleep_ms(2000)

#Stromverbrauch-Tabelle

Szenario Stromverbrauch (ca.)
Alle LEDs aus < 1 mA
Eine LED weiß (100%) 60 mA
Alle LEDs rot (100%) 1.28 A
Alle LEDs weiß (100%) 3.84 A
Alle LEDs weiß (25%) ca. 1 A
Typische Animation 0.5 A – 1.5 A

Empfehlung: Helligkeit begrenzen für stabilen Betrieb über USB (RGB-Werte max. 50-100).


#Zurück zur Hauptdokumentation

SK6812 RGBW LED-Matrix