Skip to content

Raspberry Pi Pico 2 W Drives WS2812B LEDs with 12 Effects and Browser Control

Published
Author
peppe8o
Reads
564

A MicroPython library adds brightness control, 12 animations and local HTTP commands for WS2812B LED rings and strips on the Pico.

Raspberry Pi Pico 2 W Drives WS2812B LEDs with 12 Effects and Browser Control

A Raspberry Pi Pico or Pico 2 W can do more than set a WS2812B pixel to a fixed colour. A small MicroPython module, p8o_pico.py, adds 12 lighting effects, brightness scaling and browser control over Wi-Fi, while keeping the basic LED interface straightforward. The setup suits indicators, decorative lighting and small interactive projects where an addressable strip or ring needs more than a static output.

neopixel-pinout-2

The hardware connection is simple: supply the NeoPixel module from the Pico’s VBUS pin, connect ground, and drive its data input from GP28. From there, developers can begin with MicroPython’s built-in neopixel module or use the added classes for buffered updates, animations and remote commands.

How WS2812B pixels pass data

NeoPixel is a common name for addressable RGB LED products based on WS2812B chips. A strip or ring combines multiple packages, each of which contains an RGB LED and control circuitry. The package exposes four connections: VDD or 5V for power, VSS or GND, data input (DI or DIN), and data output (DO or DOUT).

peppe8o author image
Common NeoPixel LED formats

Each chip receives a serial data stream using non-return-to-zero timing. After a reset signal, the first device takes the initial 24-bit colour value from its data input and latches it for its LED. It passes the remaining stream along through its data output, allowing later chips in the chain to receive their own colour values. That daisy-chain arrangement is why the controller needs to know the total pixel count across all connected modules.

WS2812B chip pinout
WS2812B reference: datasheet
WS2812B data timing diagram
WS2812B timing reference: datasheet

Typical WS2812B supply guidance in the stated specifications is 4.5–5.5 V, with limits listed as +3.5–+5.3 V. The listed operating temperature range is -25 to +80 °C. For data input levels, the high-level minimum is 0.7 Vdd and the low-level maximum is 0.3 Vdd; input current is ±1 µA. These values are useful when checking a specific module and its interface requirements.

website

Wiring and the first test

Connect Pico VBUS (pin 40) to the module’s VDD, a Pico ground pin to VSS, and GP28 to DI. The example setup uses GND on pin 38. Some LED modules arrive without soldered headers or wires, so connections may need to be soldered. The hardware list is short: a Pico with a micro USB cable, a NeoPixel module, jumper wires and, optionally, a breadboard. A computer is needed for setup and programming; the procedure uses MicroPython with an IDE such as Thonny.

Wiring diagram connecting a NeoPixel to a Raspberry Pi Pico
Raspberry Pi PicoNeoPixel
VBUS (pin 40)VDD
GND (pin 38)VSS (GND)
GP28DI

For additional rings or strip sections, connect the data output of one module to the next module’s data input. Update the software’s pixel count to include every LED in the chain. The physical wiring and configured count need to agree for pixel addressing to behave as intended.

x
NeoPixel and Raspberry Pi Pico 2 W wiring, view one
NeoPixel and Raspberry Pi Pico 2 W wiring, view two
NeoPixel and Raspberry Pi Pico 2 W wiring, view three
NeoPixel and Raspberry Pi Pico 2 W wiring, view four
NeoPixel and Raspberry Pi Pico 2 W wiring, view five
NeoPixel and Raspberry Pi Pico 2 W wiring, view six

After installing MicroPython on the Pico, a minimal check can light the first of 12 LEDs red:

import machine, neopixel

num_pixels = 12
neo_di = machine.Pin(28)
np = neopixel.NeoPixel(neo_di, num_pixels)

np[0] = (255, 0, 0)
np.write()

The code creates a NeoPixel object on GP28, sets pixel index 0 to the RGB value (255, 0, 0), then sends the buffered value to the hardware with write(). Pixel numbering starts at zero. This small test checks the data connection and basic driver operation before adding animation or network code.

facebook
First red LED test with a WS2812B and Raspberry Pi Pico 2 W

Buffered colour control and brightness

The p8o_pico.py file is available from the p8o_pico GitHub repository or as a raw Python file. Save it to the Pico’s root directory under the name p8o_pico.py. The module provides NeoPixel for local control and WebNeoPixel for Wi-Fi and HTTP control.

NeoPixel stores logical RGB values in an internal buffer and applies brightness scaling when it sends them to the LEDs. That means changing brightness doesn’t repeatedly modify the saved colour values and compound rounding loss. The constructor takes a GPIO number and LED count, plus optional brightness, channel order and automatic-write settings:

instagram
from p8o_pico import NeoPixel

ring = NeoPixel(
    gp=28,
    num_leds=12,
    brightness=0.25,
    order="RGB",
    auto_write=False
)

Brightness ranges from 0.0 to 1.0, with 1.0 as the default. The channel-order setting accepts "RGB", "GRB" or "BGR", which is useful because LED products can differ in the order they expect colour channels. With auto_write=False, pixel changes remain buffered until show() sends them to the strip. Setting it to True updates the LEDs as changes are made.

