USB barcode communication with Arduino Mega ADK

Hi everyone!

I’m working on a project and I really don’t know how to solve this problem:

I am using and Arduino Mega ADK (i.e with an integrated Max3421e and USB port). I want it to communicate with an USB barcode scanner. I therefore downloaded the USB hostshield library from github and tried to adapt it for the Arduino ADK as suggested at the bottom of this page:

Then I used the example code which comes with the library. However it doesn’t work fine since I keep getting error messages of this type:

"“Never include libraryx[/b].h directly; include Usb.h instead” , where libraryx[/b].h are all libraries from the USB hostshield library.
Moreover I get an error message which tells me that I can’t compile my code on the Arduino ADK (unfortunately I don’t remember the exact sentence now: if you want I’ll check ASAP).
Does anyone know what I’m missing? Would he/she be so kind to help me please? :slight_smile:
Thank you!

Hello marshalld!

I'm also considering doing a project with a barcode scanner, which model are you trying to use?

To OP, what version of Arduino were you using? I suggest using Arduino 1.0.5 to compile the code. Give it a try.


#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 pins
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
USB     Usb;
USBHub     Hub(&Usb);
HIDUniversal Hid(&Usb);
class KbdRptParser : public KeyboardReportParser
  void PrintKey(uint8_t mod, uint8_t key);
  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)
/* what to do when symbol arrives */
void KbdRptParser::OnKeyPressed(uint8_t key)  
static uint32_t next_time = 0;      //watchdog
static uint8_t current_cursor = 0;  //tracks current cursor position  
    if( millis() > next_time ) {
      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.print( (char)key );
    lcd.print( (char)key );
KbdRptParser Prs;
void setup()
    Serial.begin( 115200 );
    if (Usb.Init() == -1) {
        Serial.println("OSC did not start.");
    delay( 200 );
    Hid.SetReportParser(0, (HIDReportParser*)&Prs);
    // set up the LCD's number of columns and rows: 
    lcd.begin(DISPLAY_WIDTH, 2);
    delay( 200 );
void loop()

I solved the problem following a youtube tutorial and an advise I found here on the forum. To sum up, here is what I’ve done:

-I added the #include<hid.h> #include<hiduniversal.h> statements at the beginning of the code (for me, hid.h library was not included in the USB hostshield library)
-I commented al libraries which produced errors in the dialogue box
-I added these two lines: a) USBHub Hub(&Usb); and **b)**HIDUniversal Hid(&Usb);
-I added USB_ to the line below to keep up with a library update
-I added (char) to the serial print and lcd print functions

Now I’m dealing with another problem: I want to store my barcode into an array but my code doesn’t work. Here it is:

uint8_t key_all[]={0,0,0,0,0,0,0,0,0,0,0,0,0};
int i;

    //crea array
    if (i<12) {
      key_all[i]= key;

    else if (i==12) {
      int t=0;
      for(t=0; t<13;t++){
      Serial.print( (char)key_all[t]);

I have added it at the end of the void KbdRptParser::OnKeyPressed(uint8_t key) function.
I guess this is a pretty basic question but it’s a few hours that I’m stuck with it :confused: