Every programmer in every language, sometime early in their learning, has written a program called "Hello world!" that prints exactly that. The idea behind it is it's an excellent introduction to the language and programming environment. In CircuitPython, our Hello World! is also known as Blink. Every CircuitPython compatible board has a little red LED on it, and you can use a simple program to make it blink. After all, making things blink is great!

Here is what the basic Blink looks like on the Circuit Playground Express. Try loading it on your board to see what happens!

# SPDX-FileCopyrightText: 2017 Kattni Rembor for Adafruit Industries
#
# SPDX-License-Identifier: MIT

import time

from adafruit_circuitplayground.express import cpx

while True:
    cpx.red_led = not cpx.red_led
    time.sleep(0.5)

The little red LED is True when it's on, and False when it's off. This code cycles back and forth between the LED being on for 0.5 seconds and off for 0.5 seconds.

For this project, we will be using the built in NeoPixels. So, for this example, we've written a version of Blink that uses the first NeoPixel instead. However, setting a NeoPixel to a color is not a True or False situation. We're setting it to a color which is done using a tuple in (Red, Green, Blue) format. Cycling back and forth here involves cycling between red which is (255, 0, 0) and off which is (0, 0, 0). So, to do this, we're going to need to do a little math.

Absolute Value

The absolute value of a number can be thought of as that number's distance from zero. For example, the absolute value of 255 is 255. The absolute value of -255 is also 255, because even though it's a negative number, it is still 255 away from zero. To obtain the absolute value of a number using CircuitPython, you use abs(). You can get the absolute value of a single number or you can get the absolute value result of an equation. Try typing different numbers into your REPL to see the results. For example:

-255 and 0 - 255 are both equal to -255, however, as you can see, the absolute value of both is 255.

What does this have to do with blinking our NeoPixel? We're going to use abs() in our Blink code to cycle between 255 and 0. This will allow us to cycle back and forth between red and off. Since we're only changing one number in the (R, G, B) tuple, our NeoPixel Blink code looks like this:

# SPDX-FileCopyrightText: 2017 Kattni Rembor for Adafruit Industries
#
# SPDX-License-Identifier: MIT

import time

from adafruit_circuitplayground.express import cpx

while True:
    cpx.pixels[0] = (abs(cpx.pixels[0][0] - 255), 0, 0)
    time.sleep(0.5)

This cycles between cpx.pixels[0] = (255, 0, 0) and cpx.pixels[0] = (0, 0, 0). Load it on your Circuit Playground Express and see the difference!

Next, we're going to use this example to learn a new concept!

This guide was first published on Feb 20, 2018. It was last updated on Mar 18, 2024.

This page (Blink vs Blink) was last updated on Mar 18, 2024.

Text editor powered by tinymce.