Saturday, December 31, 2011

A USB Host Shield 2.0 compatible AndroidAccessory Library

I've been reading the Arduino + Android Projects for the Evil Genius book (by Simon Monk) .... if you are interested in learning what's required to communicate between an Arduino and an Android Phone or Tablet, this is a great way to get started.   

Anyhow...while playing with the sample projects, downloading the code, etc. I found that my USB Host Shield (which uses v2.0 of the Arduino library from Circuits@Home found here) wasn't compatible with the AndroidAcessory library found in the Android Open Accessory Development Kit (ADK) that can be accessed here.  

So, rather than taking the time to change all of the Arduino sketches to be compatible with the newer USB Host Shield library, I went a different direction and instead took some time to create a 'compatible' AndroidAccessory library.  I can tell you that all of the Arduino sketches in the aforementioned book will compile unchanged ... and they seem to run just fine.   It really should work with any sketch that references the AndroidAccessory library ... but, I certainly can't guarantee that.

You can find the new AndroidAccessory library here.

Note:  I have tested this with Arduino 1.0 only ... but it really shouldn't matter, since there now aren't any Arduino-specific #includes ... but I could certainly be wrong ... so, if you end up having problems, let me know, I'll try to help.


  • Removed some (most) of the #included headers from the AndroidAccessory.cpp file which weren't needed ... just trying to make it as-compatible as-possible.
  • Removed a cast in the AndroidAccessory::read function call to RcvData that wasn't necessary.
  • Removed USB_NAK_LIMIT from the AndroidAccessory.h file and keywords.txt  ... it is already #defined in the USB_Host_Shield 2.0 library.
  • Added some Serial.print (and println) calls for visibility while debugging... of course, your sketch will have to begin the Serial connection.   But, now you'll be able to tell if anything is amiss.
  • Renamed the .zip to and added a README.txt file; although neither of these things have any effect on the library itself.
  • Re-Added USB_NAK_LIMIT to AndroidAccessory.h for compatibility with the recent USB_Host_Shield 2.0 library changes that now support Arduino 1.0.  I made it an #ifndef, so if it's already there it won't be added and if it isn't, it is added.
  • Move Library to Github.


  1. Android tablets are great, their slimness, quality of color, functionality. At first I was so confused about it, but once I decided to lets try and buy one. now according to my experience I advise every one to buy android.

    what is arduino

  2. AndroidAccessory.h:13: error: expected class-name before '{' token

    i'm stuck on it... =(

    1. Do you havea the USB Host Shield 2.0 library from Circuits@Home? From here:

      and if so, make sure you, at least, include usb.h before including the AndroidAccessory.h in your sketch.

  3. I am working on (Location Based Alarm System), a Friend Finder API . i am getting problem while setting up the range , so Will you please help me regarding to this.
    here is my email address :

  4. Hi!

    i am a student. and i am building my custom Arduino board. i am curious that i have to use adk library or Usb Shield. There are many other questions i have.

    what i am doing is
    1) Building Custom Arduino Board
    2) Building Custom FT232-RL USB-to-Serial Converter
    3) Arduino 1.0 IDE

    My Questions are

    1) How to burn bootloader (Which Board i select)
    2) What USB Host library i use.

    Please help me and send me an email.
    Yasir Ali

  5. seems the link to the library is bad:

  6. Many Thanks for making this lib.



  7. This comment has been removed by a blog administrator.