Accessing NeoPixel on Pico

Firstly pressed on

Micropython port for ESP8266/ESP32 system has been every intuitive and promote easy access to play with WS2812 led a.k.a neopixel. Here is the snapshot of how easy it is, taken from micropython documentation

from machine import Pin
from neopixel import NeoPixel

pin = Pin(0, Pin.OUT)   # set GPIO0 to output to drive NeoPixels
np = NeoPixel(pin, 8)   # create NeoPixel driver on GPIO0 for 8 pixels
np[0] = (255, 255, 255) # set the first pixel to white
np.write()              # write data to all pixels
r, g, b = np[0]         # get first pixel colour

Meanwhile Raspberry Pi Pico took different approach as it done via interface with PIO. Check Pi’s blog page to know more about PIO (link). So it requires more lines of code to get it done. Thus i prepared a wrapper to simplify the process when working with neopixel leds.

import array, time
from machine import Pin
from rp2 import PIO, StateMachine, asm_pio

class NeoPixel:
    def __init__(self, NUM_LEDS=1,pin=28):
        @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
            out(x, 1) .side(0) [T3 - 1]
            jmp(not_x, "do_zero") .side(1) [T1 - 1]
            jmp("bitloop") .side(1) [T2 - 1]
            nop() .side(0) [T2 - 1]
        self.num=NUM_LEDS = StateMachine(0, ws2812, freq=8000000, sideset_base=Pin(pin)) = array.array("I", [0 for _ in range(self.num)])
    def display(self,num=0,color):
        r,g,b=color[num]= r<<8 | g<<16 | b,8)

We can create a good filename such as and save it in the microcontroller’s flash storage. From this point onwards, the code will be much simpler. To access one neopixel wired to pin 28, we just need to type the following.

from neopixel import NeoPixel

Thanks for reading and hope this post is useful. Until next time.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s