SparkFun Top pHAT Hookup Guide

Pages
Favorited Favorite 1

WS2812B LEDs

Note: This tutorial assumes you are using the latest version of Python 3. If this is your first time using Python or I2C hardware on a Raspberry Pi, please checkout our tutorial on Python Programming with the Raspberry Pi and the Raspberry Pi SPI and I2C Tutorial.

For the addressable LEDs, we will be using Adafruit's neopixel python package. The adafruit-circuitpython-neopixel Python package is hosted on PyPi.

PyPi Installation

On systems that support PyPi installation via pip3 (use pip for Python 2) is simple, using the following commands:

For all users (note: the user must have sudo privileges):

language:bash
sudo pip3 install adafruit-circuitpython-neopixel

For the current user:

language:bash
pip3 install adafruit-circuitpython-neopixel
Note: Based on our experience, for users to access the GPIO functionality, they will need to install the python package using the sudo command.

Python Examples

Before users jump into operating the LEDs, it is recommended that they review the the Read the Docs documentation for the Python package and datasheet for the WS2812B LEDs. Below are a few examples for using the LEDs on the Top pHAT; don't forget that the LEDs are attached to pin 12 and

Note: Based on our experience, users should start with a brightness setting of around 10. The LEDs are fairly bright and can leave users "seeing spots" at full brightness.

Example 1

This is a basic example setting the first, fourth, and fifth LEDs. The array is RGB, with the value indicating the brightness magnitude (0-255).

language:python
# Import Dependencies
import board
import neopixel

# Create the class object
pixels = neopixel.NeoPixel(board.D12, 6, auto_write=False)

# Set Pixel Configuration
pixels[0] = (10, 0, 0)
pixels[3] = (0, 10, 0)
pixels[4] = (0, 0, 10)

# Display Configuration
pixels.show()

Example 2

This is a basic example cycles through the LEDs individually setting them green.

language:python
# Import Dependencies
import board
import neopixel
import time

# Create the class object
pixels = neopixel.NeoPixel(board.D12, 6)

while True:
    for i in range(6):
        # Clear previous LED
        if i == 0:
            pixels[5] = (0,0,0)
        else:
            pixels[i-1]=(0,0,0)

        # Set LED green
        pixels[i]=(0,10,0)

        # Delay
        time.sleep(.2)