Android USB Host + Arduino: How to communicate without rooting your Android Tablet or Phone

Intro

In the past two posts we have explained the basics of USB communication with the Arduino Uno (also applicable for Arduino Mega).

Galaxy Nexus to Arduino UNO via USB

In this post we’ll put everything together and show you how to communicate between your Android application and the Arduino using nothing but the Android USB host API. Remember, this approach has nothing to do with Android ADK! Unlike Android ADK, your Android device will act as the USB host, while your Arduino board will act as the USB device.

For the following application to work, you will require an Android device that supports USB host mode as well as the USB host API. Most Android 3.1+ tablets will suffice (some may require an USB OTG adapter). Also, the Galaxy Nexus has host mode enabled and matches the requirements (you will need an USB OTG adapter however).

This example consists of two parts:

  • The Android application that makes use of the USB API
    A simple Android app that let’s you regulate the brightness of an LED on the Arduino using a slider. It also features a button to “enumerate” the USB device.
  • Firmware for the Arduino that does some serial I/O with the Android app
    Very basic firmware for the Arduino. An interrupt is generated when a new byte is received. The received data controls the brightness of the Arduino’s on-board LED.
    (implemented via usleep-style software pwm in the main loop).

The Arduino firmware

In the main loop the firmware asserts and clears the LED pin of the Arduino (PB5). Here is a shortened excerpt:

int main(void) {
	//initialization
	initIO();
	uart_init();
	sei();

	uint8_t i = 0;
	volatile uint8_t pause;

	for(;;){//this is the main loop
		pause = data;
		PORTB |= (1 << LED);
		for(i = 0; i < pause; i++)
			_delay_us(10);
		PORTB &= ~(1 << LED);
		for(i = 0; i < 255-pause; i++)
			_delay_us(10);
	}
}

During a period of 2550[us], the LED is asserted for a duration of pause*10 [us] and cleared for (255-pause)*10[us]. Simply put, this is a very simple software PWM.

During that time, “data” and consequently “pause” may be changed within an interrupt routine form the serial USART port. This happens when the Android side sends data to the Arduino. The interrupt routine is extremely basic:

ISR(USART_RX_vect) {//attention to the name and argument here, won't work otherwise
	data = UDR0;//UDR0 needs to be read
}

The RX data has to be read in the ISR (interrupt service routine) from UDR0; have a look at the Atmega328P reference manual for further details. Since we are doing no multi-buffering shenanigans the handling is extremely simple (no need to call cli() or anything).

The rest of the code is initialization of the I/O pins and UART functionality. Download the complete example here: led_pwm.c

Controlled LED on the UNO by the firmware

 

The Android app

The Android application uses the basic knowledge of the preceding blog post Arduino USB transfers. During USB initialization, the Arduino USB serial converter is set up and after that, communication is done using the bulk IN endpoint of the very same serial converter.

With both the aforementioned firmware installed your Arduino board and the Android application installed on your phone or tablet, you will be able to control the brightness of the Arduino Uno’s built-in LED with a slider on your Android device. Again, please note that this will only work with devices that actually support both USB host mode (hardware, kernel requirement) as well as the Android USB host API (Android OS requirement).

The source code is available here: UsbController.tar.gz*
* You may need to change the PID value in UsbControllerActivity.java on line 38, if you have an Arduino Uno Rev3 or higher. You can check the VID/PID value with ‘lsusb’ after connecting the Arduino to your computer.

Many parts of the code are probably familiar to Android SW engineers. The most interesting section is in the class UsbController where the Arduino device is set up and communication is initiated. So let’s have a closer look at the inner class UsbRunnable within UsbController:

private class UsbRunnable implements Runnable {
	private final UsbDevice mDevice;

	UsbRunnable(UsbDevice dev) {
		mDevice = dev;
	}

