Using USB Bluetooth dongle on Arduino to connect to Android phone

Arduino connected to Nexus S

Arduino connected to Nexus S

Since Kristian’s release of SPP service for USB Host 2.0 library I got many e-mails with questions about Bluetooth serial communications in general and using the SPP class in particular. While establishing a Bluetooth connection is quite easy when using PCs/smartphones/tablets, the Arduino implementation is less user-friendly (for a good reason) and somewhat confusing. This article is intended to be a short tutorial of Arduino USB Bluetooth connectivity using an Android device as a peer – in my opinion, the simplest one to set up and communicate.

To follow this tutorial, you will need the following:

  1. An Arduino board ( I sell them too )
  2. full-size USB Host Shield 2.0
  3. USB Host Shield 2.0 library downloaded and installed in your Arduino library directory
  4. An SPP example sketch
  5. Bluetooth dongle – see below for detailed discussion
  6. Android device – a phone or tablet. The accuracy of this tutorial has been checked on Nexus S phone and Nexus 7 tablet, both running Jelly Bean 4.1.1 OS.
  7. A Bluetooth terminal emulator app for Android – I use Blueterm

The hardware setup is shown on the title photo (click on it to make it larger). It is a good idea to use external power supply at least in the beginning – some Bluetooth dongles are not happy when powered together with Arduino board/USB Host shield from USB cable. On a side note, it is always a good idea to use external power supply while experimenting with USB devices, especially when using current Arduino boards with their weak regulators. Also, Bluetooth code requires plenty of resources. The example used in this article compiles in ~24K and even though it would work on standard Arduino, any meaningful project will likely require a bigger board, like a Mega.

Continue reading Using USB Bluetooth dongle on Arduino to connect to Android phone

Exchanging data between USB devices and Android phone using Arduino and USB Host shield

CueCat connected to Android phone

CueCat connected to Android phone

Today, I’d like to show how to exchange data between USB device and ADK-capable Android phone. I will be using CueCat barcode scanner as source device; the data will be received by the phone and displayed on a screen using Arduino Terminal Android application.

A phone is USB device, too, and since two USB devices are unable to communicate to each other directly, I’m using Arduino board equipped with USB Host shield to relay data between devices. The sketch which runs on Arduino is a mix of two other pieces of code, one from ADK terminal emulator article and another one from an article explaining interfacing with a barcode scanner. Refer to these articles if you have questions about a specific piece.

Below is a full text of an Arduino sketch. It can be pasted from this page in Arduino IDE, compiled, and loaded into the board. It is also included in the examples section of USB Host library rev.2.0 distribution on gitHub. The library itself shall be installed in Arduino IDE tree as well.

To receive data from Arduino you’ll need Arduino Terminal application installed on your phone. The source code of application is also available – it is released under GPL2, if you make modifications to the code, please make them available for other people!

Finally, we will need some hardware – an Arduino board, USB Host shield, a USB hub, “declawed” CueCat or any other HID boot barcode scanner, as well as ADK-compatible Android phone. We will also need a 5V power supply capable of providing ~700ma of electrical current. I will show arrangement of all necessary pieces after explaining the sketch code.

Continue reading Exchanging data between USB devices and Android phone using Arduino and USB Host shield

Android ADK terminal emulator for Arduino

ADK Teminal

ADK Teminal

This is a little Android application providing basic terminal emulator functionality over ADK interface. Its primary purpose is to be able to use Android phone as laptop replacement while debugging Arduino projects in the field. It can also be used to provide simple alphanumeric display. Victor Serbo, my long time friend, helped develop this application. It can be downloaded from here and currently in beta – as soon as code stabilizes I will make source code available. [EDIT] The source code is now available. It is released under GPL2, if you modify it, please make your modifications available.[/EDIT]

When Arduino is connected to the phone via USB Host Shield, it can send characters to the application’s screen and receive characters typed on application’s keyboard. Standard CR and LF control codes are also recognized so it is possible to output, for example, single line with changing content. Additionally, the appearance and behaviour of the screen can be customized – font size and type, foreground and background colors, local echo, screen rotation and more. The app has been thoroughly tested on Nexus One and Nexus S phones and works well.

Two demo sketches has been posted to gitHub. The functionality of term_test can be seen on title picture – strings sent from Android are output back on the screen. The term_time sketch demonstrates single line output. Below is brief explanation of ways to provide input/output on Arduino. I’ll start with term_test; all necessary functionality is implemented in loop().

