Tuesday, May 24, 2011

Android ADK with a standard Arduino Uno and USB Host Shield

A few weeks have past since my last post and a lot was going on. First off how great was Google IO? I hadn't the chance to participate personally but I watched a lot of sessions online and was amazed of what Google had up its sleeve. New APIs, services, best programming practices but most importantly a lot of great talks about Android. As a software and hardware tinkerer I was glad to hear that Google pushes into the direction of the internet of things. With the upcoming project Android @ Home and the introduction of the Accessory Development Kit (ADK), Google showed some future outlook into their internet of things strategy.

Here is the link for the keynote of day one where they announced the ADK, and the dedicated ADK session, in case you missed one of them.

So what was done to support hardware communication? With Android 3.1 and Android 2.3.4 Google implemented USB APIs to communicate with accessories via USB. The Android devices operate in USB slave mode so that the accessories need to provide USB host capabilities. To demonstrate what is possible when your device communicates with external hardware, Google developed a reference demo board and released it as open source. They based their reference board on the Arduino which is really great, because it is widely spread in the open hardware community. Their example firmware and Android software is based on their reference board to show what kind of sensors and actuators are easily accessible. So if you attended the Google IO and got your reference board on site, that's great news for you. You could just dive right in and tinker with it.

Unfortunately not everyone could get a ticket (me neither :( ), so those developers have to wait for compatible hardware boards being distributed some time at the end of the year, or they could order one from the japanese company which produced them for the Google IO. Unfortunately they are very pricey and cost about 400$.

Soon after hearing that I decided to port the firmware and software to my basic Arduino Uno and my USB Host Shield. It is no suprise that I wasn't the only one who did that.

So if you have the Arduino Uno and a USB Host Shield lying around you might just want to give the ADK a try.
The cool thing about it is that you register a USB Broadcast receiver on your device that can automatically detect when the device is connected to your board and start up the corresponding application.

Here the steps I took to make my setup work with the ADK:
  1. Read and understand the ADK section in the Android Developers Guide.
  2. Follow the installation process.
  3. Don't use Googles USB_HOST_SHIELD library. I used a patched version which I found here. If you are having trouble compiling the demokit.pde it may result from a wrong pin definition in the patched USB_HOST_SHIELD library. Just open the Max3421e.cpp file and change #define INT PB1 to #define INT PORTB1 and #define RST PD7 to #define RST PORTD7. You might also try to use the newly released original version of the USB_HOST_SHIELD library which now supports the ADK, but I don't know if it works.
  4. Strip down the demokit.pde to its bare essentials, meaning only the communication part.
  5. Strip down the example Android app to the communication part and implement your own use cases.
To make it a bit easier I provided my example Android app and my stripped down version of the demokit.pde at github. Feel free to play around with it. Note that my approach would be considered as a temporary hack or proof of concept rather than a good solution of the porting problem. So when new versions of the ADK will be released you would have to modify them again which would be painful.

Here is a quick demo of controlling the digital pins 2-7 in output pinmode:

As promising as the ADK is right now, I have a problem with having to connect my device via cable to the accessory. The future of the internet of things will be wireless, so as long as there are no Android devices which support a Zigbee like protocol, I will use my Arduino/Android Wifi approach which you might remember from a post in the past.

That being said...
I can't wait for Android @ Home at my home ;).


  1. What resources would you suggest *specifically* for young teens who have received the arduio kit from amazon this week?

    ..who want to quickly cross over into java/c++ with android hw.

  2. Well for Arduino experiments in particular I would suggest the tutorials on the Arduino Site:
    http://arduino.cc/en/Tutorial/HomePage and searching for tutorials on Instructables: http://www.instructables.com/tag/type-id/category-technology/channel-arduino/

    I also have some Arduino experiments on this blog but you might need additional hardware for some of them.

    To experiment with Android and hardware interfacing you would need to learn Java as the foundation and then make your way to the Android development specifics. This might be a very long way for a young teen. However I would suggest learning Java by a "Head First" book which is well written and entertaining. http://www.amazon.com/Head-First-Java-Kathy-Sierra/dp/0596009208/ref=sr_1_1?ie=UTF8&qid=1308399024&sr=8-1

    Google provides a huge amount of information and examples on their Android developer page which I think is mandatory to understand Android development.

  3. Hi, wouldn't it work to have a zigbee communication between device A and device B then attach also a bluetooth to device B to connect to any Android phone?
    Am thinking of this because neither with wifi nor bluetooth i can get reliable communication with an UAV.
    With good Zigbee modules it is possible to reach up to 1km in line of sight.

  4. Hi, Would this be possible with this Arduino clone or would I need an Arduino Uno ?

  5. Generally yes. You can use zigbee for your long range communication to the UAV and some kind of mediator board establishing a bluetooth connection to your Android device. But note that this has nothing to do with ADK. What you are doing is just defining your own communication protocol.

    The advantage of the ADK is that the device has a stable connection to the microcontroller and it is powered by the accessory itself. The ADK is designed for different use cases where you have stationary hardware that you want to interact with. In your case you would be better off not using the ADK. If you want to connect any phone to your UAV your approach would be a better idea. There are possibilities to use the ADB (Android Debug Bridge) as a fallback for devices which don't support the ADK. One project is microbridge: http://code.google.com/p/microbridge/
    But you probably don't want to connect phones by USB as the usability would suffer.

    You should however have a lookout for the Android at Home Project which Google announced at this years Google IO. They plan to use zigbee directly to control home appliances. Devices supporting zigbee are awaited later this year or the following year. So in some month you might be able to control your UAV directly by an Android device.

  6. "Hi, Would this be possible with this Arduino clone or would I need an Arduino Uno ?"

    You can use any Arduino clone which is derived from the original Arduino Duemilanove/Uno hardware design. Meaning that the board has to have the same pin layout and electrical characteristics. In your case the board you mentioned works perfectly for that.

  7. Thank you very much for this demo code.
    I got my "Arduino Mega ADK" board yesterday. I googled a while, tried a lot but could not get it to work with my Android device.
    Then I found your demo code and everything worked instantly and smoothly. This is the perfect basis for my own developments.
    Keep up the good work!

  8. how are the led pins connected?

  9. @Vali: The three LEDs are conencted to the first three PWM digital pins of the Arduino Uno /USB host shield, which are digital pin 3, 5 and 6.

  10. the phone apk does not detect the arduino board

    I still did not understood how you wired up everything...

    I wired 3 leads to pins 3 5 and 6 and their ground to ground pin on arduino board...

  11. Hi, I'm doing a project where I'm trying to take the circuit board from a blood glucose monitor and connect that to the Arduino Uno to be able to connect the mechanism to an Android phone. Would this be possible? Am I going about this the right way?

  12. I got rcode to be 4 at this line in newIntransfer():

    rcode = dispatchPkt(tokIN, ep, nak_limit)

    What may be the error?

    I am connecting a Huawei y200t 4.03 with a Arduino Uno and ITead USB shield. I am not connecting any external supply

    Help me...Thanks in advance

  13. hi,

    i tried to compile your code but got some error "acc not declared error" in my arduino ide 1.0.1. Pls advise me. Thanks.

  14. Hello Mario,

    I'm testing the Projec04 (pot) of your book Android ADK with a smarthphone Android 4.4 (Kit Kat) and does not perform correctly. If I try with an Android 4.1 (Jelly Bean) tablet everything works fine.

    I suspect a problem with the threads because Project01 Project02 projects and work well both in the KitKat and in JellyBean.

    Any tips to help with these problems in threads KitKat?

  15. I really want your help. I have an arduino UNO ! what I want to do is the following. Connect an android phone to the arduino UNO via an OTG and a USB A to B cable. Can I write an android app which lets say sends the number 1 to the phones USB and the arduino reads that data and does something? can this be done or I have to to buy a USB host shield for arduino. My android phone is galaxys3. I am searching the Internet for two weeks now and I can't find something relative. Basically I want to use my phone as a sensor shield for arduino. It's a petty to have so many sensors in an android phone and can't use them with arduino. Actually you can via Bluetooth but I want it to be a wire connection ? Thanks in advance for you reply