The Circuit Playground Express and Bluefruit have ten RGB NeoPixel LEDs built in. Though the images are of the Circuit Playground Express, the LEDs are in the same location on the Bluefruit. They're located in a ring around the board, just inside the outer ring of alligator-clip-friendly pads. RGB means red, green and blue, and that means you can create any color of the rainbow with these LEDs!
LED colors are set using a combination of red, green, and blue, in the form of an (R, G, B) tuple. A tuple is typically a group of numbers. Each member of the RGB 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 the 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.
We won't make you wait any longer. Let's get started!
Add the following code to your code.py. Remember if you need help with this, check here.
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT """This example lights up all the NeoPixel LEDs red.""" from adafruit_circuitplayground import cp while True: cp.pixels.fill((50, 0, 0))
Red lights!
First we import cp
.
Inside our loop, we have cp.pixels.fill((50, 0, 0))
which turns on all the pixels red at approximately 20% brightness. Remember, the maximum level of red is 255. That's really bright! So we've set it to a lower level of red so that it's not so blinding by setting it to 50
. The other two are 0, so there's no green or blue added into the mix yet. That's all there is to it!
Now, try changing the numbers to other values. For example, try cp.pixels.fill((50, 50, 0))
. See what happens!
One Pixel, Two Pixel, Red Pixel, Blue Pixel!
We turned on all the pixels to the same color. But what if you want to control each one individually? We can do that!
Add the following code to your code.py:
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT """This example lights up the first NeoPixel red.""" from adafruit_circuitplayground import cp cp.pixels.brightness = 0.3 while True: cp.pixels[0] = (255, 0, 0)
Now only the first pixel is red!
Let's look at the code.
First we import cp
.
Next, we have a new line: cp.pixels.brightness = 0.3
. Remember, we controlled brightness by using a lower number in the color tuple in the first piece of code. It's also possible to control brightness separately using cp.pixels.brightness
. The brightness is set by a number between 0 and 1 that represents a percentage. So, when we set it to 0.3
, we are setting it to 30% brightness.
Inside our loop, we have cp.pixels[0] = (255, 0, 0)
. Since we've set the brightness separately from the color, we are able to set the color to maximum red, or 255.
Notice we've set pixel number 0
, but it's turned on the first pixel. This is because CircuitPython begins counting with 0. So the first of something numbered in CircuitPython will always be 0.
Let's try setting the second pixel to blue. Remember, the second pixel will be pixel number 1. Add the following to your code.py.
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT """This example lights up the first and second NeoPixel, red and blue respectively.""" from adafruit_circuitplayground import cp cp.pixels.brightness = 0.3 while True: cp.pixels[0] = (255, 0, 0) cp.pixels[1] = (0, 0, 255)
Now your second pixel is blue.
That's all there is to it! You can keep adding more pixels up through 9 to set all of them different colors.
Give it a try!
Page last edited January 22, 2025
Text editor powered by tinymce.