Skip to main content

Set pixels

What this example shows​

How to light single LEDs by their (x, y) position. The example sets a low brightness, colours three corners and two pixels in the middle, once with separate red, green and blue values and once with a packed colour, and then sends everything to the grid in one show().

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 SetPixel.py from the pinned module, exactly as shipped.

# FILE: SetPixel.py
# AUTHOR: Josip Šimun Kuči @ Soldered
# BRIEF: Basic usage: set individual pixels by (x, y) coordinates on the
# Soldered 8x8 WS2812B LED grid.
# Connect the grid data line to the pin defined below.
# (0, 0) is top-left. X increases to the right, Y increases downward.
# WORKS WITH: Soldered WS2812B LED Grid: www.soldered.com
# LAST UPDATED: 2026-05-12

from machine import Pin
from WS2812Grid import WS2812Grid

PIN = Pin(6, Pin.OUT)

grid = WS2812Grid(PIN)
grid.begin()

# Set overall brightness (0 = off, 255 = full).
# Keep this low to avoid excessive current draw.
grid.setBrightness(40)

# Draw a red pixel at the top-left corner
grid.setPixel(0, 0, 255, 0, 0)

# Draw a green pixel at the top-right corner
grid.setPixel(7, 0, 0, 255, 0)

# Draw a blue pixel at the bottom-left corner
grid.setPixel(0, 7, 0, 0, 255)

# Draw a white pixel in the centre using a packed color
grid.setPixel(3, 3, WS2812Grid.Color(255, 255, 255))
grid.setPixel(4, 4, WS2812Grid.Color(255, 255, 255))

# Push all changes to the hardware
grid.show()

# Nothing to do — pixels stay lit

🔗Open the complete example

Examples/SetPixel.py at commit db93fa3

→

Expected result: the example prints nothing in the shell. When it runs, the top-left pixel (0, 0) lights red, the top-right pixel (7, 0) green, the bottom-left pixel (0, 7) blue, and the pixels at (3, 3) and (4, 4) white, all scaled to brightness 40 out of 255. They stay lit after the script ends.

Functions used​

WS2812Grid(pin, width=8, height=8)returns WS2812Grid

Creates 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

TypeNameDescription
machine.PinpinPin object connected to the grid's DIN, for example Pin(6, Pin.OUT).
intwidthNumber of columns, a multiple of 8. Default 8.
intheightNumber of rows, a multiple of 8. Default 8.
begin()returns None

Clears every pixel and calls show(), so the grid starts dark.

ReturnsNothing.

setBrightness(brightness)returns None

Sets 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

TypeNameDescription
intbrightness0 (off) to 255 (full brightness).
setPixel(x, y, r_or_color, g=None, b=None)returns None

Sets one LED by its grid position. Pass r, g and b as three values, or pass one packed colour and leave 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

TypeNameDescription
intxColumn, 0 = left.
intyRow, 0 = top.
intr_or_colorRed, 0 to 255, or a packed 0x00RRGGBB colour when g and b are left out.
intgGreen, 0 to 255. Optional.
intbBlue, 0 to 255. Optional.
WS2812Grid.Color(r, g, b)returns int

A static method that packs red, green and blue into one integer, for setPixel() with a packed colour. Call it on the class, as WS2812Grid.Color().

ReturnsThe packed colour, (r << 16) | (g << 8) | b.

Parameters

TypeNameDescription
intrRed, 0 to 255.
intgGreen, 0 to 255.
intbBlue, 0 to 255.
show()returns None

Writes the buffered colours to the LEDs.

ReturnsNothing.

Putting it together​

Create the grid with the pin wired to DIN, call begin(), set the brightness before you set any pixel, then set as many pixels as you like and finish with one show(). Nothing changes on the grid until show() runs. Raising the brightness above 40 makes the LEDs brighter and draws more current, so check your 5 V supply first.