Adafruit CircuitPython NeoPixel¶
Higher level NeoPixel driver that presents the strip as a sequence. This is a
supercharged version of the original MicroPython driver. Its now more like a
normal Python sequence and features slice support, repr
and len
support.
Colors are stored as tuples by default. However, you can also use int hex syntax
to set values similar to colors on the web. For example, 0x100000
(#100000
on the web) is equivalent to (0x10, 0, 0)
.
Note
The int hex API represents the brightness of the white pixel when present by setting the RGB channels to identical values. For example, full white is 0xffffff but is actually (0, 0, 0, 0xff) in the tuple syntax. Setting a pixel value with an int will use the white pixel if the RGB channels are identical. For full, independent, control of each color component use the tuple syntax.
Dependencies¶
This driver depends on:
Please ensure all dependencies are available on the CircuitPython filesystem. This is easily achieved by downloading the Adafruit library and driver bundle.
Usage Example¶
This example demonstrates the library with the single built-in NeoPixel on the Feather M0 Express and Metro M0 Express.
import board
import neopixel
pixels = neopixel.NeoPixel(board.NEOPIXEL, 1)
pixels[0] = (10, 0, 0)
This example demonstrates the library with the ten built-in NeoPixels on the
Circuit Playground Express. It turns
off auto_write
so that all pixels are updated at once when the show
method is called.
import board
import neopixel
pixels = neopixel.NeoPixel(board.NEOPIXEL, 10, auto_write=False)
pixels[0] = (10, 0, 0)
pixels[9] = (0, 10, 0)
pixels.show()
This example demonstrates using a single NeoPixel tied to a GPIO pin and with
a pixel_order
to specify the color channel order. Note that bpp
does not
need to be specified as it is computed from the supplied pixel_order
.
import board
import neopixel
pixel = neopixel.NeoPixel(board.D0, 1, pixel_order=neopixel.RGBW)
pixel[0] = (30, 0, 20, 10)
Contributing¶
Contributions are welcome! Please read our Code of Conduct before contributing to help this project stay welcoming.
Building locally¶
To build this library locally you’ll need to install the circuitpython-build-tools package.
python3 -m venv .env
source .env/bin/activate
pip install circuitpython-build-tools
Once installed, make sure you are in the virtual environment:
source .env/bin/activate
Then run the build:
circuitpython-build-bundles --filename_prefix adafruit-circuitpython-neopixel --library_location .
Sphinx documentation¶
Sphinx is used to build the documentation based on rST files and comments in the code. First, install dependencies (feel free to reuse the virtual environment from above):
python3 -m venv .env
source .env/bin/activate
pip install Sphinx sphinx-rtd-theme
Now, once you have the virtual environment activated:
cd docs
sphinx-build -E -W -b html . _build/html
This will output the documentation to docs/_build/html
. Open the index.html in your browser to
view them. It will also (due to -W) error out on any warning like Travis will. This is a good way to
locally verify it will pass.
Table of Contents¶
Simple test¶
Ensure your device works with this simple test.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | import time
import board
import neopixel
# On CircuitPlayground Express, and boards with built in status NeoPixel -> board.NEOPIXEL
# Otherwise choose an open pin connected to the Data In of the NeoPixel strip, i.e. board.D1
pixel_pin = board.NEOPIXEL
# The number of NeoPixels
num_pixels = 10
# The order of the pixel colors - RGB or GRB. Some NeoPixels have red and green reversed!
# For RGBW NeoPixels, simply change the ORDER to RGBW or GRBW.
ORDER = neopixel.GRB
pixels = neopixel.NeoPixel(pixel_pin, num_pixels, brightness=0.2, auto_write=False,
pixel_order=ORDER)
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:
r = g = b = 0
elif pos < 85:
r = int(pos * 3)
g = int(255 - pos*3)
b = 0
elif pos < 170:
pos -= 85
r = int(255 - pos*3)
g = 0
b = int(pos*3)
else:
pos -= 170
r = 0
g = int(pos*3)
b = int(255 - pos*3)
return (r, g, b) if ORDER == neopixel.RGB or ORDER == neopixel.GRB else (r, g, b, 0)
def rainbow_cycle(wait):
for j in range(255):
for i in range(num_pixels):
pixel_index = (i * 256 // num_pixels) + j
pixels[i] = wheel(pixel_index & 255)
pixels.show()
time.sleep(wait)
while True:
# Comment this line out if you have RGBW/GRBW NeoPixels
pixels.fill((255, 0, 0))
# Uncomment this line if you have RGBW/GRBW NeoPixels
# pixels.fill((255, 0, 0, 0))
pixels.show()
time.sleep(1)
# Comment this line out if you have RGBW/GRBW NeoPixels
pixels.fill((0, 255, 0))
# Uncomment this line if you have RGBW/GRBW NeoPixels
# pixels.fill((0, 255, 0, 0))
pixels.show()
time.sleep(1)
# Comment this line out if you have RGBW/GRBW NeoPixels
pixels.fill((0, 0, 255))
# Uncomment this line if you have RGBW/GRBW NeoPixels
# pixels.fill((0, 0, 255, 0))
pixels.show()
time.sleep(1)
rainbow_cycle(0.001) # rainbow cycle with 1ms delay per step
|
neopixel
- NeoPixel strip driver¶
- Author(s): Damien P. George & Scott Shawcroft
-
neopixel.
GRB
= (1, 0, 2)¶ Green Red Blue
-
neopixel.
GRBW
= (1, 0, 2, 3)¶ Green Red Blue White
-
class
neopixel.
NeoPixel
(pin, n, *, bpp=3, brightness=1.0, auto_write=True, pixel_order=None)[source]¶ A sequence of neopixels.
Parameters: - pin (Pin) – The pin to output neopixel data on.
- n (int) – The number of neopixels in the chain
- bpp (int) – Bytes per pixel. 3 for RGB and 4 for RGBW pixels.
- brightness (float) – Brightness of the pixels between 0.0 and 1.0 where 1.0 is full brightness
- auto_write (bool) – True if the neopixels should immediately change when set. If False,
show
must be called explicitly. - pixel_order (tuple) – Set the pixel color channel order. GRBW is set by default.
Example for Circuit Playground Express:
import neopixel from board import * RED = 0x100000 # (0x10, 0, 0) also works pixels = neopixel.NeoPixel(NEOPIXEL, 10) for i in range(len(pixels)): pixels[i] = RED
Example for Circuit Playground Express setting every other pixel red using a slice:
import neopixel from board import * import time RED = 0x100000 # (0x10, 0, 0) also works # Using ``with`` ensures pixels are cleared after we're done. with neopixel.NeoPixel(NEOPIXEL, 10) as pixels: pixels[::2] = [RED] * (len(pixels) // 2) time.sleep(2)
-
brightness
¶ Overall brightness of the pixel
-
neopixel.
RGB
= (0, 1, 2)¶ Red Green Blue
-
neopixel.
RGBW
= (0, 1, 2, 3)¶ Red Green Blue White