hote-clavier-usb.ino

Arduino Mega ADK (Rev3) · File : hote-clavier-usb.ino · Language : Arduino · Use case : Lire un clavier USB branché sur le port USB-A de la carte

A real USB keyboard plugged into the USB-A port: the boot HID class is decoded by the library, and every keypress appears on the serial monitor. The Mega ADK becomes a USB input router for old hardware.

Hardware required

An Arduino Mega ADK Rev3 (ATmega2560 + MAX3421E USB host chip with a Type-A port; digital pin 7 is reserved for the MAX3421E reset). 1 USB keyboard (plain wired, no hub).

Wiring

Keyboard straight on the USB-A port; D7 untouched.

How it works

Usb.Task drives enumeration; the boot HID parser calls OnKeyDown on each key frame received in low-speed. Select « Arduino Mega ADK » in the IDE. For USB host/ADK work, install the USB Host Shield 2.0 library; do not use D7. The USB-A port powers the accessory at 5 V.

Going further

Forward keys to a second Arduino (UART) to log a workstation, or trigger macros.

hote-clavier-usb.ino

// La Mega ADK lit un clavier USB (HID boot) via USB Host Shield 2.0.
#include <Max3421e.h>
#include <Usb.h>
#include <hidboot.h>
#include <usbhub.h>

USB Usb;
HIDBoot<USB_HID_PROTOCOL_KEYBOARD> HidKeyboard(&Usb);

class MonClavier : public KeyboardReportParser {
  void OnKeyDown(uint8_t mod, uint8_t key) override {
    Serial.print(F("Touche 0x")); Serial.println(key, HEX);
  }
};
MonClavier klavier;

void setup() {
  Serial.begin(115200);
  if (Usb.Init() == -1) { Serial.println(F("MAX3421E KO")); while (1); }
  klavier.SetReportParser(0, &klavier);
  HidKeyboard.SetReportParser(0, &klavier);
  Serial.println(F("Branchez un clavier sur le port USB-A"));
}

void loop() { Usb.Task(); }

← Back to the board