How to use WS2812 RGB LEDs with Raspberry Pi Pico

Brilliant,

Thanks @Merlinblack for the advice!

I’m sure that the devs will have this ironed out once the next stable version of MicroPython is officially released. @Michael how interesting, would it have anything to do with Zachary running through the tutorial for the first time on a fresh install rather than updating Thorny/MicroPython? I wonder whether the update just skipped by the preinstalled array library rather than trying to purge and reinstall or update it which would have caused the issues Zachary was seeing?

2 Likes

So with the latest uPython (20210222) code appears to run (without the array error) but I also experience no LED activity. Any answers?

Hi Alan, are you connecting to the 3 alligator-clip terminals or using the breakout header?

Hi Michael - I’m using the breakout header. Is there a difference? (apart from DO which I assume could be used to daisy chain)

@Alan71240 In our last batch of rainbows there is an artwork error on the breakout header where DI and DO labels are swapped - you might have received an email with the specifics already.

Try moving your signal to the pin labelled DO. You can also try connecting your signal to the large Data tab.

I apologise personally for any frustration the error might have caused.

1 Like

Thanks very much Michael - no worries. That makes a big difference - nice coloured LEDs now :grinning: It’s OK - I very nearly tried that but easier to ask as I had only soldered a 3-pin header on the board.

3 Likes

Glad you’re on your way @Alan71240 :smiley:

1 Like

I had the array issue as well, but got round it by running the slilghtly modified pimoroni version of micropython, then I found other commands didnt work properly.

Hi Michael
Thanks for the tutorial.
i am using this code to drive a RP2040-Zero from waveshare and I would like to be able to mke the code repeat continuously . I have explored making a numberof changes but nothing works . the module I am using only has 1 rgb led and your code does work but as I said I just want it to repeat ,can you help
cheers
Nick

1 Like

You will have to put your code in an infinite loop. Something like this will loop indefinitely. All this code is pretty similar to the tutorial. The only difference is the while True: loop at the bottom. I’ve removed some unused functions to make this example as small as possible while still functional.

# Example using PIO to drive a set of WS2812 LEDs forever

import array, time
from machine import Pin
import rp2

# Configure the number of WS2812 LEDs.
NUM_LEDS = 1
PIN_NUM = 22
brightness = 0.2

@rp2.asm_pio(sideset_init=rp2.PIO.OUT_LOW, out_shiftdir=rp2.PIO.SHIFT_LEFT, autopull=True, pull_thresh=24)
def ws2812():
    T1 = 2
    T2 = 5
    T3 = 3
    wrap_target()
    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]
    wrap()


# Create the StateMachine with the ws2812 program, outputting on pin
sm = rp2.StateMachine(0, ws2812, freq=8_000_000, sideset_base=Pin(PIN_NUM))

# 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.
ar = array.array("I", [0 for _ in range(NUM_LEDS)])

##########################################################################
def pixels_show():
    dimmer_ar = array.array("I", [0 for _ in range(NUM_LEDS)])
    for i,c in enumerate(ar):
        r = int(((c >> 8) & 0xFF) * brightness)
        g = int(((c >> 16) & 0xFF) * brightness)
        b = int((c & 0xFF) * brightness)
        dimmer_ar[i] = (g<<16) + (r<<8) + b
    sm.put(dimmer_ar, 8)
    time.sleep_ms(10)

def pixels_set(i, color):
    ar[i] = (color[1]<<16) + (color[0]<<8) + color[2]

while True:
    pixels_set(0, (255,0,0)) # Red
    pixels_show()
    time.sleep(1)
    pixels_set(0, (0,0,0)) # Off
    time.sleep(1)
3 Likes

Thank you kindly. The method I used was long and bulky and still was not infinite. Inwill use this to Learn from and experiment with
cheers
Nick

1 Like

Hey guys, good demo. May I suggest you update the
PIN_NUM = 22
instead of 6 in the original program to match the wiring image?

Cheers,
Kay

Hey thanks for the heads up @Kaushalya211367 - not sure how that went unnoticed for so long :sweat_smile:

I had previously used Python code on Pi which required complex c driver code and root permissions to provide the tight timing (I have written my own on Pi so understand the difficulties).

This Pico code is simple and seems like magic - it works like a charm.

2 Likes

Hey Ian,

Thanks for the positive feedback! We do our best to provide support for this stuff, so I am glad that the code worked out perfectly.

Cheers,
Blayden

1 Like