Driving APA-102 LED Pixels with a Raspberry Pi A+ in C++

Using the BCM2835 Library we can drive APA-102 LED Pixels using the Raspberry Pi SPI port. In this example I’m using a Rpi A+.

Hardware Set up

rpitoapa102

  • Use a single APA102 Pixel as a voltage level booster
  • Connect it to the Rpis +5v and Gnd and to the SPI pins
    • Pin 19-MOSI connects to the DATA pin on the APA102
    • pin 23-CLK Connects to the CLOCK pin on the APA102
  • The outputs of the single driver pixel connect to the rest of your LEDs
  • An external power supply is required to drive your LEDs, remember to tie its GND to the GND of the RPI
  • The example code here drives a strip of 144 pixels

Software Set Up

I’m using the latest Raspbian with g++. This example was originally written in C but ‘m rusty on my C so i made it in C++

Make sure the device tree is enabled

sudo raspi-config
under Advanced Options - enable Device Tree
Reboot

The BCM2835 guide states that it might be a good idea to disable the SPI module but this was not necessary on my board. If its not working disable the module like so:

  1. sudo raspi-config
     under Advanced Options - enable Device Tree
     under Advanced Options - disable SPI
     Reboot.

Next install the library

wget http://www.airspayce.com/mikem/bcm2835/bcm2835-1.50.tar.gz
tar zxvf bcm2835-1.50.tar.gz
cd bcm2835-1.xx
./configure
make
sudo make check
sudo make install

Download the example Driver from my github available at https://github.com/leonyuhanov/rpi_apa102driver

Compile using the following:

g++ -o spi spi.cpp -l bcm2845

Run as root:

sudo ./spi