Skip to content

dlopuch/apa102-java-rpi

Repository files navigation

apa102-java-rpi

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)

Rainbow Fade Demo

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.

Compile Jar from Source

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

Smoketest

You can compile the examples project and get an executable jar that makes the obligatory rainbow fade.

Rainbow Fade Example Wiring

Raspi Hardware

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!

3.3V to 5V Level Shifting: Not Strictly Needed

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.

Raspi Config

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.

Software

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!

See Also

About

Drive APA-102's directly from a Raspi/Odroid using SPI, no extra hardware

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages