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 built-in NeoPixel LEDs that are great this example.

A NeoPixel is what Adafruit calls the WS281x family of addressable RGB LEDs. The built-in LEDs on your board are NeoPixels! Each NeoPixel 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, 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 there are NeoPixels built in to your microcontroller board!

Time to get blinky!

NeoPixel Location

The four NeoPixels on Neo Trinkey (indicated by the green box below) are located towards the opposite side of the board from the USB connector, arranged in a square.

Blinking NeoPixel LEDs

All the necessary modules and libraries for this example are included with CircuitPython for your board, so you do not need to load any separate files.

Save the following as code.py on your CIRCUITPY drive.

You do not need to download the Project Bundle - all the necessary libraries are included in CircuitPython for this board!
"""CircuitPython NeoPixel Blink example - blinking the built-in NeoPixels."""
import time
import board
import neopixel

pixels = neopixel.NeoPixel(board.NEOPIXEL, 4)

while True:
    pixels.fill((255, 0, 0))
    time.sleep(0.5)
    pixels.fill((0, 0, 0))
    time.sleep(0.5)

The built-in NeoPixel LEDs begin blinking!

This is the Neo Trinkey, but your board should look similar!

It's important to understand what is going on in this program.

First you import two modules, time and board, and one library, neopixel. This makes these modules and libraries available for use in your code.

Next, you set up the NeoPixel LEDs. 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 pixels.

Finally, you create a while True: loop. This means all the code inside the loop will repeat indefinitely. Inside the loop, you "fill" the pixels 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 pixels 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 the built-in NeoPixel LEDs 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 10, 2021. It was last updated on 2021-04-14 11:58:11 -0400.

This page (NeoPixel Blink) was last updated on Nov 28, 2021.

Text editor powered by tinymce.