Category Archives: Android Development Kit

Getting started with the ADK – Arduino

With Android 2.3.4 and 3.1, Google introduced support for external USB hardware to interact with an Android powered device, known as, Android Open Accessory 1. The USB Accessory hardware is based on Arduino 2, an open source electronics prototyping platform. There are certain requirements for these accessories, but to get started, it would be easiest to get an prototyping board, something like the Arduino Mega ADK.

You must download the Arduino drivers, and copy it to your Arduino Library directory. Instructions can be found on the Android website

The example that I am going to walk you through below, will allow for a LED to be controlled from your Android device. In another post, I will show you an example of an Android application that will be used to interact with this Arduino sketch.

To get started, include the USB Host Shield, and Android Accessory libraries in your project.

#include 
#include 
#include 

We are going to make use of the LED on the Android ADK board, but you can change this PIN value to the whatever reflects your hardware. On Arduino boards, the on-board LED is on pin 13.

#define LED1 13

Create a new object of the AndroidAccessory class, and specify your device identifiers.
The parameters are:
manufacturer, model, description, version, uri, and serial.

AndroidAccessory acc("Omskep",
		     "HelloADK",
		     "HelloADK Demo",
		     "1.0",
		     "http://www.omskep.com",
		     "0000000012345678");

Create a function that will initialize the LED to an OFF state.

void init_leds()
{
  Serial.print("rnInit LED");  //This is just for console output.
  digitalWrite(LED1, LOW);
  pinMode(LED1, OUTPUT);
}

On startup, initialize the LED and make a connection with the Android hardware, using the PowerOn function in the AndroidAccessory class.

void setup()
{
  Serial.begin(115200);
  Serial.print("rnStart");
  init_leds();
  
  acc.powerOn();
}

This is where the fun starts. We are going to check if there is a connection to the Android device, and listen for messages. We will be receiving a message, consisting of 3 bytes:

  1. Command - I like to think of this as a grouping, for example: Buttons, LEDs, Relays, Servos, etc.
  2. Target - Specify which component you want to “communicate” with. For example, the 1st LED.
  3. Value - This would be a value to be passed on to the component. In the case of the LED, a simple ON or OFF is all we need.

We are going to “group” the LED in group 1, so we will be listening for a command of 1. Since this is the only LED, we will be want the target to be 1. And finally, we will write the received value to the LED, on PIN 13.

void loop()
{
  byte msg[3];
  
  if( acc.isConnected() )
  {
    int len = acc.read(msg, sizeof(msg), 1);
    if( len > 0 )
    {
      if( msg[0] == 0x1 )
      {
        if( msg[1] == 0x1 )
        {
          digitalWrite(LED1, msg[2] ? HIGH : LOW);
        } 
      }
    }  
  }
  delay(10);  
}

I hope this helped explain the hardware side of the accessory. Another post explaining the Android side of the accessory is on the way.

Share:

Notes:

  1. http://developer.android.com/guide/topics/usb/accessory.html
  2. http://www.arduino.cc/