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

8 thoughts on “Arduino and Android devices: the (not so) well known USB connection

    • Hi dear Marco, I think you write from Italy! 🙂
      Your question is very interesting.
      In order to send commands to Android shell from Arduino the ADB protocol (over USB connection) should be used, instead the Accessory Mode.
      Some time ago I tried ADB using the most famous Arduino library for ADB (Microbridge), and I replaced Wiring.h with Arduino.h in Adb.h, max3421e.cpp, and usb.cpp (#include “Wiring.h” -> #include “Arduino.h”) in order to compile the example sketches.
      …But if I well remember, I had some problem launching the Microbridge example script “shell”, tied to the error “OSC did not started”, it could be tied to the fact that I don’use the USB Host shield but the Mega ADK (see this part in in my post…). Googling around I saw that someone suggests to try the last revision of Arduino IDE (1.5.5) insted 1.0.5 (I’m using it) but I didn’t tried this simple(st) solution.
      Well… another possible source for your experiments can be:
      The Microbridge Getting Started
      .
      But I think these two posts are more interesting: http://mlab.taik.fi/paja/?p=2449 and http://romfont.wordpress.com/2011/05/15/microbridge-adb-support-for-arduino/
      Your question excited me, so I think I will execute some other experiment on the argument….but if in the meantime you find some interesting result, you could post it here in Garretlabs. Let us know! 🙂
      Bye bye!

      • Thanks for the useful links! I am italian but I leave in the USA 🙂
        Actually I ordered the arduino Mega ADK rev 3 and I will receive it on Wednesday. So I will be able to make some experiments. I will be using a Samsung Galaxy S3 (T-mobile). Hopefully I can interface using adb. My project is to read AT commands coming from the GSM modem and, in case of an incoming signal, send another command to android shell. Any previous project that you have seen doing it or similar?
        Thanks

      • It’s a very interesting project! Unfortunately I never done something like that.
        For these purposes I use always a single board computer (such as Raspberry PI) with Linux onboard, because it’s simpler in my opinion using Linux to manage the connection with phones/3g dongles.
        In my next post I will use a Raspberry in order to receive and parse SMS messages, and then to command (via GPIO pins) a 220 Volts relay. 😉

  1. so…first results: galaxy S3 and arduino mega adk works together 🙂 I can send commands from the arduino to the android shell. Because the galaxy S3 has an android version >4.2 however, the library Adb to use is here
    https://github.com/agoransson/microbridge-arduino
    because it needs the RSA authentication once you plug the arduino board to the android device. Now I have to find a way to read AT commands…I will update 🙂

  2. by the way…I made it work using arduino 1.5.6 r2. The error that you mentioned OSCKIRQ bla bla bla is nothing that depends on power supply or board…is just matter of Adb library used. The one that you download from the above link does not give any of those problems. I had that problem too using the microbridge version downloaded from their website.

    • Yes, I saw that the “OSC did not started” error was not tied on power failure (as reported on some forum online).
      As I wrote in in the post, in my case, using Arduino IDE 1.0.5 with library for USB host shield 2.0, the problem was tied to a undefined macro (stating that I was using Mega ADK instead USB host shield). I think using IDE 1.5.6 this #define wuold be not necessary (as reported by some other forum).
      I think your case is similar to mine. 😉

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s