	@Override
	public void run() {//here the main USB functionality is implemented
		UsbDeviceConnection conn = mUsbManager.openDevice(mDevice);
		if (!conn.claimInterface(mDevice.getInterface(1), true)) {
			return;
		}
		// Arduino USB serial converter setup
		conn.controlTransfer(0x21, 34, 0, 0, null, 0, 0);
		conn.controlTransfer(0x21, 32, 0, 0, new byte[] { (byte) 0x80,
				0x25, 0x00, 0x00, 0x00, 0x00, 0x08 }, 7, 0);

		UsbEndpoint epIN = null;
		UsbEndpoint epOUT = null;

		UsbInterface usbIf = mDevice.getInterface(1);
		for (int i = 0; i < usbIf.getEndpointCount(); i++) {
			if (usbIf.getEndpoint(i).getType() == UsbConstants.USB_ENDPOINT_XFER_BULK) {
				if (usbIf.getEndpoint(i).getDirection() == UsbConstants.USB_DIR_IN)
					epIN = usbIf.getEndpoint(i);
				else
					epOUT = usbIf.getEndpoint(i);
			}
		}

		for (;;) {// this is the main loop for transferring
			synchronized (sSendLock) {//ok there should be a OUT queue, no guarantee that the byte is sent actually
				try {
					sSendLock.wait();
				} catch (InterruptedException e) {
					if (mStop) {
						mConnectionHandler.onUsbStopped();
						return;
					}
					e.printStackTrace();
				}
			}
			conn.bulkTransfer(epOUT, new byte[] { mData }, 1, 0);

			if (mStop) {
				mConnectionHandler.onUsbStopped();
				return;
			}
		}
	}
}

After the USB interface has been claimed the Arduino USB serial converter is initialized by issuing the following control transfers:

conn.controlTransfer(0x21, 34, 0, 0, null, 0, 0);
conn.controlTransfer(0x21, 32, 0, 0, new byte[] { (byte) 0x80,
				0x25, 0x00, 0x00, 0x00, 0x00, 0x08 }, 7, 0);

The first call sets the control line state, the second call sets the line encoding (9600, 8N1).
For communication, an additional thread is used to send data without blocking the Activity’s main UI thread. By notifying sSendLock of the UsbController the data will be transferred. After submission, the thread will go into “wait” again. This way, even if submission takes more time than expected, the Activity’s main thread will not be blocked and hence the app will not become unresponsive.

Screenshot of the Android App

Also note that in the Android Manifest none of the XML-style device filters are needed, since enumeration happens by the user in the app when pressing the “enumerate” button. Device filters – and therefore automatic activity launch when connecting the Arduino – are not used in this example in order to make the code simpler to comprehend. However, this could be easily implemented with a few lines of additional code.

For developing this example we have used a Galaxy Nexus Phone with an USB-OTG adapter cable. It has also been successfully tested with an Android Tablet, the Acer Iconia Tab A500, this tablet does not need any additional adapter cables.

