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

dslrrc – another Arduino+Android wireless EOS controller

Here is another exciting camera control project – an improvement of Yanis controller which I covered some time ago. The author – Ziggy from Crisp Concept used Yanis hardware as-is but developed his own control code for both Arduino and Android. Several additional commands has been implemented, most notably video recording and LiveView zoom control.

The project is hosted on Google Code. The source code of Arduino sketch is posted there and can be used for learning how to use commands not covered in PTP library examples. It should be noted that Ziggy uses modified versions of old PTP and USB Host libraries – you’d need to use them instead of standard ones in order for sketch to compile.

I’m hoping parallel development will be beneficial for Manishi’s and Ziggy’s projects. I’m following both with great interest and will be posting updates here. Stay tuned!


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

Building YaNis Android Wireless EOS Controller

In this article I’ll show how to build Wireless EOS Controller designed by Manishi Barosee. I’m building it for my Canons and if I like it, I’ll see if it’s possible to modify it for other camera systems. My controller is built around full-size USB Host Shield instead of Mini which Manishi used – I’m going to do some debuggung and need space to connect probes. Also, full-size shield is much easier to work with.

The design of Yanis is simple yet elegant. It consists of Arduino board, USB Host Shield and serial Bluetooth module. An Arduino sketch reads the serial port, generates camera control commands and sends them to the camera over USB. The Android application acts as a UI for the controller and sends control data over Bluetooth. Here is very basic schematic drawing of Arduino part of the controller showing necessary connections. USB Host connections are described in hardware manual and Bluetooth module connections are shown in detail below.

I’d like to start with radio link. The Bluetooth module used in this build is RN-42 from Roving networks. It is 3.3V device and its pins are not specified as 5V tolerant which means that Tx pin of standard 5V Arduino can’t be connected directly to Rx pin of [my] RN-42. Fortunately, the 5V to 3.3V level shifter on USB Host shield has 2 extra gates and I will be using one of them to “condition” the Tx. To do this, I need to cut ground trace going to pin 9 of D6 (marked ‘HCT’ on the PCB), connect it to Arduino pin 1 and then connect pin 8 of D6 to Rx of RN-42.

Cut pin 9

Cut pin 9

Tx to level shifter

Tx to level shifter

Continue reading Building YaNis Android Wireless EOS Controller

Testing RN-42 Bluetooth module using Android phone

RN-42 paired to an Android phone

RN-42 paired to an Android phone

Bluetooth modules from Roving Networks are popular among DIY enthusiasts – they are small, powerful and cheap. They are used in many projects; I’m currently building Yanis Android Wireless EOS Controller which uses Bluetooth module for communication between Arduino and Android phone. I know from experience that radio links are not always easy to establish and sometimes hard to troubleshoot, that’s why I have the habit to test radio component in stand-alone mode before other pieces of the project. In this article I show how I do it with RN-42 serial Bluetooth module using simple setup and test procedure. Other Bluetooth modules can be tested in similar fashion.

In general, any Bluetooth link is established in two steps. First is called “pairing” and the result of it is an exchange of credentials, which are then stored on one or both devices. Usually, pairing needs to be performed only once – for all subsequent connections stored credentials are used. Second step is actual connection – an application on one device connects to the other device and data exchange starts.

In order to test my RN-42, I need to somehow power the module and then be able to send and receive serial data. To achieve this, I made couple connections using pieces of thin wire (actually, cut-offs of resistor pins), which can be seen in detail on the picture below. Power connector for small LiPo battery is comprised of two wire strips on the right. A serial loopback, which connects TX and RX pins (more on this later) can be seen on the left. Title picture shows the module with battery connected.

When all necessary soldering was completed, I powered the RN-42 using small LiPo battery paying special attention to polarity and went to Network Settings on my phone. First, I made sure Bluetooth is checked and then went to Bluetooth Settings and started Scan for Devices. By default, RN-42 starts in discoverable mode on power-on so there is no need to do anything special. When device is discovered, I select it and enter 1234 as pin. When device is registered as “paired”, I can check the data transfer.

To send and receive serial data over Bluetooth, I need terminal program. There are several applications on the market, my favorite is BlueTerm. To check data transfer between RN-42 and Android, I start BlueTerm, connect to RN-42 and start typing. If RN-42 works correctly, characters I type will appear on the screen.

RN-42 connections

RN-42 connections

This happens because TX and RX pins on Bluetooth module are shorted together by a loopback wire. Data sent from the phone will appear on TX pin, then RX pin, then sent back to phone and then to the screen. To see if this is indeed the case I disconnect power from RN-42. Character echoing should stop.

This is all that needs to be done to check the module. Troubleshooting is also easy. If powered-on RN-42 won’t appear in discovery, check polarity (reverse polarity survival is not guaranteed – don’t make this mistake or you may need a new module). If polarity is correct, check battery voltage with multimeter. If voltage is within specs, check if it applies to correct pins. If everything is good but no pairing occurs, replace the module. If device is paired but no characters are echoed in terminal, check loopback pins and soldering quality.

When I confirm that my module is good, I can continue building the controller. I’ll write another post as soon as I get it working.


Arduino+Android wireless EOS controller

This is another excellent project which uses USB Host Shield and digital camera control library. Manishi Barosee created Bluetooth interface between Canon EOS camera and Android phone. Current revision of the code allows for manual control of aperture, shutter speed, ISO, white balance, focus, LiveView, and of course, capture. Planned features include video mode control, rack focusing, as well as intervalometer/timer/HDR services.

Watch the video and if you’d like to build the circuit, visit Manishi’s blog to see what’s involved. I am very impressed with design of this controller and plan to build one myself to control my DSLRs. Also, please take a look at Arduino code – an easy to understand sketch, which can be modified for other camera systems without touching the Android part of the controller.

Good job, Manishi!


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

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.