In learning any programming language, you often begin with some sort of Hello, World!
program. In CircuitPython, Hello, World! is blinking an LED. Blink is one of the simplest programs in CircuitPython. Despite its simplicity, it shows you many of the basic concepts needed for most CircuitPython programs, and provides a solid basis for more complex projects. Your board has a built-in NeoPixel LED that is great this example.
A NeoPixel is what Adafruit calls the WS281x family of addressable RGB LEDs. The built-in status LED on your board is a NeoPixel! 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!
Time to get blinky!
The NeoPixel LED is located under CLK on the board silk, by the right upper corner of the RP2040 microcontroller chip.
Blinking a NeoPixel LED
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/neopixel_blink_one_pixel/ 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 blink example for built-in NeoPixel LED""" import time import board import neopixel pixel = neopixel.NeoPixel(board.NEOPIXEL, 1) while True: pixel.fill((255, 0, 0)) time.sleep(0.5) pixel.fill((0, 0, 0)) time.sleep(0.5)
The built-in NeoPixel LED begins blinking!
It's important to understand what is going on in this program.
First you import
three modules: time
, board
and 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
.
Finally, you create a while True:
loop. This means all the code inside the loop will repeat indefinitely. Inside the loop, you "fill" the pixel with red using the RGB tuple (255, 0, 0)
. (For more information on how RGB tuples work, see the next section!) Then, you use time.sleep(0.5)
to tell the code to wait half a second before moving on to the next line. The next fills the pixel with "black", which turns it off. Then you use another time.sleep(0.5)
to wait half a second before starting the loop over again.
With only a small update, you can control the blink speed. The blink speed is controlled by the amount of time you tell the code to wait before moving on using time.sleep()
. The example uses 0.5
, which is one half of one second. Try increasing or decreasing these values to see how the blinking changes.
That's all there is to blinking a built-in NeoPixel LED using CircuitPython!
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)
Text editor powered by tinymce.