How to use WS2812 RGB LEDs with Raspberry Pi Pico

New RPi Pico guide just released: “How to use WS2812 RGB LEDs with Raspberry Pi Pico”

3 Likes

With the Pico Rainbow example had to change each instance of time to utime

I have a 16 LED ring and your code works, thanks! I used pin0, no problem. However, I had to set the # of LEDs to 21. Noticing that 3/4, 7/8, 11/12, 15/16, and 19/20 were linked, is this normal?

Hi Joshua,

You’re probably it with RGBW LEDs? They need an extra byte to tell the white LED what to do, else your data will get spread across the LEDs weirdly. Check-out this thread:

GOT IT! I had to change my threshold from 24 to 32. That’s why they were off by 3/4. Kept throwing 8 bits away. Thanks again for your tutorial!
@rp2.asm_pio(sideset_init=rp2.PIO.OUT_LOW, out_shiftdir=rp2.PIO.SHIFT_LEFT, autopull=True, pull_thresh=32)
OTHER REFERENCE:

3 Likes

Fantastic!

Thanks for sharing Joshua, it’s always the little things and hopefully, this will help someone else in the future with a similar issue. All the best with your projects!

2 Likes

I just tried to follow this tutorial but I’m running into issues with the array module not importing properly.
I get this from a paste of the code example:

Traceback (most recent call last):
  File "<stdin>", line 3, in <module>
ImportError: no module named 'array'

I’m inexperienced with this kind of thing and googling hasn’t really helped. The only thing I can think of is maybe its related to the current version of micropython on my board (20210202-v1.14).
Any ideas?

3 Likes

I have got the same issue and I can’t find anything about it

1 Like

Hey Zachary and Edward,

No worries, welcome to the forum! Can you please put the code that you’ve setup here on the forum. We’ll take a look at the syntax and see what we can figure out in order to fix this.

I’m not at my computer atm, but it was an exact copy and paste from the tutorial page.
I’ll try to share the code later today.

1 Like

Hi Zac,

Did you follow the Thonny setup guide first?

1 Like

the code I used

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

import array
import time
from machine import Pin
import rp2

# Configure the number of WS2812 LEDs.
NUM_LEDS = 60
PIN_NUM = 0
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]

def pixels_fill(color):
    for i in range(len(ar)):
        pixels_set(i, color)

def color_chase(color, wait):
    for i in range(NUM_LEDS):
        pixels_set(i, color)
        time.sleep(wait)
        pixels_show()
    time.sleep(0.2)

def wheel(pos):
    # Input a value 0 to 255 to get a color value.
    # The colours are a transition r - g - b - back to r.
    if pos < 0 or pos > 255:
        return (0, 0, 0)
    if pos < 85:
        return (255 - pos * 3, pos * 3, 0)
    if pos < 170:
        pos -= 85
        return (0, 255 - pos * 3, pos * 3)
    pos -= 170
    return (pos * 3, 0, 255 - pos * 3)


def rainbow_cycle(wait):
    for j in range(255):
        for i in range(NUM_LEDS):
            rc_index = (i * 256 // NUM_LEDS) + j
            pixels_set(i, wheel(rc_index & 255))
        pixels_show()
        time.sleep(wait)

BLACK = (0, 0, 0)
RED = (255, 0, 0)
YELLOW = (255, 150, 0)
GREEN = (0, 255, 0)
CYAN = (0, 255, 255)
BLUE = (0, 0, 255)
PURPLE = (180, 0, 255)
WHITE = (255, 255, 255)
COLORS = (BLACK, RED, YELLOW, GREEN, CYAN, BLUE, PURPLE, WHITE)

print("fills")
for color in COLORS:
    pixels_fill(color)
    pixels_show()
    time.sleep(0.2)

print("chases")
for color in COLORS:
    color_chase(color, 0.01)

print("rainbow")
rainbow_cycle(0)

the error message

Traceback (most recent call last):
  File "<stdin>", line 3, in <module>
ImportError: no module named 'array'
2 Likes

Hi Edward,

Make sure you’ve got the correct interpreter selected:

I reset my pico and I then followed the instructions in the guide and I am still getting the error that the module array there and also I am running micropython (Raspberry pi pico) and 1.14

Hey Edward,

What we’ve got here is an issue with the particular library array. Out of curiousity to see whether or not the module is installed correctly. Can you please run the line import array from your Repl Interface with the Pico connected to your machine then the line array? I’m interested to see whether the library actually appears correctly, or whether there has been an issue with the library installations.

Hi @Edward153770
As a sanity-check I reprogrammed my Pico with the code from the tutorial - this worked for me
I also followed Oliver’s advice and ran import array in the REPL which executed without problems.
The below image is a screenshot of my working environment.

All I can think of is the version of MicroPython/Thonny you might be running. My REPL initialises with the following message:

This is v1.13

You mentioned that you were running 1.14.
To make sure we’re on the same page, have you used the update/install firmware utility in Thonny?

2 Likes

I’ve hit the array import issue also. I have MicroPython 1.14 installed, and typing ‘import array’ in the REPL gives the following.
Traceback (most recent call last):
File “”, line 1, in
ImportError: no module named ‘array’

It looks like this is a known issue, and they (MicroPython developers) are working on it.
See rp2 port no module named array · Issue #6837 · micropython/micropython · GitHub

2 Likes

I’ve got the code running by grabbing the latest MicroPython uf2 from here:
https://www.raspberrypi.org/documentation/pico/getting-started/
Do the standard hold down BootSel and connect procedure and copy the uf2 onto the pico manually rather than let Thonny download and install it.
If you read the Github Issue I posted earlier, they mentioned they changed the link of the above page to be the latest ‘unstable’ version of MicroPython which does not have the array problem.

So thats my code running. Now to see why I have no blinky LEDs!

3 Likes

Great detective-work @Merlinblack!
I wonder why the Thonny update worked for me and v1.13

2 Likes

Thanks, that fixed it!

3 Likes