image 19

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 a USB interface in Arduino projects.

Sample sketches demonstrating sending and receiving data to one of the most useful HID device types – boot keyboard/mouse, have been released along with the library.

The beauty of boot protocol lies in the simplicity of the 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.

Want to connect barcode scanner to Arduino using USB Host Shield?

How To Connect Barcode Scanner To Arduino Using USB Host Shield?
How To Connect Barcode Scanner To Arduino Using USB Host Shield?

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 the practical application of HID boot devices in building a simple gadget.

Related Article:  https://www.circuitsathome.com/best-usb-powered-monitor/USB Powered Monitor

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 properties.

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 a portable autonomous barcode scanner built using an Arduino board, USB Host shield, handheld USB barcode scanner, and LCD display (see title picture).

The operation is simple – when the handheld scanner button is pressed, it scans the barcode and sends it to Arduino symbol by symbol. Arduino then outputs these symbols on an LCD display.

LCD is erased before outputting each new barcode by tracking the time between the arrival of two consecutive symbols. To keep the code simple, I intentionally did not implement any data processing, however, since the Arduino sketch for the gadget compiles in just a little over 14K, there is plenty of memory space left for expansion.

See also  What Is RG6 Coaxial Cable? [The Most Detailed Guide]

Let’s talk a little bit about the necessary parts. First of all, we’ll need an Arduino board and USB Host Shield. I use a standard 16×2 HD44780-compatible LCD display, connected similarly to one in Arduino LiquidCrystal tutorial.

Since the USB Host shield uses pins 11 and 12 to interface to Arduino, I had to move corresponding LCD signals to pins 6 and 7; the rest of the connections shall be made exactly like in the tutorial. Lastly, we need a compatible handheld barcode scanner.

The best place to search for one is eBay; look for the phrases “no driver required” and “USB HID device” in the product description. To make sure a device is indeed a boot keyboard, take a look at device descriptors using the USB_Desc example from the USB Host library – a sample output is given below.

Class, Subclass, Protocol in interface descriptor (lines 29-31) should be 03, 01, 01. If Subclass is zero, boot protocol is not supported. The non-boot scanner is still useful but accessing it is going to be slightly more complicated. I will cover HID report protocol in one of the next articles.

image 18
Connect Barcode Scanner To Arduino
123456789101112131415161718192021222324252627282930313233343536373839404142Device descriptor:Descriptor Length:      12Descriptor type:        01USB version:            0100Device class:           00Device Subclass:        00Device Protocol:        00Max.packet size:        08Vendor  ID:             04B4Product ID:             0100Revision ID:            0100Mfg.string index:       01Prod.string index:      02Serial number index:    00Number of conf.:        01 Configuration descriptor:Total length:           0022Num.intf:               01Conf.value:             01Conf.string:            04Attr.:                  A0Max.pwr:                64 Interface descriptor:Intf.number:            00Alt.:                   00Endpoints:              01Intf. Class:            03Intf. Subclass:         01Intf. Protocol:         01Intf.string:            05Unknown descriptor:Length:         09Type:           21Contents:       00011001223F000705 Endpoint descriptor:Endpoint address:       81Attr.:                  03Max.pkt size:           0008Polling interval:       0A

Below is the sketch which needs to be compiled and loaded into Arduino. I wrote it by copying the keyboard example, taking out all unnecessary code, and adding LCD support.

See also  How Does A Propane Refrigerator Work? [In-depth Guide]

