Introduction
In this video, we try to program WS2812B NeoPixel LED Stick Using Raspberry Pi Pico. The NeoPixel is controlled using a programmable I/O (PIO) system on a Pico microcontroller.
Video
Watch this video to know more about it.
Hardware Preparation
This is the list of items used in the video.

import array, time | |
from machine import Pin | |
import rp2 | |
from rp2 import PIO, StateMachine, asm_pio | |
# Configure the number of WS2812 LEDs. | |
NUM_LEDS = 8 | |
@asm_pio(sideset_init=PIO.OUT_LOW, out_shiftdir=PIO.SHIFT_LEFT, | |
autopull=True, pull_thresh=24) | |
def ws2812(): | |
T1 = 2 | |
T2 = 5 | |
T3 = 3 | |
label("bitloop") | |
out(x, 1) .side(0) [T3 – 1] | |
jmp(not_x, "do_zero") .side(1) [T1 – 1] | |
jmp("bitloop") .side(1) [T2 – 1] | |
label("do_zero") | |
nop() .side(0) [T2 – 1] | |
# Create the StateMachine with the ws2812 program, outputting on Pin(0). | |
sm = StateMachine(0, ws2812, freq=8000000, sideset_base=Pin(0)) | |
# Start the StateMachine, it will wait for data on its FIFO. | |
sm.active(1) | |
# Display a pattern on the LEDs via an array of LED RGB values. | |
pixel_array = array.array("I", [0 for _ in range(NUM_LEDS)]) | |
############################################ | |
# Functions for RGB Coloring | |
############################################ | |
def updatePixel(brightness=1): # dimming colors and updating state machine (state_mach) | |
dimmer_array = array.array("I", [0 for _ in range(NUM_LEDS)]) | |
for ii,cc in enumerate(pixel_array): | |
r = int(((cc >> 8) & 0xFF) * brightness) | |
g = int(((cc >> 16) & 0xFF) * brightness) | |
b = int((cc & 0xFF) * brightness) | |
dimmer_array[ii] = (g<<16) + (r<<8) + b | |
sm.put(dimmer_array, 8) # update the state machine with new colors | |
def set_led_color(color): | |
for ii in range(len(pixel_array)): | |
pixel_array[ii] = (color[1]<<16) + (color[0]<<8) + color[2] | |
#Color based on RGB (R,G,B) | |
red = (255,0,0) | |
green = (0,255,0) | |
blue = (0,0,255) | |
white = (255,255,255) | |
set_led_color(red) | |
updatePixel(1) | |
time.sleep(1) | |
set_led_color(green) | |
updatePixel(1) | |
time.sleep(1) | |
set_led_color(blue) | |
updatePixel(1) | |
time.sleep(1) | |
set_led_color(white) | |
updatePixel(1) | |
time.sleep(1) | |
References
- WS2812 LED Ring Light with Raspberry Pi Pico
- How to use WS2812B RGB LEDs with Raspberry Pi Pico
- Python enumerate(): Simplify Looping With Counters
Thanks for reading this tutorial. If you have any technical inquiries, please post at Cytron Technical Forum.
“Please be reminded, this tutorial is prepared for you to try and learn.
You are encouraged to improve the code for better application.“