Discussion about connecting a USB barcode scanner to an Arduino Mega 2560 ADK and reading barcodes via the built-in USB port

I am using an Arduino Mega 2560 ADK, which has a built-in USB host controller (MAX3421E), to connect a USB barcode scanner.

Here is a picture of the board I am using: Arduino Mega 2560 ADK. It indeed has a built-in USB host controller (MAX3421E):

My goal is to read barcodes from the scanner and display them on the Serial Monitor, as it is essential for my project to interface it through the Arduino Mega 2560 ADK's built-in USB port. I have connected an external 9V power supply to ensure sufficient power. However, I encounter an issue where the initialization of the USB host controller sometimes fails with the error: "OSC did not start." Even when the initialization passes, I do not see any output from the barcode scanner. I have included my code below and would appreciate any guidance or sample code to successfully read barcodes using the built-in USB port on the Arduino Mega 2560 ADK.

#include <hid.h>
#include <usbhub.h>
#include <hiduniversal.h>
#include <hidboot.h>
// Satisfy IDE, which only needs to see the include statment in the ino.
#ifdef dobogusinclude
#include <spi4teensy3.h>
#include <SPI.h>
#endif

char scan;

class MyParser : public HIDReportParser {
  public:
    MyParser();
    void Parse(HID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf);
  protected:
    uint8_t KeyToAscii(bool upper, uint8_t mod, uint8_t key);
    virtual void OnKeyScanned(bool upper, uint8_t mod, uint8_t key);
    virtual void OnScanFinished();
};

MyParser::MyParser() {}

void MyParser::Parse(HID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf) {
  if (buf[2] == 1 || buf[2] == 0) return;

  for (uint8_t i = 7; i >= 2; i--) {
    if (buf[i] == 0) continue;

    if (buf[i] == UHS_HID_BOOT_KEY_ENTER) {
      OnScanFinished();
    }

    else {
      OnKeyScanned(i > 2, buf, buf[i]);
    }

    return;
  }
}

uint8_t MyParser::KeyToAscii(bool upper, uint8_t mod, uint8_t key) {
  if (VALUE_WITHIN(key, 0x04, 0x1d)) {
    if (upper) return (key - 4 + 'A');
    else return (key - 4 + 'a');
  }

  else if (VALUE_WITHIN(key, 0x1e, 0x27)) {
    return ((key == UHS_HID_BOOT_KEY_ZERO) ? '0' : key - 0x1e + '1');
  }

  return 0;
}

void MyParser::OnKeyScanned(bool upper, uint8_t mod, uint8_t key) {
  uint8_t ascii = KeyToAscii(upper, mod, key);
  Serial.print((char)ascii);
  scan = (char)ascii;

}


void MyParser::OnScanFinished() {

  Serial.println(" - Finished");

}

USB          Usb;
USBHub       Hub(&Usb);
HIDUniversal Hid(&Usb);
MyParser     Parser;

void setup() {
  Serial.begin( 9600 );
  Serial.println("Start");

  while (Usb.Init() == -1) {
    Serial.println("OSC did not start.");
    delay( 1000 );
  }

  delay( 200 );

  Hid.SetReportParser(0, &Parser);
}

void loop() {

  Usb.Task();
}

Any help or suggestions on how to resolve the initialization issue or improve my code to successfully read barcodes would be greatly appreciated. Thank you!

Please show the link to your board. Original Mega doesn't contains USB host controller .

You don't need an Arduino for it, you can connect the Barcode reader directly to PC

Board Information: I have added the picture of the board(Arduino Mega 2560 ADK) I am using to the post. It indeed has a built-in USB host controller (MAX3421E).

Project Requirement: Connecting the barcode reader directly to a PC is not an option as it is essential for my project to interface it through the Arduino Mega 2560 ADK's built-in USB port.

I have already found the solution. Arduino IDE is identifying my board as "Arduino Mega or Mega 2560", however, it is "Arduino Mega ADK".

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.