tinue / apa102-pi

Pure Python library to drive APA102 LED stripes; Use with Raspberry Pi.
GNU General Public License v2.0
201 stars 71 forks source link
apa102 dotstar-leds led-strips python raspberry-pi spi

apa102-pi

Introduction

Apa102-pi is a pure Python library to drive APA102 and SK9822 type LED strands. It is supposed to work on a Raspberry Pi, and is not tested on any other platform.

Be advised that I consider this library to be "done", and don't plan to develop more functionality. I still monitor the Github page for issues or pull requests, and will release minor updates from time to time.

APA102 LEDs are typically 5050 type LEDs with an additional driver chip per LED. The driver chip takes care of receiving the desired colour via its input lines, and then holding this colour until a new command arrives.

Depending on the LEDs vendor, they might be called "APA102", "SK9822", "Superled", or "DotStar". They must not be confused with the three-wire WS2812 LEDs, also known as "NeoPixel".

The really nice part about the driver chip is this: Once it has received its own colour command, it forwards any further colour commands from its input to its output lines. This allows to easily chain multiple LEDs together. Colour command #1 lights the first LED, command #2 the next LED, and so forth. Sending for example 300 colour commands does light an entire 5 Meter, 60 LEDs per Meter strip.

Some APA102 pictures are available here

Purpose

The library takes care of the details on sending colour commands. It is supposed to be educational, and is therefore written in Python. The library is fast enough to produce nice colour effects on a 300 LED strand, even though it is running via the Python interpreter. However, if you need something really fast, e.g. to drive a small "display" based on APA102 LEDs with 15 frames per second, then you have to look elsewhere.

Prerequisites

For a permanent installation, a 10$ Raspberry Pi Zero W can be dedicated to the task of driving the LEDs. The connector to the LED stripe would be soldered directly to the correct ports on the board. For development purposes, a Raspberry Pi 4 Model B is a better choice due to its greater speed. Even the 1GB model is more than enough for this purpose.

Wiring

The Raspberry Pi is a 3.3 volt device, and the APA102 LEDs are 5 volt devices. Therefore, it is possible that the first LED driver chip does not properly detect the 3.3 volt SPI signal from the Raspberry Pi.
The first chip will amplify the signal to 5 volts before passing it on, so it is really only the first chip that must detect the 3.3 volt signal.
To avoid this risk, use a 74AHCT125 or 74AHC125 level shifter for both the clock and the MOSI signal. In my limited testing with four different strips from various Chinese sources I had no issues without a level shifter, but your experience might be different.

Without a level shifter, the wiring is very simple:

A note about "chip select": The Raspberry Pi's SPI0 bus has two hardware Chip Select pins: CE0 and CE1. They correspond to the devices /dev/spidev0.0 and /dev/spidev0.1. A typical SPI device has one Chip Select input line. So, on a stock Raspberry Pi one can connect two SPI devices: Both share SCLK, MOSI and MISO, and each one uses its own Chip Select. You might be wondering where the Chip Select input line is on an LED strip. Answer: There is none. You therefore can't disable the Strip from reading data on SCLK/MOSI, at least not without additional hardware. For example, you can wire the chip select GPIO of the Raspberry Pi to the level shifter "output-enable" pin.

The Adafruit library does not use or support the hardware chip select pins of the Raspberry Pi. Instead, any free GPIO pin can be used. Because this is multiplexed in software, it is very slow. The feature supports e.g. sensors, where you have a lot of them (more than two), and each sensor only sends or receives a limited amount of data.
The apa102-pi library attempts to use this software multiplexing if a CE value is passed on driver initialization. The strip will update a lot slower if this is used. It is still a bit faster than bitbang, though. Please note that I have not tested this feature, because I never assembled the hardware required for this.

The LED strip uses a lot of power (roughly 20mA per LED, i.e. 60mA for one bright white dot). If you try to power the LEDs from the Raspberry Pi 5V output, you will most likely immediately kill the Raspberry! Therefore, I recommend not to connect the power line of the LED with the Raspberry. To be on the safe side, use a separate USB power supply for the Raspberry, and a strong 5V supply for the LEDs. If you use a level shifter, power it from the 5V power supply as well.

