App Development
IT & Services

Android ADK Part 2: Let There Be Light (Emitting Diodes)

Mutual Mobile
No items found.
August 31, 2011

This is the second installment in our series on programming for the Android Accessory Development Kit. To read the first installment covering the background of mobile accessories and some of the potential of the ADK, click here.

Although experienced developers can create incredibly sophisticated projects with the ADK, the barrier to entry is low enough nearly anyone can tinker with making app accessories. Our exploration of the ADK starts at the ground level, by adapting a demo project that accompanies the Seeeduino ADK Main Board, an Arduino printed circuit board created by Seeed Studios.

In addition to the board, a few additional parts are needed to get started. We suggest a breadboard, LEDs, resistors, a potentiometer/variable resistor, and some wire. These are all parts that anyone can readily acquire online or at a brick-and-mortar electronics store, and are all staples of any familiar electronic accessory. To the individual making a retail purchase these components are inexpensive, typically less than a dollar per (except for the breadboard which can be found from $5 to $15 and upwards depending on size, number of busses and other features). On an industrial production scale these components are often fractions of a cent.

Seeduino’s original project tutorial uses a protocol called Microbridge, but we have adapted the project to use the Android Accessory Protocol. The more advanced AAP protocol is designed for this type of communication and is suited for more sophisticated Android devices, like the Xoom tablet.

The board is itself programmed in a C-like language that should come naturally to those with experience in Java. To edit and compile the microcontroller code a free open-source Integrated Development Environment is available at the Arduino website. Programs written in this IDE  are called ‘sketches’, have .pde extension, and are loaded onto the micro-controller via a USB cable.

In order for an Android device to communicate with the board, we also need a way to write and compile an Android application package file. Our preferred tool is the Eclipse IDE using the Android Development Tools plugin.

Finally, once the project is built in our IDE, we will need to get it into the device. The device will already be plugged into the microcontroller, so at this point a developer usually has to shuffle around the USBs. We found it much easier to use a cloud service like Dropbox to transfer the package file to the device. A computer with Bluetooth capability can also transfer the file via that route.

This short piece of code includes the USB and AndroidAccessory libraries, as well as a global log for the sampling timestamp, a boolean to hold the LEDs toggle state, and the initialization of an AndroidAccessory object. This is followed by the two lifecycle methods all sketches are minimally required to include: ‘setup’, which unsurprisingly sets up and initializes the program, and ‘loop’, which repeatedly performs the actions. We have also included a method named ‘toggleFlashing’ that, toggles the flash of the LEDs.

On the Android side, the code is longer but will be fairly straight-forward to anyone experienced with Android development. This simple demo app only has one small activity, which is to demonstrate the interaction between the ADK and the Android device:

When these two pieces of code are run together – the sketch on the Seeduino board which is wired and connected to the LED and variable resistor on the breadboard, and the Android app on a compatible device, with the device and microcontroller connected via a USB cable the result is an app that can toggle a flashing LED on the breadboard, as well as display a numeric output and progress-bar to indicate the changing resistance on the potentiometer as it is manipulated.

The above is a very simple illustration of what is required to connect an Android device to an ADK compatible board that is in turn connected to external components, sort of a ‘Hello World’ for the Android Accessory Protocol. In our next installment we are going to turn it up to eleven, and explore connecting an electric guitar to our ADK board to use the Android device as a signal processor to create a virtual effects box.