Android Things with Raspberry Pi: Workshop Basics Part 2 (The Internet of Bling with the APA 102)

ANDROID-THINGSIn the previous post, we talked about the basics of getting Android Things running on a device and connecting to it on a Pi. Now that it’s working, let’s do the traditional “Hello World” with some lights. We could use a standard LED, but that’s pretty plain vanilla, so, instead, we’re going to use APA 102 LEDs. Your first question might be: “Why the APA 102?”14015-03

  1. Each LED actually consists of three individual LEDs, red, green and blue, which can be used to created a large number of colors.
  2. They are controlled via the SPI protocol, which allows you to use 4 wires to control hundreds of LEDs.
  3. At the time of writing, it is the only RGB LED of this type supported by Android Things.

Electricity Basics

LEDs, especially in multiples, can easily consume a reasonable amount of electricity, so now might be a good time to review some basics. USB ports run on 5V DC. If you have multiple USB-based chargers, however, you might notice that different ones have different associated wattages/amp ratings. For the sake of this discussion, we’re going to talk about things in terms of Watts, which can always be calculated if you have an Amps value (by multiplying voltage by amps.) Most pre-USB-C smartphones come with chargers rated at 1-1.5 Amps. Tablets such as the iPad are equipped with chargers rated at 2.4 Amps.

The Raspberry Pi 3 is a fairly power-hungry device for which the manufacturer recommends 2.5 Amp USB power supply (12.5 Watts). Approximately 1.5 Amp is budgeted to run all of the hardware on the board when it is running at full load, and the rest is for providing power to peripherals. For example, to power a USB keyboard or microphone, some of the power is used from the main line. Under-powering the device can cause issues, and cause your Pi to behave strangely. The Raspberry Pi has a 5V pin that is directly hooked up to the power supply.

Hooking Up a Small Strip

The APA102s run off of 5 volts. Each APA102 LED consumes 0.3 Watts of power. So, if you have a 2.5 Amp power supply with no peripherals plugged in, you could, theoretically, run up to 16 of them directly off the board. However, given that many power supplies don’t fully deliver the power they advertise, and because you may want to plug in other peripherals on your board, we recommend maxing out at no more than 7 to be safe.

The easiest form factor to work with are the ones that come in a strip which can be purchased at places like Amazon.  If you want to avoid having to do any soldering, you can either cut a small strip on the end with a connector or use a male hookup wire to pierce through the small holes in between the LEDs (assuming you bought a strip that has these small holes.)

Caution: Connecting the leads while the unit is powered up could lead to a accidental short, which could damage the power supply and SD card of the unit itself. Always connect wires while the unit is powered down!
apa102 Android Things
Use the diagram above to hook up your LEDs. One thing to understand is that the signal wires are hooked up in series and can only pass signals in one direction. To that end, you will see terminals labelled DI, CI, DO and CO. In these abbreviations, the I stands for input and the O stands for output. Thus, if you had a small strip of lights and hooked your wires up in the middle of the strip, half of the lights would receive signals and work, while the other half would not. You therefore want to make sure that your control wires are always hooked up to the connectors labelled with an I.

Running Your First “Internet Of Bling” App

An Android Things app is, at its core, the same thing as an Android application. The main difference is that some services were removed, support is offered for accessing device pins, and you may, or may not, have a display. You can learn all about those differences here. The APA-102 driver is part of an contrib-drivers open source repo. There is also a  examples repo that contains examples for using these drivers. Of note:  At the time of this writing, the Pi diagram on the APA102 examples-repo shows the connections going to the wrong pins on the strip. (We have submitted a pull request that will fix this.)

Once you’ve powered up the device and connected (via “adb connect <ip-address>”), run the APA102 module from Android Studio and deploy it to your connected Pi. If everything is connected correctly and you app compiles and deploys, you should see something like this:

If the lights don’t light up, but everything looks good in your Android Studio Logcat, verify that you have connected things to the correct pins and that you don’t have a bad wire.

7 (or 16) LEDs are great, but I want MORE LEDs!

As you might recall from our earlier discussion, it is possible to control a lot more than 7 APA102 LEDs from an Android Things device. There are a few things you will need to know before attempting this. In our first exercise we were running 5 volts to the strip. That said, switching for SPI lines and all of the other I/O pins on a Pi are 3.3 volts. This means that the signals we were sending with our first circuit were not getting the full amplitude of the signals it was meant to receive. For a small strip of lights this will work, but as our strip gets longer and we start using an external power supply, we should convert the 3.3 volt signals to 5 volt ones. Beyond it giving each LED better data, it also allows us to have longer wire runs.

To do that, we are going to add a voltage leveler to the control lines. We are also adding in an external power supply for the LEDs that can handle the current, along with connectors. The capacitor is being added to reduce voltage spikes when you first plug in the external power supply. If we didn’t have this, a sudden voltage spike could end up burning out the first LED in the strip.

Things you will need:


Once you’ve gathered up all of these parts, you will want to hook up the circuit as per the diagram above. Note:  You will need to make sure that the notch on the voltage leveler is oriented correctly to get the correct hookups. In the diagram, it is on the left side of the chip.


Modifying the Code

Once you have connected everything up, you will need to modify the NUM_LEDS final in the file of the APA102 module to reflect the number of LEDs in your strip.

private static final int NUM_LEDS = 144; (1)
Now run the module and your lights should look like the image above!

Stay tuned for the next installment where we will discuss a few common problems.

About Me: I am a Atlanta based, native Android/IOS developer with AngularJS/Ruby experience and am founder of Polyglot Programming Inc.. Many of my projects focus on IOT and Wearables. You will often find me purr programming and I regularly speak at conferences around the world. I am available for hire! More Posts

Follow Me:
TwitterLinkedInGoogle Plus

I am a Atlanta based, native Android/IOS developer with AngularJS/Ruby experience and am founder of Polyglot Programming Inc.. Many of my projects focus on IOT and Wearables. You will often find me purr programming and I regularly speak at conferences around the world. I am available for hire!

Posted in Android, Android Things, Maker, Mobile Tagged with: , ,