Raspi/Odroid SPI driver lib for APA-102's, using Pi4j to do all the hard SPI control stuff.
Control APA-102 LED strips directly from a Raspi without any additional hardware -- no pixelpushers, no fadecandy, etc. (level shifters to bring the 3.3V raspi up to 5V APA102 signalling are recommended, but not necessary for small runs).
For use with Java-based LED art like Processing or LX (example LXOutput)
To use:
Grab dist/apa102-java-rpi-lib-1.0-all.jar
and stick it in your project.
Apa102Output.initSpi();
// Could also init with non-defaults using #initSpi(SpiChannel spiChannel, int spiSpeed, SpiMode spiMode)
// Default speed is 7.8 Mhz
Apa102Output strip = new Apa102Output(NUM_LEDS);
byte[] ledRGBs = new byte[ NUM_LEDS * 3 ];
while (true) {
// <fill in your ledRGBs buffer with your pattern... eg examples/RainbowStrip.java>
strip.writeStrip(ledRGBs);
}
See examples/RainbowStrip for a full example.
Note that byte
's in java are only signed -- APA102's are controlled with values 0-255, but in java bytes are valued
-128 - 127. This can make working with bytes unintuitive to the uninitiated.
See examples/BytesInJava
for a primer.
This project is configured to make a fatjar so all dependencies are included in a single executable.
To compile the library
module and get a jar to use in your project:
./gradlew library:shadowJar
cp library/build/libs/apa102-java-rpi-lib-1.0-all.jar my/destination/project
You can compile the examples
project and get an executable jar that makes the obligatory rainbow fade.
First, wire up your APA-102 and raspi like so:
APA-102 | Connection | Notes |
---|---|---|
+ |
power supply +5V |
NOT your raspi! (Your raspi can't source that much current, give your APA-102 a dedicated +5 source) |
CLK |
raspi SCLK |
|
DAT |
raspi MOSI |
|
GND |
power supply GND AND raspi GND |
make a common ground, even if connecting to different ports on the same battery pack! |
Note: The Raspberry Pi is a 3.3V device, while the APA-102's are 5V devices. You CAN connect the 3.3V signal lines directly to the 5V APA's, and it will generally work if the wire or strip runs aren't so long that they cause enough of a voltage drop that the 3.3V signal drops below the 5V signaling threshold.
The real way of doing this though is to use a level shifter like a 74AHCT125 or 74AHC125 for both the SCLK
and MOSI
lines. For a small piece of art, you can drive the signal directly.
In a pinch, you can use a single APA-102 "dummy pixel"/repeater close to the raspi as a good-enough level shifter (also called a "booster pixel").
The number of 3.3V-drivable pixels can also be affected by the SPI clock frequency. By default the library initializes to 7.8Mhz. If you observe flickering down your line, you can try initializing SPI at 4Mhz or lower.
Make sure you have SPI enabled on your raspi:
sudo raspi-config
- "Interfacing Options" > "SPI" > "Enable SPI"
- Exit and reboot
Since this is java, you will also need a JDK.
Grab the dist/apa102-java-rpi-examples-1.0-all.jar
jar, or compile the examples/
project from source:
./gradlew examples:shadowJar
Get the jar onto your raspi:
scp examples/build/libs/apa102-java-rpi-examples-1.0-all.jar root@raspi.local:~/
And run it!
root@raspi.local $ sudo java -jar apa102-java-rpi-examples-1.0-all.jar
(Note you need to run it as sudo to get access to SPI driver).
If all is well, you should be seeing some rainbow blinky-blinks!
- APA Signaling Protocol:
- Previous work / other libs:
- Python driver and lots of good documentation: tinue/APA102_Pi
- C++ driver: leonyuhanov/rpi_apa102driver