This post concludes the 3-Part Arduino USB communication series. Feel free to post any questions or feedback/ideas in the comments section or contact us via E-Mail (http://www.nexus-computing.ch).

All code you find in this post can be used under GPL for your own projects.

The following section “About Android and USB Host” again concludes why USB-Host is becoming more and more important for mobile devices and points out main differences between Android ADK (Android Accessory Development Kit) and the Android USB Host API.

EDIT: control transfers for FTDI equiped Arduinos
Since we got requested a lot if the FTDI Usb-Serial converter will work too, here ist the control transfer code that needs to be exchanged. No warranties though ๐Ÿ™‚

conn.controlTransfer(0x40, 0, 0, 0, null, 0, 0);// reset
conn.controlTransfer(0x40, 0, 1, 0, null, 0, 0);// clear Rx
conn.controlTransfer(0x40, 0, 2, 0, null, 0, 0);// clear Tx
conn.controlTransfer(0x40, 0x03, 0x4138, 0, null, 0, 0);

Source Links

Android App: UsbController.tar.gz*
Arduino main: led_pwm.c

* You may need to change the PID value in UsbControllerActivity.java on line 38, if you have an Arduino Uno Rev3 or higher. You can check the VID/PID value with ‘lsusb’ after connecting the Arduino to your computer.

About Android and USB Host

Lately it has become more and more popular to use tablets or mobile phones to communicate with the outside world over USB. After all, many devices now feature a full USB Host, either with a USB-OTG converter or even with a full sized USB Type A interface. Also, the future promises even more host availability on mobile phones. This opens up an entire range of new possibilities for already existing hardware as well as newly designed hardware for phones.

Not too long ago, Android received its own USB API. It was at the Google I/O in summer 2011 when the “Android Open Accessory Development Kit” was announced and released. However it lacked of a few crucial points. The API implied that there is an external USB host, acting as a “master” if you will.
On the one hand, this means that an already existing USB device mode gadget cannot work with your Android device. Therefore, if a manufacturer wanted to support Android phones it was necessary to create new hardware as well as new firmware. Secondly, the new hardware had to be designed to power itself and also deliver power the Android device; this implied that mobile gadgets require their own power source. And secondly, there was another issue: only devices running Android 2.3.4+ were shipped with the new API. Even then, it was up to the manufacturer to actually include the required stack in the OS.

But wait, there is another way to communicate over USB. At the same time (Google I/O) the Android USB Host API has been published for Android 3.1+. Starting with the second half of 2011, Android devices appeared which supported USB OTG (although devices with Android version < 3.1 were not supporting the API). With an appropriate adapter it has become possible for Android to act as USB Host. This meant that already present USB hardware has become compatible with the OS. As long as the kernel on the Android device supported the USB standard driver of the hardware (mass storage, input, etc.), Android would be able to use it and therefore open up a new range of extra devices compatible with the system.

However, there are many devices that have not been “compatible” from the beginning. For instance, let’s say your common RFID reader. It most likely uses a USB-serial port and probably comes with a Linux or Windows driver as well as some software. Most Android tablets will come without the usb-serial driver for your RFID reader however. Therefore, if you want to load your driver you will need to root your tablet, determine the version of your current kernel, find the kernel sources online, hope that everything compiles to have your driver ready and then load it onto your tablet. In the end, when you finally have your kernel driver running, you will be required to write C code as well as some JNI glue to communicate with your Activity or Service in Android. All in all, this approach is not very straightforward.

Writing your own USB soft driver

There is a very elegant solution to aforementioned problem. It requires far less skills in hacking and porting than the mentioned approach. However, you will require some advanced knowledge in Android programming as well as some USB know-how.
You can write your own “soft driver” in Android. Since the USB Host API has been released, it is now possible to communicate with any USB device using the most commonly seen USB transfers (control, interrupt, bulk). In the end, your result will be portable across all Android devices that have USB host enabled and have Android version 3.1+. Moreover, this solution does NOT require root access to the tablet or phone. It is currently the only viable solution that does not require the user to have any know-how of rooting/hacking the device and risk losing warranty in the process.
The above Android application uses exactly this approach. It represents a very basic soft driver for the Arduino’s on-board USB-to-serial converter.

Arduino USB transfers

In this post we will show you how to communicate between the Arduino Uno and your computer using plain USB bulk and control transfers, not relying on a serial communication interface. Our development computer runs Ubuntu 11.10 (Oneiric Ocelot).

This is a follow up to Programming the Arduino Uno with Eclipse on Linux. The next post in this series is Android USB Host + Arduino: How to communicate without rooting your Android Tablet or Phone.

The Arduino Uno has an on-board USB-to-serial converter (ATMega8u2 or ATMega16u2 since rev. 3). The preloaded firmware on this converter presents itself as an USB Communication Device Class (CDC). Usually, no further software driver is needed on the host computer. You can simply plug in the Arduino Uno to your computer, and you will be able to communicate with the Arduino’s main microcontroller, the ATMega328, over an emulated serial communication interface.

For many applications, a serial communication interface is just fine. You can use minicom, gtkterm or a similar terminal application to send and receive data from your microcontroller. Or even use a python serial library and start off from there. In some circumstances however, you might not have the possibility to open a serial communication. Android for instance does not have a native serial communication interface, instead it features an USB host API. In this case, you must rely solely on USB control and bulk transfers.

When you connect an Arduino Uno to your linux computer with an USB cable, you will notice that a new device node /dev/ttyACM0 or similar is created. For this purpose, check out the last few lines from dmesg right after connecting the Arduino Uno.
$ dmesg | tail
...
[] usb 1-5.2: new full speed USB device number 43 using ehci_hcd
[] cdc_acm 1-5.2:1.0: ttyACM0: USB ACM device

In the above example, the Arduino Uno is assigned to USB device number 43, and a new device node /dev/ttyACM0 is created. This device node is automatically created by the module cdc_acm and represents a serial communication interface which you can use with minicom, gtkterm, etc. However, we are not going to do that. We want to find out how to talk to this device using USB transfers, right? ๐Ÿ˜‰

Further investigations with the vendor (0x2341) and product (0x0001) id …
lsusb -v -d 2341:0001
… yield more interesting information about this device. For instance, bNumInterfaces = 2 shows that there are two available interfaces. This stems from the mentioned pre-loaded Arduino firmware. There you will see two projects:

  • arduino-usbdfu is the Arduino USB DFU bootloader firmware, which is used for flashing the ATmega8u2 or ATmega16u2 respectively.
  • arduino-usbserial is the real firmware of the USB-to-serial converter. This is where the magic happens ๐Ÿ™‚

Again, looking at the output of …
lsusb -v -d 2341:0001
… you will see two interface descriptors. The first one with bInterfaceNumber=0 is the DFU bootloader, and the second one with bInterfaceNumber=1 is our usb-serial firmware interface descriptor. You can double-check that in arduino-usbdfu/Descriptors.c and arduino-usbserial/Descriptors.c respectively.

The starting point of the usb-serial project is in firmwares/arduino-usbserial/Arduino-usbserial.c. At the top, a variable VirtualSerial_CDC_Interface is initialized, and it is then passed in the main’s for-loop:
...
CDC_Device_USBTask(&VirtualSerial_CDC_Interface);
USB_USBTask();

The function CDC_Device_USBTask() is part of the LUFA-lib (Lighweight USB Framework Library), and is defined in CDCClassDevice.c. Likewise, this source file can be considered as the heart-piece of the usb-serial firmware. There we find all important functions such as CDC_Device_SendByte and CDC_Device_ReceiveByte. You will notice that almost every function returns immediately if no LineEncoding/Baudrate has been set. Luckily, the function CDC_Device_ProcessControlRequest (also in CDCClassDevice.c) unravels the mysteries of setting the LineEncoding, Baudrate, LineState, etc. Here is a shortened excerpt:

void CDC_Device_ProcessControlRequest() {
 switch (USB_ControlRequest.bRequest){
  case CDC_REQ_SetLineEncoding:
  if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE)){
   CDCInterfaceInfo->State.LineEncoding.BaudRateBPS = Endpoint_Read_32_LE();
   CDCInterfaceInfo->State.LineEncoding.CharFormat  = Endpoint_Read_8();
   CDCInterfaceInfo->State.LineEncoding.ParityType  = Endpoint_Read_8();
   CDCInterfaceInfo->State.LineEncoding.DataBits    = Endpoint_Read_8();
  }
...

Now we’re cooking :-). To set the LineEncoding we therefore need to send an USB control request with bRequest = CDC_REQ_SetLineEncoding = 0x20 (as defined in CDCClassCommon.h) and with bmRequestType = 0x21. Also, the data must hold 7 bytes with corresponding values.

