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

CueCat meets Arduino, works with HID boot code

CueCat meets Arduino

CueCat meets Arduino

Soon after posting Arduino barcode scanner article I started receiving questions about CueCat. Many of these cat-shaped devices were distributed free of charge in the US at the end of the dot com craze and even though company which developed and distributed them went out of business long time ago, the USB CueCats are still available for very reasonable price. Declawed CueCat with USB interface can be purchased for as little as $13 total in the US and non-modified ones for even less; at the same time, typical no-name handheld barcode scanner goes for around $25 on eBay and will be shipped from China.

I tested “declawed” USB CueCat with my code – it initializes as HID boot keyboard and works very well. No modifications are needed to the sketch from the previous article – just plug in the device and start scanning. CueCat reads many different barcode systems, including UPC and ISBN. Since CueCat is in constant scanning mode (no button needs to be pressed to initiate a scan), it can be used for applications like automated inventory control or as a part of a motion feedback circuit in CNC/robotics project.

One last advantage of CueCat I’d like to point out is low power consumption. As can be seen on a title picture, an Arduino, USB Host Shield, CueCat and HD44780-compatible LCD display can be run from a small LiPo boosted to 5V by Mintyboost from Adafruit Industries with its output connected to Arduino USB port.

CueCat is compact and inexpensive barcode scanner still available for sale despite being discontinued many years ago. It is implemented as USB HID boot device and supported by USB Host library. If you were living in the US in 2000, chances are you already have one or even several of these cat-shaped devices laying around – try it with my code and let me know if you have any issues.


Connecting barcode scanner to Arduino using USB Host Shield

Scanning barcodes using Arduino and USB Host Shield

Scanning barcodes using Arduino and USB Host Shield

An addition of Human Input Device Class support to USB Host Shield library 2.0, announced several days ago allows using powerful and inexpensive input devices with USB interface in Arduino projects. Sample sketches demonstrating sending and receiving data to one of the most useful HID device types – boot keyboard/mouse, has been released along with the library. The beauty of boot protocol lies in the simplicity of device report – a data packet containing information about button presses and mouse movements. However, samples were designed to demonstrate all features of the class and because of that, they are somewhat heavy. In real-life applications, it is often not necessary to implement each and every virtual function – only what is needed. In today’s article I will show practical application of HID boot device building a simple gadget.

Originally, HID boot protocol was meant to be used with keyboards and mice. When USB became popular, other keyboard-emulating devices, such as barcode scanners and magnetic card readers have been migrated from PS/2 standard to USB while keeping their keyboard-emulating property. As a result, many modern “not-so-human” input devices behave exactly like a keyboard including boot protocol support. A gadget that I demonstrate today is portable autonomous barcode scanner built using Arduino board, USB Host shield, handheld USB barcode scanner and LCD display (see title picture). The operation is simple – when handheld scanner button is pressed, it scans the barcode and sends it to Arduino symbol by symbol. Arduino then outputs these symbols on LCD display. LCD is erased before outputting each new barcode by tracking time between arrival of two consecutive symbols. To keep the code simple, I intentionally did not implement any data processing, however, since Arduino sketch for the gadget compiles in just a little over 14K, there is plenty of memory space left for expansion.

Continue reading Connecting barcode scanner to Arduino using USB Host Shield

HID support for USB Host Shield Library 2.0 released

HID r.2.0 released

HID r.2.0 released

I pleased to announce that after a long and difficult development period Human Input Device AKA HID class support has been added to USB Host Shield Library r.2.0 and is available on gitHub – I suggest downloading the whole directory, since some modifications has been also made to core files to accommodate a new class. HID devices include popular devices like keyboards, mice, joysticks, game controllers, bar code scanners, RFID and magnetic card readers, digital scales and UPSes, to name a few.

I previously wrote about interfacing to HID devices here, here, and here. The code examples in these articles were written for legacy USB Host Shield library and can’t be compiled with current revision, however, the basic principles are the same – the device is periodically polled by the host and sends back data block called report containing changes in device controls (buttons, switches, jog dials etc.) since the last poll. Even though different devices have different report formats, for a certain device, report format is stored in the device in data structure called report descriptor. Therefore, it is possible to learn about device controls from the device itself by parsing its report descriptor.