Individual LEDs can be changed with set_pixel(index, color) or ordinary indexing, and the interface supports negative indices and slices. Colours are RGB tuples with channel values from 0 to 255. The class also provides fill(), on() and off(); the latter two update the strip immediately. percentage() lights a proportional part of the strip, while hsv_to_rgb() converts hue, saturation and value into an RGB tuple. Its hue input is in the range [0..360), with saturation and value in [0..1].

linkedin
from p8o_pico import NeoPixel
from time import sleep

ring = NeoPixel(gp=28, num_leds=12)

ring.set_pixel(0, (255, 0, 0))
ring[3] = (0, 255, 0)
ring[6:9] = (0, 0, 255)
ring.show()
sleep(2)

ring.percentage(50, color=(0, 220, 90), bg_color=(10, 10, 20))
sleep(2)

cyan = NeoPixel.hsv_to_rgb(180, 1.0, 1.0)
ring.on(cyan)
sleep(2)
ring.off()

The examples demonstrate why a buffer helps: several pixels can be prepared before one refresh, avoiding intermediate display states while the pattern is being assembled. Each animation accepts a frame interval in milliseconds through delay_ms and a cycle count; cycles=None runs continuously. The animation methods use a try...finally structure that switches the LEDs off when the animation exits or receives a keyboard interrupt.

Twelve effects for rings and strips

The library includes effects with different motion and display patterns. chase() moves one pixel along the strip with a fading tail; spinner() wraps a rotating tail around a ring. pulse() fades all LEDs in and out, while rainbow_cycle() rotates a full-spectrum pattern. A scanner() bounces a bar back and forth, and theater_chase() produces a spaced marquee pattern.

youtube

For less regular patterns, twinkle() picks random pixels to sparkle over a background colour. wipe() lights pixels progressively, and flash() produces repeated blinks. vu_meter() simulates an audio-level bar with low, mid and high colours, but the example is an animation rather than a measured audio input. gradient_shift() scrolls a two-colour gradient, while police() alternates two colours across the strip’s halves.

Timing and pattern parameters can be adjusted at the call site. For example, chase() defaults to a 50 ms frame interval and a two-pixel tail; spinner() uses a four-pixel tail by default. The scanner’s default bar width is two, and the theater chase spaces lit pixels three positions apart. These controls make the same routines usable on different pixel counts, though the result naturally depends on the physical length and layout of the LEDs.

pinterest

Control over Wi-Fi from a browser

WebNeoPixel inherits the local pixel methods and adds Wi-Fi connection handling and a lightweight HTTP server. The example asks the user to provide network name, password and a two-character country code, then starts the server on port 8080:

from p8o_pico import WebNeoPixel

ring = WebNeoPixel(gp=28, num_leds=12, brightness=0.25, order="RGB")

try:
    ip = ring.connect_wifi(
        "YOUR_SSID",
        "YOUR_PASSWORD",
        country="YOUR_2_DIG_COUNTRY_CODE",
        verbose=True
    )
    ring.serve(port=8080)
finally:
    ring.off()

Connection details, including the assigned IP address, appear in the MicroPython shell. Use that address with port 8080 to reach the Pico from another device on the same local network. The example notes that reserving the address in the router helps keep it consistent across sessions.

The HTTP interface accepts commands as query strings. Requests can turn all LEDs on in white or a specified RGB colour, fill the strip, set one pixel, show a percentage bar with chosen foreground and background colours, change global brightness, or switch the LEDs off. For example, /?percentage(75,(0,220,90)) lights 75 percent of the pixels in the specified foreground colour, while /?brightness(0.1) sets the brightness multiplier to 0.1.

Valid commands return HTTP/1.1 200 OK with the body success; malformed arguments or unknown commands return HTTP/1.1 400 Bad Request and error. The server has no TLS or authentication, so its intended setting is a trusted local network. It provides a simple control path for a bench prototype or local display, not an authenticated remote service.

Browser response from remote NeoPixel control on the Raspberry Pi Pico 2 W

Commands can also be sent from a terminal with curl, using the Pico’s assigned address:

curl -i "http://192.168.1.25:8080/?set_pixel(0,(255,0,0))"
curl -i "http://192.168.1.25:8080/?percentage(75,(0,220,90))"
curl -i "http://192.168.1.25:8080/?brightness(0.08)"
curl -i "http://192.168.1.25:8080/?off()"

A separate demo script exercises the library’s methods and can be downloaded as neopixel_pico_demo.py. To run a project without keeping the Pico attached to a computer, save the desired program on the board as main.py. MicroPython runs that file at startup; keep p8o_pico.py in the root directory under its library filename.

The result is a compact path from a one-pixel wiring check to a network-controlled lighting module. The most useful design choices are the configurable pixel count and channel order, buffered updates, and a brightness setting that preserves the application’s original colour values.

Source: peppe8o · peppe8o.com

Related reading