Once you know which requests to look for, it is pretty straightforward. Use an USB sniffer like wireshark and observe what happens when you plug in your Arduino board. With wireshark, you can also search for a specific bmRequestType by applying a filter like:
usb.bmRequestType == 0x21

As it turns out, there are two important control transfers when connecting the Arduino board to the computer. The first one has bRequest = CDC_REQ_SetControlLineState = 0x22 = 34 and the second one bRequest = CDC_REQ_SetLineEncoding = 0x20 = 32.

If you want to try it for yourself, download and run this python script (with sudo). This script will initialize the usb-serial-converter with USB control transfers and then send single bytes from your computer to the Arduino Uno using bulk transfers. Just make sure to remove the cdc_acm module beforehand by issuing:
$ sudo rmmod cdc_acm
As this module will otherwise lock access to the USB device. Also, you will need appropriate software running on your Arduino board. You may download this source code and flash it onto the device. This will set the duty cycle of a PWM signal on PIN3 according to the byte received from the computer. Also it will light up the on-board LED if the received byte is an odd value and shut it off when an even value is received.

Programming the Arduino with Eclipse on Linux

This is the first of a series of three posts. The ultimate goal is to setup a communication interface between an Arduino Uno/Mega board and an Android tablet over USB. Everything will be as user-friendly as possible, i.e. no root will be required on your Android tablet. The following posts Arduino USB transfers and Android USB Host + Arduino: How to communicate without rooting your Android Tablet or Phone conclude the series.