Having said this, you can power the Raspberry from the same power supply as the LED strips (instead of using an extra USB power supply). If you decide to do this, make sure to never plug a USB power supply to the Raspberry Pi, or you risk that the LEDs try to take power through the Raspberry.

All combined, this is my extremely low-tech wiring diagram:

Wiring Diagram

And here it is, the finished contraption running a "rainbow" program:

Raspberry Pi Zero driving APA102 LEDs

This is a Raspberry Pi Zero W with a Phat Beat amplifier on top. The amplifier's "VU meter" is simply a bunch of APA 102 LEDs; They show the "Rainbow" color scheme:

Raspberry Pi Zero W with Phat Beat

Plugged into the USB port is a WLAN stick (nowadays I use a Raspberry Pi Zero W, of course). This way I can reprogram the light show from my desk, even if the strips sit outside as a Christmas light. Compare this to an Arduino/WS2812 based installation: To reprogram one has to take the Arduino inside, or a laptop outside.

This is a Raspberry Pi 4 with a 3D RGB Xmas Tree from Pi Hut:

Raspberry Pi 4 with Xmas tree

Quick Raspberry Pi setup

Because the Raspberry Pi Zero runs headless, I recommend using the Raspberry Pi OS Lite image. This image only contains the bare minimum of packages, and some packages have be added manually.

I suggest to use the Raspberry Pi Imager for installation. This software allows to setup hostname, WiFi and enable SSH without further work. The Raspberry Pi will run headless from the start, and there is no need to ever connect a display or a keyboard.

After installation, ssh into the freshly setup Raspberry Pi and install additional packages. Also, enable SPI:

Use the APA102 project as a library

The library was originally built as an educational piece of software. It shows how the protocol for APA102 LEDs works. Most of this is explained in the form of comments in the source code. If you are interested in this, then follow up with the chapter after this one. If all you need is the library itself for your own projects, then this chapter is enough to get you started.

Install the library like this: sudo pip3 install apa102-pi. This will install the library, and its dependencies for all users.

Note: If you want to install the library into a virtual env, you must create the venv with the option --system-site-packages. This is necessary to get access to the shared RPi.GPIO library.

To verify the installation, download the test script from Github: curl https://raw.githubusercontent.com/tinue/apa102-pi/main/runcolorcycle.py -o runcolorcycle.py. To run, type python3 ./runcolorcycle.py.

Full installation into a Python virtual environment

To retrieve the full library including source code, and use a virtual env in the process, this is what you need to do:

Full installation using Ansible

If you know Ansible, you can use the role apa102_install from directory AnsiblePlaybook. This role installs everything that is outlined in the chapter above automatically. Once the role has executed, you can ssh into the Raspberry Pi, activate the venv and run the sample program.

Troubleshooting

Strip remains dark

The number one reason for this problem is that MOSI and SCLK are swapped. You can't rely on the colors of the wires, they seem to change with every batch of APA102 strips that are produced. To resolve, swap the two data wires and try again.
If it still does not work, then you might need a level shifter. Before you do this, you can try to lower the supply voltage for the strip. According to the APA102 data sheets, a "one" signal is detected as a percentage of the supply voltage: The lower the supply voltage, the higher the chance that 3.3 Volts is still detected as a "one".

Flicker

Sometimes the end of large strips flickers. It seems that there is a correlation amongst three variables:

It turns out that you can only have two out of three: On a long, bright strip you will have to lower the bus speed significantly.
Check the apa102.py driver: Default is 8MHz (BUS_SPEED_HZ = 8000000). You may have to go as low as 1.5MHz, i.e. BUS_SPEED_HZ = 1500000. This means that all light programs with lots of updates and zero wait (e.g. rainbow) will run much slower.

Brightness

There is a default global brightness value in the driver itself (apa102.py, line 90). Of this parameter, the LED controller chip interprets only the first 5 bits, snd ignores the rest. Therefore, everything >= 31 is 100% brightness. The value is set to 1/8th of the maximum (i.e. 8), and can be changed either by patching apa102.py, or by setting the value from your application. Check sample.py to see how this is done.

Release history