NeoPixels are a revolutionary and ultra-popular way to add lights and color to your project. These stranded RGB lights have the controller inside the LED, so you just push the RGB data and the LEDs do all the work for you! They're a perfect match for CircuitPython.
You can drive 300 pixels with brightness control (e.g. setting brightness=0.2
to set it to 20% brightness) and 1000 pixels without (e.g. not setting brightness
at all or setting brightness=1.0
in object creation). That's because to adjust the brightness we have to dynamically re-create the datastream each write.
Here's an example with a lot of different visual effects you can check out.
In the example below, click the Download Project Bundle button below to download the necessary libraries and the code.py file in a zip file. Extract the contents of the zip file, open the directory and then click on the directory that matches the version of CircuitPython you're using and copy the contents of that directory to your CIRCUITPY drive.
Your CIRCUITPY drive should now look similar to the following image:
# SPDX-FileCopyrightText: 2017 John Edgar Park for Adafruit Industries # # SPDX-License-Identifier: MIT # Circuit Playground NeoPixel import time import board from rainbowio import colorwheel import neopixel pixels = neopixel.NeoPixel(board.NEOPIXEL, 10, brightness=0.2, auto_write=False) # choose which demos to play # 1 means play, 0 means don't! color_chase_demo = 1 flash_demo = 1 rainbow_demo = 1 rainbow_cycle_demo = 1 def color_chase(color, wait): for i in range(10): pixels[i] = color time.sleep(wait) pixels.show() time.sleep(0.5) def rainbow_cycle(wait): for j in range(255): for i in range(10): rc_index = (i * 256 // 10) + j * 5 pixels[i] = colorwheel(rc_index & 255) pixels.show() time.sleep(wait) def rainbow(wait): for j in range(255): for i in range(len(pixels)): idx = int(i + j) pixels[i] = colorwheel(idx & 255) pixels.show() time.sleep(wait) 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) OFF = (0, 0, 0) while True: if color_chase_demo: color_chase(RED, 0.1) # Increase the number to slow down the color chase color_chase(YELLOW, 0.1) color_chase(GREEN, 0.1) color_chase(CYAN, 0.1) color_chase(BLUE, 0.1) color_chase(PURPLE, 0.1) color_chase(OFF, 0.1) if flash_demo: pixels.fill(RED) pixels.show() # Increase or decrease to change the speed of the solid color change. time.sleep(1) pixels.fill(GREEN) pixels.show() time.sleep(1) pixels.fill(BLUE) pixels.show() time.sleep(1) pixels.fill(WHITE) pixels.show() time.sleep(1) if rainbow_cycle_demo: rainbow_cycle(0.05) # Increase the number to slow down the rainbow. if rainbow_demo: rainbow(0.05) # Increase the number to slow down the rainbow.
The NeoPixel object's argument list requires the pin you'll use (any pin can be used) and the number of pixels. There are two optional arguments, brightness
(range from 0 off to 1.0 full brightness) and auto_write
. auto_write
defaults to True
when not set. When auto_write
is set to True
, every change is immediately written to the strip of pixels, which is easier to use but way slower. if you set auto_write=False
then you will have to call pixels.show()
when you want to actually write color data out.
You can easily set colors by indexing into the location pixels[n] = (red, green, blue)
. For example, pixels[0] = (100, 0, 0)
will set the first pixel to a medium-brightness red, and pixels[2] = (0, 255, 0)
will set the third pixel to bright green. Then, if you have auto_write=False
don't forget to call pixels.show()
!
For powering the pixels from the board, the 3.3V regulator output can handle about 500mA peak which is about 50 pixels with 'average' use. If you want really bright lights and a lot of pixels, we recommend powering direct from the power source. On the Circuit Playground Express this is the Vout pad - that pad has direct power from USB or BAT (battery), depending on which is higher voltage.
Verify the wiring on your strip or device - plugging into the 'DOUT' side is a common mistake! Wire up NeoPixels only while the Circuit Playground Express is not on, to avoid possible damage!
If the power to the NeoPixels is > 5.5V you may have some difficulty driving some strips, in which case you may need to lower the voltage to 4.5-5V or use a level shifter
Text editor powered by tinymce.