# How to use WS2812 RGB LEDs with Raspberry Pi Pico

**URL:** <https://forum.core-electronics.com.au/t/how-to-use-ws2812-rgb-leds-with-raspberry-pi-pico/9229>\
**Category:** Guides\
**Created:** [22 January 2021 07:27 UTC](https://forum.core-electronics.com.au/t/how-to-use-ws2812-rgb-leds-with-raspberry-pi-pico/9229 "2021-01-22T07:27:26Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/michael/32/3545_2.png) [@Michael](https://forum.core-electronics.com.au/u/Michael)\
**Post date:** [22 January 2021 07:27 UTC](https://forum.core-electronics.com.au/t/how-to-use-ws2812-rgb-leds-with-raspberry-pi-pico/9229/1 "2021-01-22T07:27:26Z")

</div>

New RPi Pico guide just released: [**“How to use WS2812 RGB LEDs with Raspberry Pi Pico”**](https://core-electronics.com.au/tutorials/how-to-use-ws2812b-rgb-leds-with-raspberry-pi-pico.html)  
  
 ![](https://core-electronics.com.au/media/kbase/Pico-ws2812-feature.png)

---

<div class="post-metadata">

**Author:** ![John153237](https://avatars.discourse-cdn.com/v4/letter/j/ac8455/32.png) [@John153237](https://forum.core-electronics.com.au/u/John153237)\
**Post date:** [2 February 2021 19:27 UTC](https://forum.core-electronics.com.au/t/how-to-use-ws2812-rgb-leds-with-raspberry-pi-pico/9229/2 "2021-02-02T19:27:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Joshua153254](https://avatars.discourse-cdn.com/v4/letter/j/278dde/32.png) [@Joshua153254](https://forum.core-electronics.com.au/u/Joshua153254)\
**Post date:** [2 February 2021 23:28 UTC](https://forum.core-electronics.com.au/t/how-to-use-ws2812-rgb-leds-with-raspberry-pi-pico/9229/3 "2021-02-02T23:28:29Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Oliver](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/oliver/32/3315_2.png) [@Oliver](https://forum.core-electronics.com.au/u/Oliver)\
**Post date:** [2 February 2021 23:31 UTC](https://forum.core-electronics.com.au/t/how-to-use-ws2812-rgb-leds-with-raspberry-pi-pico/9229/4 "2021-02-02T23:31:32Z")

</div>

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:

> [@WS2812 & pico](https://forum.core-electronics.com.au/t/ws2812-pico/9283/10):
>
> Hi Paul, Not coming at you with any solutions just yet. I reviewed the code for someone at Core to do a bit of testing with. I found [this page here](https://www.raspberrypi.org/blog/neopixel-dithering-with-pico/) from the Raspberry Pi Foundation outlining some similar code in C++. I’ll add some comments with explanations as to what the code is/might be doing (I have marked them with ’ #L - ') # Example using PIO to drive a set of WS2812 LEDs. #L - Importing the libraries commonly used in microcontrollers import array, time from machine import Pin impor…

---

<div class="post-metadata">

**Author:** ![Joshua153254](https://avatars.discourse-cdn.com/v4/letter/j/278dde/32.png) [@Joshua153254](https://forum.core-electronics.com.au/u/Joshua153254)\
**Post date:** [3 February 2021 01:28 UTC](https://forum.core-electronics.com.au/t/how-to-use-ws2812-rgb-leds-with-raspberry-pi-pico/9229/5 "2021-02-03T01:28:28Z")

</div>

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:

> **[NeoPixel dithering with Pico - Raspberry Pi](https://www.raspberrypi.org/blog/neopixel-dithering-with-pico/)**
>
> In the special Pico launch issue of HackSpace magazine, Ben shows you how to get extra levels of brightness out of your LEDs with our new toy

---

<div class="post-metadata">

**Author:** ![Bryce](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/bryce/32/3631_2.png) [@Bryce](https://forum.core-electronics.com.au/u/Bryce)\
**Post date:** [3 February 2021 04:13 UTC](https://forum.core-electronics.com.au/t/how-to-use-ws2812-rgb-leds-with-raspberry-pi-pico/9229/6 "2021-02-03T04:13:54Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Zachary50853](https://avatars.discourse-cdn.com/v4/letter/z/59ef9b/32.png) [@Zachary50853](https://forum.core-electronics.com.au/u/Zachary50853)\
**Post date:** [7 February 2021 02:52 UTC](https://forum.core-electronics.com.au/t/how-to-use-ws2812-rgb-leds-with-raspberry-pi-pico/9229/7 "2021-02-07T02:52:37Z")

</div>

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:

```auto
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?

---

<div class="post-metadata">

**Author:** ![Edward153770](https://avatars.discourse-cdn.com/v4/letter/e/53a042/32.png) [@Edward153770](https://forum.core-electronics.com.au/u/Edward153770)\
**Post date:** [7 February 2021 16:19 UTC](https://forum.core-electronics.com.au/t/how-to-use-ws2812-rgb-leds-with-raspberry-pi-pico/9229/8 "2021-02-07T16:19:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Bryce](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/bryce/32/3631_2.png) [@Bryce](https://forum.core-electronics.com.au/u/Bryce)\
**Post date:** [7 February 2021 23:30 UTC](https://forum.core-electronics.com.au/t/how-to-use-ws2812-rgb-leds-with-raspberry-pi-pico/9229/9 "2021-02-07T23:30:33Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Zachary50853](https://avatars.discourse-cdn.com/v4/letter/z/59ef9b/32.png) [@Zachary50853](https://forum.core-electronics.com.au/u/Zachary50853)\
**Post date:** [7 February 2021 23:38 UTC](https://forum.core-electronics.com.au/t/how-to-use-ws2812-rgb-leds-with-raspberry-pi-pico/9229/10 "2021-02-07T23:38:54Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Oliver](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/oliver/32/3315_2.png) [@Oliver](https://forum.core-electronics.com.au/u/Oliver)\
**Post date:** [8 February 2021 00:29 UTC](https://forum.core-electronics.com.au/t/how-to-use-ws2812-rgb-leds-with-raspberry-pi-pico/9229/11 "2021-02-08T00:29:45Z")

</div>

Hi Zac,

Did you follow the Thonny setup guide first?

> **[How to Setup a Raspberry Pi Pico and Code with Thonny - Tutorial](https://core-electronics.com.au/tutorials/how-to-setup-a-raspberry-pi-pico-and-code-with-thonny.html)**
>
> Let's get set up with scripting in Thonny for the Raspberry Pi Pico. 
> We'll install Thonny, configure for Pico and write our first script. To follow along you'll need:
> 
> A Raspberry Pi Pico
> A USB micro B lead
> 
> 
> Contents
> 
> Install Thonny
> Set up...

---

<div class="post-metadata">

**Author:** ![Edward153770](https://avatars.discourse-cdn.com/v4/letter/e/53a042/32.png) [@Edward153770](https://forum.core-electronics.com.au/u/Edward153770)\
**Post date:** [8 February 2021 09:10 UTC](https://forum.core-electronics.com.au/t/how-to-use-ws2812-rgb-leds-with-raspberry-pi-pico/9229/12 "2021-02-08T09:10:05Z")

</div>

the code I used

```auto
# 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

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

```

---

<div class="post-metadata">

**Author:** ![Oliver](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/oliver/32/3315_2.png) [@Oliver](https://forum.core-electronics.com.au/u/Oliver)\
**Post date:** [8 February 2021 22:20 UTC](https://forum.core-electronics.com.au/t/how-to-use-ws2812-rgb-leds-with-raspberry-pi-pico/9229/13 "2021-02-08T22:20:30Z")

</div>

Hi Edward,

Make sure you’ve got the correct interpreter selected:

> **[How to Setup a Raspberry Pi Pico and Code with Thonny - Tutorial](https://core-electronics.com.au/tutorials/how-to-setup-a-raspberry-pi-pico-and-code-with-thonny.html#setup-thonny)**
>
> Let's get set up with scripting in Thonny for the Raspberry Pi Pico. 
> We'll install Thonny, configure for Pico and write our first script. To follow along you'll need:
> 
> A Raspberry Pi Pico
> A USB micro B lead
> 
> 
> Contents
> 
> Install Thonny
> Set up...

---

<div class="post-metadata">

**Author:** ![Edward153770](https://avatars.discourse-cdn.com/v4/letter/e/53a042/32.png) [@Edward153770](https://forum.core-electronics.com.au/u/Edward153770)\
**Post date:** [9 February 2021 08:18 UTC](https://forum.core-electronics.com.au/t/how-to-use-ws2812-rgb-leds-with-raspberry-pi-pico/9229/14 "2021-02-09T08:18:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Bryce](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/bryce/32/3631_2.png) [@Bryce](https://forum.core-electronics.com.au/u/Bryce)\
**Post date:** [10 February 2021 04:51 UTC](https://forum.core-electronics.com.au/t/how-to-use-ws2812-rgb-leds-with-raspberry-pi-pico/9229/15 "2021-02-10T04:51:58Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Michael](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/michael/32/3545_2.png) [@Michael](https://forum.core-electronics.com.au/u/Michael)\
**Post date:** [10 February 2021 05:09 UTC](https://forum.core-electronics.com.au/t/how-to-use-ws2812-rgb-leds-with-raspberry-pi-pico/9229/16 "2021-02-10T05:09:37Z")

</div>

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._

 ![image](https://canada1.discourse-cdn.com/flex028/uploads/core_electronics/original/2X/a/a1a59ea22da79f5a05f4957778d7a3774fca3fb4.png)

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

 ![image](https://canada1.discourse-cdn.com/flex028/uploads/core_electronics/original/2X/6/6aeb68980995ff87c220661382aabc1c2581975d.png)

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?

 ![image](https://canada1.discourse-cdn.com/flex028/uploads/core_electronics/original/2X/4/4fcc2d4a752e1748c6624d89ef8c8230a4d5738e.png)

---

<div class="post-metadata">

**Author:** ![Merlinblack](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/merlinblack/32/4120_2.png) [@Merlinblack](https://forum.core-electronics.com.au/u/Merlinblack)\
**Post date:** [10 February 2021 11:40 UTC](https://forum.core-electronics.com.au/t/how-to-use-ws2812-rgb-leds-with-raspberry-pi-pico/9229/17 "2021-02-10T11:40:21Z")

</div>

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](https://github.com/micropython/micropython/issues/6837)

---

<div class="post-metadata">

**Author:** ![Merlinblack](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/merlinblack/32/4120_2.png) [@Merlinblack](https://forum.core-electronics.com.au/u/Merlinblack)\
**Post date:** [10 February 2021 12:43 UTC](https://forum.core-electronics.com.au/t/how-to-use-ws2812-rgb-leds-with-raspberry-pi-pico/9229/18 "2021-02-10T12:43:59Z")

</div>

I’ve got the code running by grabbing the latest MicroPython uf2 from here:  
[https://www.raspberrypi.org/documentation/pico/getting-started/](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!

---

<div class="post-metadata">

**Author:** ![Michael](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/michael/32/3545_2.png) [@Michael](https://forum.core-electronics.com.au/u/Michael)\
**Post date:** [10 February 2021 22:11 UTC](https://forum.core-electronics.com.au/t/how-to-use-ws2812-rgb-leds-with-raspberry-pi-pico/9229/19 "2021-02-10T22:11:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Andy21094](https://avatars.discourse-cdn.com/v4/letter/a/9f8e36/32.png) [@Andy21094](https://forum.core-electronics.com.au/u/Andy21094)\
**Post date:** [11 February 2021 07:59 UTC](https://forum.core-electronics.com.au/t/how-to-use-ws2812-rgb-leds-with-raspberry-pi-pico/9229/20 "2021-02-11T07:59:20Z")

</div>

Thanks, that fixed it!

[Next page](https://forum.core-electronics.com.au/t/how-to-use-ws2812-rgb-leds-with-raspberry-pi-pico/9229.md?page=2)
