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!

NeoPixel Location

The built-in NeoPixel, highlighted in red, is labeled NEO on the QT Py RP2040 board. It is located next to the reset button, inside the SCK and MI pin labels on the silk.

Blinking a NeoPixel LED

To use the built-in NeoPixel on your board, you need to first install the NeoPixel library into the lib folder on your CIRCUITPY drive.

Then you need to update code.py.

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, and copy the entire lib folder and the code.py file to your CIRCUITPY drive.

"""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)

Your CIRCUITPY drive should resemble the image.

You should have in / of the CIRCUITPY drive:

  • code.py

And in the lib folder on your CIRCUITPY drive:

  • adafruit_pypixelbuf.mpy
  • neopixel.mpy

The built-in NeoPixel LED begins blinking!

If your NeoPixel does not start blinking, make sure you've copied all the necessary files and folders to the CIRCUITPY drive!

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 (RG, 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)

This guide was first published on Apr 21, 2021. It was last updated on 2021-05-04 15:35:03 -0400.

This page (Blink) was last updated on May 08, 2021.

Text editor powered by tinymce.