There is one special case where report format is known in advance. Almost all HID keyboards and mice support so-called boot protocol intended for communication to very simple systems like PC configuration screen when computer runs from BIOS. Keyboard boot protocol report consists of 8 bytes containing state of modifier keys (CTRL, SHIFT,etc.) in the first byte, second byte being reserved, and up to 6 key scan codes in the rest of the report. Mouse boot protocol report consists of 3 bytes, first of which contains state of left, right and middle buttons and other 2 store X and Y travel since last poll.

In many cases boot protocol capabilities are more than enough for an Arduino project; for this reason, boot protocol class is the first to be released. To demonstrate operations of this class, 2 simple sketches has been developed, one for mouse, another for keyboard.

Continue reading HID support for USB Host Shield Library 2.0 released

Using HID device for RC control

[ EDIT ] Here is a link to The Tx Project source code [/EDIT]
This is another RC car related post. This time, Santiago Saldana has created a device that allows you to control any RC car,plane,helicopter, etc. using any HID joystick. The “Tx Project” makes use of an Arduino, an Assan Hack Module, a HD44780 LCD, and a USB Host Shield v1.0. The project also makes use of a Trimersion Headset for FPV control of the RC Car, a Microsoft Sidewinder Racing Wheel, and a 900 Mhz Video transmitter / receiver. Code is open source and will be made available after beta testing, or upon request if there is interest. The code makes us of my MAX3421E and USB Host libraries as well as borrowing HID parser code from the LUFA project.

First video is from an earlier version, but demonstrates the devices flexibility in USB device input. The second video is from a presentation Santiago made at Wake Forest University’s Computer Science department.

Controlling robotic arm with Arduino and USB mouse

Many people asked me to post a video showing an arm from inverse kinematics article in action. While making a video, I realized that shots of the arm following a pattern of computer-generated coordinates is going to be less than exciting and decided to add manual control. The video below shows the result. In addition to the video, a HID introductory page has been written describing HID communication basics as well as some simple Arduino code. Enjoy! ( Youtube link, where HD quality video can be selected ).

Arduino sketch written for this video is on gitHub.

How to drive USB keyboard from Arduino

USB Host Shield driving a keyboard

USB Host Shield driving a keyboard

I am continuing on topic of using USB Host shield to drive USB peripherals started in the previous article. The code presented here performs keyboard polls, character translation and management of “LOCK” keys – NumLock, CAPSlock and ScrollLock.

A keyboard is hard to use if results of typing can’t bee seen, so my first step was to add an output device. Even though HD44780-compatible character LCD can be driven with Arduino pins, I wrote a little library which uses GPOUT port of MAX3421E for LCD control. This library is now a part of USB Host Shield repo on github. I used LCD high-level routines from official Arduino distro and developed low-level functions specific to MAX3421E hardware, as well as LCD initialization. Since function syntax and behaviour (and source code ) is identical to official LCD functions, the user manual is already written and can be found in the library reference by title LiquidCrystal. The only difference is in the constructor; since only 4-bit mode is possible and only one pinout is supported, Max_LCD constructor is not accepting any parameters. The pinout of LCD connections to GPOUT is given in the Max_LCD.h file; see also title image of this article.

Continue reading How to drive USB keyboard from Arduino

Arduino USB Host – Peripherals.

USB constructor

USB constructor

The time has come to start using the code to communicate to peripheral devices. There are many devices which work well with microcontrollers. HID devices, such as keyboards, joysticks, and Radio Controller look-a-likes are useful and not too difficult to implement. Another good application of embedded USB host is digital camera control. Communication using Bluetooth and WiFi peripherals are also a possibility.

Today I’m going to show how to communicate with USB keyboard. Standard USB keyboard belongs to a class of devices, called HID (Human Interface Device). The format of data generated by HID device is quite flexible and each device stores it’s data definitions in structures called Report descriptor and Physical descriptor. Generally, to work with the device, report descriptor has to be retrieved and parsed. Because of HID flexibility, a universal HID driver will take many many lines of code. Luckily for us, if all we need is to talk to the keyboard, there is an easier way.

Continue reading Arduino USB Host – Peripherals.