The Arduino Uno is a popular and affordable hardware platform which comes with its own IDE and core libraries for programming. If you like the Arduino IDE you may skip this post, however, if you feel more comfortable developing in Eclipse this might help you out.
We are using Ubuntu 11.10 (oneiric ocelot) and Eclipse 3.6.1 (helios), although this should not play an important role (as it turns out, Eclipse Indigo might need a few tweaks, check the end of this post). Also, setting up Eclipse for programming the Arduino Uno Atmega328P is pretty straightforward. Nevertheless, here is a quick how-to for the Arduino Uno (Arduino Mega 2560 below):

  1. Install avrdude and AVR libraries:
    sudo apt-get install gcc-avr avr-libc avrdude
  2. Start Eclipse and install CDT Plugin (C/C++ Development Tools):
    • Help -> Install New Software…
    • Work with: (your current Eclipse version)
      i.e. “Helios – http://download.eclipse.org/releases/helios”
    • Download the “pending” software package (don’t worry, download starts automatically ๐Ÿ˜‰ )
    • Choose “Programming Languages” and select “C/C++ Development Tools”
    • Accept and continue by restarting Eclipse
  3. Install AVR Eclipse Plugin:
    • Help -> Install New Software…
    • Add new repository: http://avr-eclipse.sourceforge.net/updatesite/
    • Re-download the “pending” software package, download will be faster since it is probably cached ๐Ÿ˜€
    • Download AVR Eclipse Plugin and restart Eclipse
  4. Create new C project named “BlinkBlink”:
    • Project Type AVR Cross Target Application (Empty Project, AVR-GCC Toolchain)
    • Click next…
    • Untick “Debug” (in Debug mode, no hex-files are generated and avrdude can’t flash the device)
    • Click Advanced settings…
    • AVR -> AVRDude -> Programmer configuration…
    • Create a new programmer and name it “Arduino Uno”. Make sure this newly created programmer configuration is selected for the current project.
      • Programmer Hardware: Arduino
      • Override default port: /dev/ttyACM0 or similar
      • Override default baudrate: 115200
    • AVR -> Target Hardware:
      • MCU Type: ATmega328P (or load from MCU)
      • MCU Clock Frequency: 16000000 (default external clock source of Arduino Uno)
  5. Click Apply and OK to leave the properties window and click Finish to create the new project in the workspace.
  6. Create a new source file main.c and copy the contents from this link. Make sure to save main.c before proceeding (File -> Save).
  7. Project -> Build Project
  8. Click on the AVR Button within Eclipse to upload the generated hex file from BlinkBlink/Release/BlinkBlink.hex.
    Your Arduino Uno’s LED should be blinking on and off repeatedly. If somehow it doesn’t work, right-click your Project and select Properties. Make sure all AVR and Programmer settings are active as mentioned above.

