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


13.1. Import module

import neopixel

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

import neopixel

np = neopixel.NeoPixel(pin0, 8)

13.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).

np[0] = (255, 0, 0)  # first pixel red
np[-1] = (0, 255, 0)  # last pixel yellow
np.show()  # display updated values

13.4. Show

neopixel.show()

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

13.5. Get colour

Get the colour of a specific pixel just reference it.

print(np[0])

13.6. Fill V2

neopixel.fill(colour)

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

13.7. Clear

neopixel.clear()

Clear all the pixels.

13.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)