The sketch can be copied from this page and pasted to the Arduino IDE window. An explanation of key pieces is given after the listing.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110/* Portable barcode scanner. Uses USB HID barcode scanner, Arduino Board, USB Host Shield and HD44780-compatible LCD display   The circuit: * LCD RS pin to digital pin 7 * LCD Enable pin to digital pin 6 * LCD D4 pin to digital pin 5 * LCD D5 pin to digital pin 4 * LCD D6 pin to digital pin 3 * LCD D7 pin to digital pin 2 * LCD R/W pin to ground * 10K resistor: * ends to +5V and ground * wiper to LCD VO pin (pin 3) */ #include <LiquidCrystal.h>#include <avr/pgmspace.h> #include <avrpins.h>#include <max3421e.h>#include <usbhost.h>#include <usb_ch9.h>#include <Usb.h>#include <usbhub.h>#include <avr/pgmspace.h>#include <address.h>#include <hidboot.h> #include <printhex.h>#include <message.h>#include <hexdump.h>#include <parsetools.h> #define DISPLAY_WIDTH 16 // initialize the LCD library with the numbers of the interface pinsLiquidCrystal lcd(7, 6, 5, 4, 3, 2); USB     Usb;//USBHub     Hub(&Usb);HIDBoot<HID_PROTOCOL_KEYBOARD>    Keyboard(&Usb); class KbdRptParser : public KeyboardReportParser{ protected: virtual void OnKeyDown (uint8_t mod, uint8_t key); virtual void OnKeyPressed(uint8_t key);}; void KbdRptParser::OnKeyDown(uint8_t mod, uint8_t key) {    uint8_t c = OemToAscii(mod, key);     if (c)        OnKeyPressed(c);} /* what to do when symbol arrives */void KbdRptParser::OnKeyPressed(uint8_t key) {static uint32_t next_time = 0;      //watchdogstatic uint8_t current_cursor = 0;  //tracks current cursor position       if( millis() > next_time ) {      lcd.clear();      current_cursor = 0;      delay( 5 );  //LCD-specific       lcd.setCursor( 0,0 );    }//if( millis() > next_time …     next_time = millis() + 200;  //reset watchdog     if( current_cursor++ == ( DISPLAY_WIDTH + 1 )) {  //switch to second line if cursor outside the screen      lcd.setCursor( 0,1 );    }     Serial.println( key );    lcd.print( key );}; KbdRptParser Prs; void setup(){    Serial.begin( 115200 );    Serial.println(“Start”);     if (Usb.Init() == -1) {        Serial.println(“OSC did not start.”);    }     delay( 200 );     Keyboard.SetReportParser(0, (HIDReportParser*)&Prs);    // set up the LCD’s number of columns and rows:     lcd.begin(DISPLAY_WIDTH, 2);    lcd.clear();    lcd.noAutoscroll();    lcd.print(“Ready”);    delay( 200 );} void loop(){  Usb.Task();}
  • Lines 5-15. This comment contains information about necessary LCD connections.
  • Lines 46-52. KeyboardReportParser declaration. Compare with keyboard example. Here, we only need OnKeyDown() and OnKeyPressed() methods.
  • Lines 54-60. OnKeyDown() callback definition. When a keyboard key is pressed, it receives the scan code of this key plus modifiers (Ctrl, Alt, Shift). Since our LCD takes ASCII codes the callback performs scan code to ASCII conversion and then calls OnKeyPressed()
  • Line 63. OnKeyPressed() definition
  • Lines 68-73. Clears the display before outputting the first symbol of the new barcode, i.e., if the pause between symbols is larger than 200ms. Note delay in line 71 – my display is old and slow and unless I wait for a little, it won’t print the first symbol of the sequence. Newer displays should work fine without it
  • Lines 81-82. The output. A symbol is sent to the serial terminal and LCD
See also  What Is A Neutral Wire And How Does It Work? (Find Out Here Now!)

The rest of the sketch contains standard initialization of USB Host and Liquid crystal libraries, as well as periodic calling of USB.Task(), from which keyboard callbacks are called.

Reading RFID tags with Arduino/USB Host Shield
Reading RFID tags with Arduino/USB Host Shield

Lastly, bonus material for those who managed to keep reading the article to this place. Since the sketch assumes HID boot device, it can be used with many different devices.

For example, it is possible to produce an output using the ordinary keyboard (due to watchdod, in order to get more than one symbol on the LCD display, you’d have to type pretty fast). Also, the same setup makes a pretty good RFID reader – many inexpensive USB readers in the 125KHz class are implemented as HID boot keyboards.

The picture on the left shows one such device connected to Arduino. It also shows a way of providing power via Arduino USB port using a portable USB charger. RFID cloning, anyone?

Developing an Arduino project with BarCode Scanner + Arduino USB Shield

In this test, I used the code that Olegon Mazurov kindly shows us on their website www.circuitsathome.com and I made some modifications because it did not work well for my barcode scanner “Datalogic QuickScan” and I could not find more information on the web about the use of barcode readers with the USB port connected to a separate host as the Arduino USB Host Shield.

I built the following circuit:

The code from Oleg Mazurov is as follows, with modifications to work with my barcode scanner and other different barcode scanners:

Bar Code Scanner + Arduino Host Shield code

It is necessary to take Oleg Mazurov’s advice into account when verifying the parameters of our device, which identify the CLASS and communication PROTOCOL as follows:

Intf. Class:            03
Intf. Subclass:         01
Intf. Protocol:         01

If your code reader does not match the above information after verification, it is possible that you will not be able to implement the code that we work on here; therefore, it is critical to check with your example of USB Host Shield 2.0 library called “USB desc.”

To see the USB desc example in the example menu, you must first place the USB Host Shield 2.0 library in the Arduino software’s libraries folder.

Oleghe Mazurov’s code essentially consisted of taking the communication code of a USB keyboard, modifying the barcode reader, and adding LCD control code. The example for controlling a USB keyboard can also be found in the USB Host Shield library 2.0 under the name “USBHIDBootKbd.”

Developing an Arduino project with BarCode Scanner + Arduino USB Shield
Developing an Arduino project with BarCode Scanner + Arduino USB Shield

FAQs On How To Connect Barcode Scanner To Arduino?

Can the barcode reader connect to a USB port?

You can use a USB barcode scanner with the Loyverse POS app if your Android smartphone or tablet supports USB on the go or OTG. A USB OTG adapter or cable is required to use a USB scanner with Loyverse POS on an Android device. On one end, it has a female USB connector and on the other, a male micro USB connector.

What is a USB host shield?

The USB Host Shield includes all of the digital logic and analog circuitry required to build a full-speed USB peripheral/host controller with your Arduino. This means you can use your Arduino to communicate with and control any USB slave device, including thumb drives, digital cameras, Bluetooth dongles, and much more!

Which sensor is used in the barcode scanner?

This method employs a CCD (Charge Coupled Device) semiconductor device, which converts light signals into electric signals. Light is built into the CCD method bar code scanner. When a scanner shines this light on a bar code, the reflection is captured by a CCD and read.

Similar Posts