For the Arduino Mega 2560 you should choose Atmel STK500 Version 2.x firmware as Programmer Hardware, and ATMega2560 as target hardware, the rest is the same as with the Arduino Uno. Also, if you are using the above source file for testing, you should change the definition of LED from PB5 to PB7, since the LED on the Arduino Mega is on Pin7 of Port B.

Update: if you are using Eclipse Indigo, some specific AVR symbols such as DDRB (data direction register of port b) may not be recognized.
To solve this problem go to preferences…

  • C/C++
  • Language Mappings
  • Add the following mappings:
    • Content Type: C Header File / Language: GNU C
    • Content Type: C Source File / Language: GNU C
  • Make sure to click on Apply afterwards… also you may need to restart eclipse after adding the mappings

Your Language Mapping preference window should look like the following screenshot:

If it still does not work, also try adding this line at the beginning of your main.c source file:
#include <avr/iom128.h>

Creating a Serial to USB driver using the Android USB-Host API

Lately we have received many concerns about using the serial interface on Android (Honeycomb with USB-Host). The way to usually communicate with a USB-Serial device in Linux, is to create a virtual serial interface and use it like a normal serial interface. For instance if you connect an FTDI controller via USB, a device /dev/ttyUSBX will be created.

USB-Serial Connection connects streams to Bulk Endpoints

However, behind the scenes you are actually communicating with the device using plain USB Bulk transfers. This job is done by the USB-Serial driver for you (ftdi_sio for instance). If you like to avoid using the kernel driver for some reason you can do this by using a library based on libusb called “libftdi”.

If you look at the source code you will notice that this library manages the connection (depending on the chip type) to the serial controller. Part of this task for instance is resetting TX/RX or setting the baudrate, which is not entirely trivial as it turns out.

Reading data is pretty simple (if your IN packet returns more than two bytes there is data ready). While when sending data you have to be sure not to write more than the max packet size defined by the controller. For details see the source code of libftdi.

Looking at Android there is a core problem with serial devices. As shown in a previous article it is pretty complicated to get started with building the driver. However, the even more painful part is, that there is no API-featured way to receive permissions for the /dev/USBX device node! So root is required.

But Hey! With libftdi, or it’s implementation it is now possible to write your own driver using the Android USB Host API. In the attached example application a connection to a VNC2 Controller is opened and each seconds one byte is transferred. For the example LEDs on the board are toggled. Using the Bulk OUT EP a rs232 “write” is emulated.

Download the Android source
Note that you should change the VID_PID String according to your device “VVVV:PPPP”.
Also note that if you would like to test if this works on your computer you should first remove the ftdi_sio driver using:
$ sudo rmmod ftdi_sio.

Debugging on your Ubuntu machine:
It’s probably much easier to debug on your workstation. Recently I found pyusb pretty helpful:
$ sudo apt-get install python-usb.
Here is a python script that will get you started: main.py.

Honeycomb Tablet optimized version of the Android Oscilloscope

The ultimate goal of our Bachelors Thesis one year ago was to deliver a “market-ready” version of our oscilloscope project where you can just install an app from the market and connect the oscilloscope over USB.

Before the “Tablet Age” our idea was to set up the OTG port of a phone for host mode. However, this requires rooting the phone as well as adding a voltage supply to the connector. Not a very elegant solution.

When the first tablets with honeycomb arrived (3.0) we managed to use the host port on a rooted device to get our oscilloscope working. Still, root was required.

With Honeycomb 3.1 it was not only possible to acquire permission of the USB device it also became possible to program USB with the Java API rather than to go native with libusb.

Therefore we have written a tablet optimized version of our oscilloscope. With the all new user interface comes the ability to connect to our USB oscilloscope. You can get the application from the Android Market with your 3.0 and up tablet. Get the source code: HoneyOsciPrime.tar.gz, or the APK HoneyOsciPrime.apk. You can always download our Layout Files here and build your own oscilloscope or contact us for support.

Acer A500 running OsciPrime
Screenshot of the new Honeycomb OsciPrime App