Qwiic LED Stick - APA102C Hookup Guide

Pages
Contributors: El Duderino, MAKIN-STUFF
Favorited Favorite 1

Python Examples

The Qwiic LED Stick Python package includes eleven examples ranging from basics to get you started with the board to some some nifty demos. In this section we'll take a closer look at a few of the examples and what they do.

Just like in the Arduino library, each example creates the my_stick object and includes a begin statement that initializes the Qwiic LED Stick on the I2C bus and freezes the code if that fails:

language:python
my_stick = qwiic_led_stick.QwiicLEDStick()

if my_stick.begin() == False:
    print("\nThe Qwiic LED Stick isn't connected to the sytsem. Please check your connection", \
        file=sys.stderr)
    return
print("\nLED Stick ready!")

If your code freezes here, double check the I2C bus is enabled on your Pi and your Qwiic LED Stick is connected properly. Adjusting the address either via the ADR jumper or changing it via Example 10 - Change Address requires entering the new address in the object creation here:

language:python
my_stick = qwiic_led_stick.QwiicLEDStick(NEW ADDRESS HERE)

Example 1 - Blink

The first example demonstrates a standard "Blink" for all ten LEDs. After initializing the LED Stick on the bus, the code sets the brightness to ~50% so we don't burn our retinas looking at super-bright white LEDs.

After that, the code sets the color of the LEDs to all white and blinks them on and off every second:

language:python
my_stick.set_all_LED_color(50, 50, 50)
time.sleep(1)
my_stick.LED_off()
time.sleep(1)

Example 3 - Single Pixel 2

This example creates three lists of ten to set the Red, Green and Blue values for each LED on the stick and then uses the set_all_LED_unique_color() function to set the color of each individual LED.

Try playing around with the values in each list to switch the colors of the LEDs:

language:python
red_list = [214, 78, 183, 198, 59, 134, 15, 209, 219, 186]
green_list = [59, 216, 170, 21, 114, 63, 226, 92, 155, 175]
blue_list = [214, 147, 25, 124, 153, 163, 188, 33, 175, 221]

Example 4 - Set Brightness

The last example we'll cover here demonstrates how to set the brightness of the LED Stick. The first example uses the function in its basic form so you may be familiar with it already. This example sets the LEDs to a rainbow and steps through each valid brightness setting to give you a good idea of what each color looks like at each brightness level to figure out what works best for your project.

language:python
for i in range(0, 32):
    my_stick.set_all_LED_brightness(i)

    print("\nBrightness level: " + str(i))
    time.sleep(1)

The other examples demonstrate some neat lighting displays along with demonstrations of how to change the I2C address, adjust the total number of LEDs controlled to add other LED Sticks or LED strips.