Continue reading Android ADK terminal emulator for Arduino

Google Open Accessory Interface for USB Host Shield Library 2.0 released

ADK code

ADK code

Google Open Accessory Interface is now included in USB Host Shield Library 2.0 package. It follows standard structure of rev2.0 device driver (wherever possible) and because of this is slightly different from original Arduino code developed by Google. The interface itself is pretty simple and there are many articles on the net describing it in details; in this article I will give brief overview of new code and explain the differences.

To test the code I made a little “shield” resembling Google’s one. Since the only thing I cared about while testing was data transfer in both directions, I implemented just one LED and one button. The demokit_20.pde Arduino sketch works with DemoKit.apk Android application, however, only “B1” button and “LED 1 Red” are functional. To run the sketch, you will need an Arduino board, USB Host Shield, as well as USB Host Shield 2.0 library.

The ADK class contains all necessary functionality to communicate to Android phone via ADK interface. In order for USB subsystem to initialize the device when it is connected, the instantiation should look like the following code fragment. First, the USB class is instantiated, then ADK is instantiated taking address of USB instance as the first parameter. The rest of the parameters are ID strings for the phone – they are identical to the original code.

The initialization of Android device and switching it to accessory mode is performed automatically by USB subsystem. The ADK::Init() member function is called each time a new device is detected on a bus. It first tries to determine if a device is in accessory mode already by reading its VID, PID, and if yes, configures it and reports success to the system. If device fails accessory check, the standard probing and accessory switching method is performed. If successful, device resets and appears on USB bus as an “accessory mode-capable” unit. In certain cases, Init() may be executed two times.

Continue reading Google Open Accessory Interface for USB Host Shield Library 2.0 released

Building Google ADK hardware from standard components

Soon after Google ADK announcement it was discovered that $300 Arduino-based ADK hardware kit (out of stock at the time of this writing) is not really necessary; the same functionality can be achieved with standard Arduino board and USB Host Shield. Today, one of the developers sent me a link to GoogleADK – site, which explains how to build Google ADK hardware from standard components and use it.

The site shows how USB Host Shield looks like when all headers are soldered in place. It also has Get Started page which gives step-by-step instructions on installing necessary software components on Linux box. Word of caution – the link on this page points to the product page of old rev.1.xx shield, even though all pictures show the current rev.2.0 one.

At present, the site is pretty lean but it is a good start nevertheless. I will be monitoring it to see if any new content would come up. Arduino-Android tandem offers very interesting capabilities and I’m hoping to see many cool Android-based projects soon.

Andriod ADK-compatible USB Host Library release.

UHS Charging Android

UHS Charging Android

Android Open Accessory Development Kit, announced on Google IO the other day is a hot topic. Android Open Accessory support, introduced in Android 3.1 (tablet OS) and backported to 2.3.4 (phone OS), allows external USB devices to interact with Android phone/tablet switched to so-called “accessory” mode. Because phones are USB functions (AKA devices) this accessory device must act as USB host. Arduino USB Host Shield has been around for couple of years providing USB Host functionality for standard and Mega form factor official Arduinos, as well as several clones. Luckily for all Arduino enthusiasts out there, Google decided to adopt this very architecture for hardware component of ADK. As a result, said enthusiasts now have well defined and documented code layer to interact with Android devices, ready to run on Arduino board of their choice. Well, almost.

The ADK code is targeted for ADK hardware component, which is just Arduino Mega 2560 and USB Host Shield combined on a single PCB. However, they decided to move some pins around; as a result in order to use the code with standard “Arduino plus Shield” setup certain code modifications are necessary. One such modification has been posted on Romfont. I tried it and the code works well, however, this approach has issues. First, it creates yet another distro. Second, the code modification exercise would have to be repeated each time Google releases new version of ADK. Finally, it’s difficult to have both standard and Google USB librares installed in Arduino IDE at the same time. Given all that, I decided to take different approach.

Google’s variant of USB Host Shield library differs very little from official library. The only significant difference is newInTransfer() member function of USB class, which returns real length of received packet. In order to support AndroidAccessory component in official library, I simply added this function to the library code and made a new release – it is now available on gitHub. I placed it into “dev” branch for now – it will be moved to “master” after a period of testing.

Continue reading Andriod ADK-compatible USB Host Library release.