14. neopixel module

The neopixel module lets you use NeoPixel (WS2812) individually addressable RGB and RGBW LED strips with the microbit.

See: https://microbit-micropython.readthedocs.io/en/v2-docs/neopixel.html


14.1. Import module

import neopixel

14.2. Initialise neopixels

class neopixel.NeoPixel(pin, n, bpp=3)

Initialise a new strip of n number of neopixel LEDs controlled via pin pin.

Parameters:
  • pin – The pin to which the NeoPixel strip is connected.

  • n – The number of NeoPixel LEDs in the strip.

  • bpp – The number of bytes per pixel. Default is 3 for RGB and GRB, and 4 for RGBW.

from microbit import *
import neopixel

np = neopixel.NeoPixel(pin0, 8)

14.3. set neopixel colours

Each pixel is addressed by a position (starting from 0).
Neopixels are given RGB (red, green, blue) values between 0-255 as a tuple.
For example, in RGB, (255,255,255) is white.
For RGBW, (red, green, blue, white), (255,255,255,0) or (0,0,0,255) is white.

Writing the colour doesn’t update the display (use show() for that).

from microbit import *
import neopixel

np = neopixel.NeoPixel(pin0, 8)
np[0] = (255, 0, 0)  # first pixel red
np[-1] = (0, 255, 0)  # last pixel green
np.show()  # display updated values

14.4. Show

neopixel.show()

Show the pixels. Must be called for any updates to become visible.

The pixels light up red one at a time because show() is called after each one.
from microbit import *
import neopixel

np = neopixel.NeoPixel(pin0, 8)

for i in range(len(np)):
    np[i] = (40, 0, 0)
    np.show()
    sleep(200)

14.5. Get colour

Get the colour of a specific pixel just reference it.

from microbit import *
import neopixel

np = neopixel.NeoPixel(pin0, 8)
np[0] = (255, 0, 0)
# print(np[0])
display.scroll(str(np[0]), delay=80)

14.6. Fill V2

neopixel.fill(colour)

Colour all pixels a given value. e.g. np.fill((0, 0, 255))

All the pixels change between blue and green.
from microbit import *
import neopixel

np = neopixel.NeoPixel(pin0, 8)

while True:
    np.fill((0, 0, 40))
    np.show()
    sleep(500)
    np.fill((0, 40, 0))
    np.show()
    sleep(500)

14.7. Clear

neopixel.clear()

Clear all the pixels.

The pixels turn white, then turn off after 1 second.
from microbit import *
import neopixel

np = neopixel.NeoPixel(pin0, 8)

np.fill((30, 30, 30))
np.show()
sleep(1000)
np.clear()

14.8. Random pixel colours

"""
Repeatedly displays random colours on the LED Neopixel strip on pin0 with a length of 8 pixels
"""
from microbit import *
import neopixel
from random import randint

# Setup the Neopixel strip on pin0 with a length of 8 pixels
np = neopixel.NeoPixel(pin0, 8)

while True:
    # Iterate over each LED in the strip
    for pixel_id in range(0, len(np)):
        red = randint(0, 60)
        green = randint(0, 60)
        blue = randint(0, 60)

        # Assign the current LED a random red, green and blue value between 0 and 60
        np[pixel_id] = (red, green, blue)

        # Display the current pixel data on the Neopixel strip
        np.show()
        sleep(100)