🛠 HID host : clavier, souris et scanner sur le port USB-A

Arduino Mega ADK (Rev3) · avance · Schéma : IMG-mega-adk-avance-2-mega-adk-hid-host.svg — Clavier USB et lecteur de code-barres branchés sur le port USB-A host de la Mega ADK, sortie sur la console série

Objectives

Use the Mega ADK as a true standalone USB host, without Android: the official board page highlights interfacing "with keyboard, mouse and games controllers as Wiimote and PS3", and users in 2024 are reading USB barcode readers via the integrated port. A HID keyboard plugs directly into the USB-A port; the maintained USB Host Shield 2.0 library provides the HID classes. This is the board's second life: an AVR controller with an integrated USB host port, the only one of its generation.

Hardware

Full Code

// mega-adk-hid-clavier.ino — Board: "Arduino Mega ADK"
// D'après l'exemple HIDKeyboard de USB Host Shield 2.0 (API à confirmer
// avec la version installée).
#include <Usb.h>
#include <hid.h>
#include <hidkeyboard.h>

USB Usb;
USBHID HID;
HIDKeyboard Keyboard(&HID);

void setup {
  Serial.begin(115200);
  if (Usb.Init == -1) { Serial.println(F("OSCOKIRQ failed to assert")); while (1); }
  if (HID.Init != 0)  { Serial.println(F("Init HID impossible"));        while (1); }
  if (Keyboard.Init(&HID) != 0) { Serial.println(F("Clavier non reconnu")); }
  Serial.println(F("Branchez/frappez sur le clavier USB-A..."));
}

void loop {
  Usb.Task;
  if (!Keyboard.isOK) return;

  uint8_t touche[6];
  // lit le rapport de touche HID (6 touches simultanées max)
  if (Keyboard.getReport == 0 && Keyboard.NakPoll) {
    Keyboard.getKeyReport(touche);
    for (int i = 0; i < 6; i++) {
      if (touche[i]) {
        Serial.print(F("code HID 0x"));
        Serial.println(touche[i], HEX);
      }
    }
  }
}

Expected Verification

After plugging the keyboard into the USB-A port, the library enumerates the device (the keyboard's NumLock LED is often lit by the HID reset); each keystroke prints the key codes to the serial console. With a barcode reader in keyboard emulation mode, each scan produces the sequence of keys corresponding to the barcode followed by Enter. No phone is involved: the board works on its own.

Common Errors

  1. Wireless keyboard with proprietary dongle or "Bluetooth class" device: not recognized as standard HID — the host port expects a wired HID device (or a dongle declaring itself as HID).
  2. Forgetting Usb.Task in loop: without it, no HID report is ever received.
  3. Neglecting device current draw: backlit keyboard + phone on the same board powered via USB = exceeding the 500 mA limit, causing the polyfuse to trip.
  4. Compiling with the wrong board variant: the MAX3421E pins on the ADK differ from the standard shield (SS PB4 on shield, different on ADK) — select BOARD_MEGA_ADK or the appropriate menu option.
  5. Expecting ASCII text: the HID report provides key codes (HID usage table), not characters — translating to AZERTY/QWERTY is up to you.
  6. Mixing with other SPI devices on the bus: the MAX3421E is permanently present; any other SPI master must properly deselect it during its transfers.

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials