Arduino and Android devices: the (not so) well known USB connection

Hi boyz and girlz (with the “worm” of embedded development inside! 🙂 ).

This time I would like to talk about a new topic (following an input from SirsLab of University of Siena), tied to the connection via USB between Android phones (or tablets) and Arduino.

The goal is to cerate a master-slave channel on the USB in order to control an Arduino from a Android app. A possible logo for this “experiment” could be the following:

Arduino+androidMhhh…Android hides behind an Arduino. What a strange idea (and what a strange logo—>produced by ML)! 😀

Ok, I say clearly that I don’t like to use an Android phone as Arduino controller, since in my honest opinion writing an Android app in order to use the USB in master mode is not so simple and immediate. I prefer to use a linux single board computer (such as Raspberry PI) to control Arduino, as you can see in my previous post(s): here and here.

But the argument is very exciting and …”I was born ready” (cit. Jack Burton of “Big trouble in Chinatown” movie)! 😉

This is the “hand-made schema” of the complete system at the end of this post (click to view the zoomed image):

wpid-20140509_151054.jpg

 

First step: verify the Arduino USB connection

Well, for my experiments I used the Arduino Mega ADK board (it has onboard the USB Host Max4321 controller ), but there is another chance: you can use the dear old Arduino Uno with the Arduino USB Host Shield 2.0 from Circuits@Home (or the alternative solution from SparkFun). My Arduino Mega ADK was part of the famous “Arduino on Android Kit” bought from Robot Italy (this has been the Monica gift for my birthday! 😉 ).

In order to verify the USB host capability of the Arduino I connected a USB memory stick to the USB host port, then I downloaded the USB Host Shield 2.0 library and I inserted it in the libraries folder of Arduino IDE.

IMPORTANT ACTION:  I opened the library file settings.h and I  modified the following line (note that the default for the define is =0, meaning that you are using the USB host shield instead Arduino Mega ADK):

#define USE_UHS_MEGA_ADK 1

This step is fundamental (if using Arduino IDE 1.0.5) because otherwise all sketches using USB Host library  will give the error “OSC DID NOT START” at runtime, since the function Usb.Init() will return a -1.

Once saved the modified file, I opened the library example script called USB_desc, and after the succesful compilation and download on the Arduino ADK, I opened the Arduino serial shell at 115200 bps.

The output reported all informations about the USB memory stick (vendor, class, ID etc.).

So, we can conclude that the USB host capability of the Arduino Mega ADK is OK.

…..Pheeew, now I can take a breath (and a break). 🙂

 

Second step: connect to Arduino an Android phone (or tablet) supporting the “Android Accessory Mode”

This is the hard truth: not all Android devices can enable the “Accessory Mode”, the necessary capability to talk with “accessories” (in our case the Arduino Mega ADK) via the ADK protocol. In these cases only the ADB protocol could be used… but this is another story! 😉

Note that the Accessory Mode is a capability depending on the Android version installed on your phone. 😦

For example,  my old Samsung Galaxy S (GT-I9000) has a too old Android version installed, so, in order to enable the Accessory Mode, I flashed on it the latest version of Cyanogen Mod.

And, at the same time, I had no problem using a Samsung Galaxy Tab 2 with his original firmware….since the Android version is newer and it supports Accessory Mode.

IMPORTANT ACTION: In order to enter automatically in Accessory Mode (if supported by the Android version), the mandatory step is to enable the “USB Debug mode” on the phone.

I opened and compiled the ArduinoBlinkLed  sketch from the ADK examples provided in the USB Host Library.

I powered ON the Arduino and I opened the Arduino shell (…and the encourageant sentence “Android accessory started” appeared ;-)).

Then I connected the Android device to the Arduino host USB. After a moment, the screen of the device reported “Accessory Mode entered” and after this a message appeared on the device requesting to download from internet the Android app associated  to the sketch.

The downloaded app (reported in the Arduino sketch) is http://www.tkjelectronics.dk/uploads/ArduinoBlinkLED.apk, note that I didn’t found the sources for it… 😦

Anyway, starting the downloaded/installed app, a great button appeared on the device screen. And the result of this big effort activity is a little poor, but at the same time it’s very nice: clicking the software button, the Arduino LED 13 (the onboard led) turns ON/OFF!!! Yeahhhhhhhh! 🙂

 

Conclusions

The code on the Arduino side is (as always) very very very simple. The code on the Andoid side (the app), in order to manage the ADK protocol, in my opinion can be very difficult to be implemented, because it contains some strange instruction and construct.

In which mode I discovered it? Well… studying and compiling in the Android IDE an application similar to the “closed source” one used for my experiment. I found it in the ADK examples from Google Code or around Internet (it is called SkeletonApp) : you can download it from this link. Obviously you should compile this code in the Android IDE Eclipse in order to produce the app (i.e. the .apk file to install on your Android device).

Note that you could modify the ArduinoBlinkLed script in order to download/install from internet your own app modifying the following code:

ADK adk(&Usb, "ML Soft", // Manufacturer Name
              "ArduinoBlinkLED", // Model Name
              "Example sketch for the USB Host Shield", // Description (user-visible string)
              "1.0", // Version
              "http://YOUR_OWN_SITE_ADDRESS/SkeletonApp.apk", // URL (web page to visit if no installed apps support the accessory)
              "123456789"); // Serial Number (optional)

Ok boyz and girlz….this time we produced a very interesting  result. 😉

Now I need a very long time of relax… so I think I will launch a Debian kernel recompilation process (and in the meantime I will drink a good Grappa). 😀

Bye bye!

 

Advertisements