Animations
What this example shows
How to animate the whole grid by redrawing it frame by frame. The example runs two animations in an endless loop: a row sweep that lights one row at a time from top to bottom, and a rainbow that cycles every pixel through a colour wheel, each pixel a little further along than its neighbour.
Wire VCC to 5 V, GND to GND and DIN to pin 6 of your controller, or
change the number in Pin(6, Pin.OUT) to the pin you used.
The example code
This is Animations.py from the pinned module, exactly as shipped.
# FILE: Animations.py
# AUTHOR: Josip Šimun Kuči @ Soldered
# BRIEF: Demonstrates row sweep and colour-cycle animations on the
# Soldered 8x8 WS2812B LED grid.
# Connect the grid data line to the pin defined below.
# WORKS WITH: Soldered WS2812B LED Grid: www.soldered.com
# LAST UPDATED: 2026-05-12
from machine import Pin
from WS2812Grid import WS2812Grid
import time
PIN = Pin(6, Pin.OUT)
grid = WS2812Grid(PIN)
grid.begin()
grid.setBrightness(40)
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
def colorWheel(pos):
"""Simple HSV-to-RGB wheel: hue 0-255."""
pos = 255 - pos
if pos < 85:
return WS2812Grid.Color(255 - pos * 3, 0, pos * 3)
if pos < 170:
pos -= 85
return WS2812Grid.Color(0, pos * 3, 255 - pos * 3)
pos -= 170
return WS2812Grid.Color(pos * 3, 255 - pos * 3, 0)
# ---------------------------------------------------------------------------
# Animations
# ---------------------------------------------------------------------------
def rowSweep():
for y in range(8):
grid.clear()
for x in range(8):
grid.setPixel(x, y, 0, 180, 255)
grid.show()
time.sleep_ms(80)
def rainbowGrid(cycles):
for _ in range(cycles):
for hue in range(256):
for y in range(8):
for x in range(8):
offset = (x + y * 2) & 0xFF
grid.setPixel(x, y, colorWheel((hue + offset) & 0xFF))
grid.show()
time.sleep_ms(10)
# ---------------------------------------------------------------------------
# Main loop
# ---------------------------------------------------------------------------
while True:
rowSweep()
time.sleep_ms(200)
rainbowGrid(2)
time.sleep_ms(200)
🔗Open the complete example
Examples/Animations.py at commit db93fa3
Expected result: the example prints nothing in the shell. On the grid, at brightness 40 out of 255, each row in turn lights in the colour (0, 180, 255) for 80 ms, from the top row to the bottom one. After a 200 ms pause, the rainbow steps through the 256 colours of the wheel twice, 10 ms per step, with each pixel offset along the wheel by its column plus twice its row. Another 200 ms pause, and the loop starts again until you stop the script.
Functions used
WS2812Grid(pin, width=8, height=8)returns WS2812GridCreates the grid driver: a neopixel.NeoPixel object for width x height LEDs on the given pin. One grid is 8 x 8, the default. Brightness starts at 255.
ReturnsThe grid object.
Parameters
| Type | Name | Description |
|---|---|---|
machine.Pin | pin | Pin object connected to the grid's DIN, for example Pin(6, Pin.OUT). |
int | width | Number of columns, a multiple of 8. Default 8. |
int | height | Number of rows, a multiple of 8. Default 8. |
begin()returns NoneClears every pixel and calls show(), so the grid starts dark.
ReturnsNothing.
setBrightness(brightness)returns NoneSets the brightness scale. Values outside 0 to 255 are clamped. It is applied when you set a pixel, so call it before setPixel() or fill(); pixels already set keep their brightness.
ReturnsNothing.
Parameters
| Type | Name | Description |
|---|---|---|
int | brightness | 0 (off) to 255 (full brightness). |
clear()returns NoneTurns every LED off in the buffer. It does not call show(): the LEDs go dark at the next show(). The row sweep uses it to wipe the previous row before drawing the next.
ReturnsNothing.
setPixel(x, y, r_or_color, g=None, b=None)returns NoneSets one LED by its grid position. The row sweep passes r, g and b as three values; the rainbow passes one packed colour from colorWheel() and leaves g and b out. The colour is scaled by the brightness. A position outside the grid is ignored. Call show() afterwards to send the change to the LEDs.
ReturnsNothing.
Parameters
| Type | Name | Description |
|---|---|---|
int | x | Column, 0 = left. |
int | y | Row, 0 = top. |
int | r_or_color | Red, 0 to 255, or a packed 0x00RRGGBB colour when g and b are left out. |
int | g | Green, 0 to 255. Optional. |
int | b | Blue, 0 to 255. Optional. |
WS2812Grid.Color(r, g, b)returns intA static method that packs red, green and blue into one integer. The example's colorWheel() helper uses it to turn a position on the wheel into a colour.
ReturnsThe packed colour, (r << 16) | (g << 8) | b.
Parameters
| Type | Name | Description |
|---|---|---|
int | r | Red, 0 to 255. |
int | g | Green, 0 to 255. |
int | b | Blue, 0 to 255. |
show()returns NoneWrites the buffered colours to the LEDs.
ReturnsNothing.
Putting it together
Each frame is drawn the same way: change the pixels in the buffer with
clear() and setPixel(), then call show() once and wait before the next
frame. The time.sleep_ms() after each show() sets the speed, so change the
80 and 10 to make the row sweep or the rainbow faster or slower. colorWheel()
and the two animation functions are part of the example, not the module.