Thursday, December 4, 2025

MicroPython evolves: build in neopixel library

For an index to all my stories click this text

In 2021 my book about the Raspberry Pi Pico was published. At that time MicroPython was at version 1.14. In the book there is a chapter that describes how to use neopixels with the Pico and MicroPython. You needed to download a library from github to get the neopixels working.



In later versions of MicroPython (starting at version 1.18) there is a neopixel library build in !! It is mentioned in the second row at the 4th line.

This does not mean that you can not use the "old" library that is discussed in my book. Actually the library discussed in the book has much more commands for working with neopixels as the build-in library nowadays has.

- strip.fill(r, g, b) fills the complete strip with a color
- strip.set_pixel_line(x, x, r, g, b) fills a range of pixels with a color
- strip.rotate_right(n) rotates all pixels n places right
- strip.rotate_left(n) rotatres all pixels n places left

These commands are all missing from the build-in library. Well except for one, on which later more.

Which commands are available.

First we need to load the needed libraries with:

from machine import Pin
from neopixel import NeoPixel

Then you need to define at which pin the neopixels are connected:

pin = Pin(4, Pin.OUT)

In this example the neopixels are connected to GPIO 4

Now we can start using the library.
According to the official MicroPython documentation there are only 3 commands.

- np = NeoPixel(pin, 12)
This command creates a neopixel object with the name np which is attached to pin and there are 8 neopixels connected to that pin.

- np[0] = (255, 255, 255)
This command sets the first neopixel (starting at 0) to the RGB values 255,255,255 which is full white. The values (255,255,255) need to be set exactly like this as the command aacepts them as a tuple. The first value is the value for red, the second for green and the last for blue (R, G, B) the values may vary from 0 to 255 where 0 is off and 255 is brightest.

- np.write()
And this command is absolutely needed after each time you change the colors of one or more neopixels to activate them.

Please be aware that this new library is available in MicroPython for the ESP8266, ESP32 and the Raspberry pi pico series.

Demonstration setup

Here is my demonstration setup.


As I used a neopixel ring with 12 neopixels the Raspberry Pi Pico does not supply enough current to feed the neopixel ring. So I attached a seperate USB connector which is connected to a USB power supply (or powerbank).
The GND of the USB connector is connected to the Pico's GND rail at the bottom of the breadboard. The VCC of the USB connector is only attached to the VCC of the neopixel-ring.

The Pico sends the data for the neopixel-ring over GPIO 4 which is connected to a current delimiting resistor of 470 Ohm.

The only other thing is that I put a capacitor over the USB power rail to stabilise the USB power a bit.

The MicroPython program

import machine, neopixel

ring = neopixel.NeoPixel(machine.Pin(4), 12)

import time

while True:
    for i in range(12):
        ring[i] = (60, 0, 0)
        ring[i-1]=(0,0,0)
        ring.write()
        time.sleep(.2)

As you can see I created the neopixel object with the name ring which is more suitable for my neopixel-ring.

This is just a very simple program that sets the neopixel with the number i to the value (60, 0, 0) which is a dim red. It then sets the previous neopixel (i-1) to black (0,0,0). and then moves on to the next neopixel. np.write() then writes the values to the neopixels so the colors actually change. Forget this command and nothing will happen.

The effect is a red dot looping around the ring.

A hidden command.


There is a hidden command that is nowhere listed in the manual and that is the fill() command.
The fill command fills the complete range of neopixels with a single color.

ring.fill((0,0,60))

In our example this would set the complete ring to a dim blue.

I have really no idea why this command is not listed in the documentation.

That's all for now.
Till next time and have fun

Luc Volders