🛠 HID host : clavier, souris et scanner sur le port USB-A
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
- 1 × Arduino Mega ADK + external power supply 7–12 V 1.5 A recommended (a backlit keyboard may draw more than 100 mA; the port's polyfuse limits current to 500 mA)
- 1 × simple wired USB keyboard (wireless keyboards with proprietary 2.4 GHz dongles are not standard HID devices; Bluetooth class devices on the host port were already experimental in 2012)
- 1 × USB HID barcode reader in keyboard emulation mode (alternative to a keyboard)
- Library: USB Host Shield 2.0 (Library Manager menus > USB Host Shield 2.0 > HID > HIDKeyboard)
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
- 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).
- Forgetting Usb.Task in loop: without it, no HID report is ever received.
- Neglecting device current draw: backlit keyboard + phone on the same board powered via USB = exceeding the 500 mA limit, causing the polyfuse to trip.
- 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.
- Expecting ASCII text: the HID report provides key codes (HID usage table), not characters — translating to AZERTY/QWERTY is up to you.
- 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
clignotante.inoArduinobouton-led.inoArduinoscan-hote-usb.inoArduinoadk-chat-android.inoArduinoadk-telemetrie.inoArduinohote-clavier-usb.inoArduino