Your board has a built-in RGB NeoPixel status LED. You can use CircuitPython code to control the color and brightness of this LED. It is also used to indicate the bootloader status and errors in your CircuitPython code.
A NeoPixel is what Adafruit calls the WS281x family of addressable RGB LEDs. It contains three LEDs - a red one, a green one and a blue one - along side a driver chip in a tiny package controlled by a single pin. They can be used individually (as in the built-in LED on your board), or chained together in strips or other creative form factors. NeoPixels do not light up on their own; they require a microcontroller. So, it's super convenient that the NeoPixel is built in to your microcontroller board!
This page will cover using CircuitPython to control the status RGB NeoPixel built into your microcontroller. You'll learn how to change the color and brightness, and how to make a rainbow. Time to get started!
The NeoPixel LED (highlighted in green), labeled Neo on the silk, is located near the top left corner of the Feather.
NeoPixel Color and Brightness
To use with CircuitPython, you need to first install a few libraries, into the lib folder on your CIRCUITPY drive. Then you need to update code.py with the example script.
Thankfully, we can do this in one go. 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 CircuitPython_Templates/status_led_one_neopixel_rgb/ 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: 2021 Kattni Rembor for Adafruit Industries # SPDX-License-Identifier: MIT """CircuitPython status NeoPixel red, green, blue example.""" import time import board import neopixel pixel = neopixel.NeoPixel(board.NEOPIXEL, 1) pixel.brightness = 0.3 while True: pixel.fill((255, 0, 0)) time.sleep(0.5) pixel.fill((0, 255, 0)) time.sleep(0.5) pixel.fill((0, 0, 255)) time.sleep(0.5)
The built-in NeoPixel begins blinking red, then green, then blue, and repeats!
First you import two modules, time
and board
, and one library, neopixel
. This makes these modules and libraries available for use in your code. The first two are modules built-in to CircuitPython, so you don't need to download anything to use those. The neopixel
library is separate, which is why you needed to install it before getting started.
Next, you set up the NeoPixel LED. To interact with hardware in CircuitPython, your code must let the board know where to look for the hardware and what to do with it. So, you create a neopixel.NeoPixel()
object, provide it the NeoPixel LED pin using the board
module, and tell it the number of LEDs. You save this object to the variable pixel
.
Then, you set the NeoPixel brightness using the brightness
attribute. brightness
expects float between 0
and 1.0
. A float is essentially a number with a decimal in it. The brightness value represents a percentage of maximum brightness; 0
is 0% and 1.0
is 100%. Therefore, setting pixel.brightness = 0.3
sets the brightness to 30%. The default brightness, which is to say the brightness if you don't explicitly set it, is 1.0
. The default is really bright! That is why there is an option available to easily change the brightness.
Inside the loop, you turn the NeoPixel red for 0.5 seconds, green for 0.5 seconds, and blue for 0.5 seconds.
To turn the NeoPixel red, you "fill" it with an RGB value. Check out the section below for details on RGB colors. The RGB value for red is (255, 0, 0)
. Note that the RGB value includes the parentheses. The fill()
attribute expects the full RGB value including those parentheses. That is why there are two pairs of parentheses in the code.
You can change the RGB values to change the colors that the NeoPixel cycles through. Check out the list below for some examples. You can make any color of the rainbow with the right RGB value combination!
That's all there is to changing the color and setting the brightness of the built-in NeoPixel LED!
RGB LED Colors
RGB LED colors are set using a combination of red, green, and blue, in the form of an (R, G, B) tuple. Each member of the tuple is set to a number between 0 and 255 that determines the amount of each color present. Red, green and blue in different combinations can create all the colors in the rainbow! So, for example, to set an LED to red, the tuple would be (255, 0, 0)
, which has the maximum level of red, and no green or blue. Green would be (0, 255, 0)
, etc. For the colors between, you set a combination, such as cyan which is (0, 255, 255)
, with equal amounts of green and blue. If you increase all values to the same level, you get white! If you decrease all the values to 0, you turn the LED off.
Common colors include:
- red:
(255, 0, 0)
- green:
(0, 255, 0)
- blue:
(0, 0, 255)
- cyan:
(0, 255, 255)
- purple:
(255, 0, 255)
- yellow:
(255, 255, 0)
- white:
(255, 255, 255)
- black (off):
(0, 0, 0)
NeoPixel Rainbow
You should have already installed the library necessary to use the built-in NeoPixel LED. If not, follow the steps at the beginning of the NeoPixel Color and Brightness section to install it.
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 CircuitPython_Templates/status_led_one_neopixel_rainbow/ 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: 2021 Kattni Rembor for Adafruit Industries # SPDX-License-Identifier: MIT """CircuitPython status NeoPixel rainbow example.""" import time import board from rainbowio import colorwheel import neopixel pixel = neopixel.NeoPixel(board.NEOPIXEL, 1) pixel.brightness = 0.3 def rainbow(delay): for color_value in range(255): pixel[0] = colorwheel(color_value) time.sleep(delay) while True: rainbow(0.02)
The NeoPixel displays a rainbow cycle!
This example builds on the previous example.
First, you import the same three modules and libraries. In addition to those, you import colorwheel
.
The NeoPixel hardware setup and brightness setting are the same.
Next, you have the rainbow()
helper function. This helper displays the rainbow cycle. It expects a delay
in seconds. The higher the number of seconds provided for delay
, the slower the rainbow will cycle. The helper cycles through the values of the color wheel to create a rainbow of colors.
Inside the loop, you call the rainbow helper with a 0.2 second delay, by including rainbow(0.2)
.
That's all there is to making rainbows using the built-in NeoPixel LED!
Text editor powered by tinymce.