🛠 Utiliser la Due en hôte USB : lire un clavier sur le port Native
Objectives
The Due's Native port is driven directly by the SAM3X and is OTG-capable: the board can act as a USB HOST — the official "Keyboard Controller" tutorial leverages this with the USBHost library. You will connect a standard USB keyboard, intercept key presses, and relay them to the serial port. This is the foundation for turning the Due into a control panel, HID device reader, or keystroke logger.
Hardware
- 1 Arduino Due
- 1 USB keyboard (standard HID, no exotic active hub)
- 1 USB-A female → micro-USB male adapter ("OTG" cable) to connect the keyboard to the Native port; a powered USB hub is often more practical as it provides the 5 V required by the keyboard
- The Programming port remains free for the serial monitor (or use SerialUSB if the host is elsewhere)
- Library: USBHost (Arduino), to be installed via the library manager
Complete Code
// Hote USB : clavier HID -> moniteur serie (inspiré du tuto officiel
// docs.arduino.cc/tutorials/due/keyboard-controller)
#include <Keyboard.h>
USBKeyboard Keyboard;
uint8_t addr = 0;
void setup {
Serial.begin(9600); // via le port Programming (16U2)
while (!Serial) { }
Keyboard.begin; // la Due devient hote USB sur le port Native
}
void loop {
Keyboard.Task; // machine a etat de l'hote USB
if (Keyboard.getConnected(addr)) {
while (Keyboard.keyAvailable) {
Keyboard.key_t k;
if (Keyboard.getKey(k)) {
Serial.print("touche: ");
Serial.print(k.key);
Serial.print(" etat: ");
Serial.println(k.state == KEY_PRESSED ? "appuyee" : "relachee");
}
}
}
}
Expected Verification
Connect the keyboard to the Native port (via the adapter/powered hub): the Programming port monitor displays each key press/release. Unplugging/replugging the keyboard can be done hot, as the Task state machine handles enumeration and disconnection. Uploading continues to work via the Programming port while the Native port acts as the host.
Common Errors
- Plugging the keyboard into the Programming port: this port is a serial peripheral (16U2 bridge), it CANNOT act as a host; only the Native port (OTG) can.
- Power-hungry keyboard without a powered hub: the 5 V available via a simple passive adapter is limited; backlit keyboards may refuse to enumerate — hence the need for a powered hub.
- Confusing
SerialandSerialUSBin this setup: the Native port is busy acting as a host, so debugging must go throughSerial(Programming port). - Forgetting
Keyboard.Taskin loop: without this regular call, USB enumeration does not progress and the keyboard remains "not connected". - Using a multimedia/proprietary HID keyboard: the library handles standard HID keyboards; special keys may remain silent.
- Expecting the program to run without a powered USB host: in standalone configuration, ensure 5 V is provided on the hub side, otherwise the keyboard will not power on.
- Be mindful of micro-USB connector ergonomics: low-quality USB-A female adapters can cause poor contact — test with a known working keyboard.
- Resetting the board breaks the ongoing enumeration: reconnecting the keyboard after a reset is normal.
Other tutorials for this board
- Faire clignoter la LED de l'Arduino Due et apprivoiser ses deux ports USB
- Lire un potentiomètre avec le vrai ADC 12 bits de la Due
- Allumer une LED externe en respectant les broches 3,3 V et 9 mA de la Due
- Générer une vraie tension analogique avec les DAC 12 bits (broches 66 et 67)
- Dialoguer avec un GPS (ou tout module TTL) sur Serial1 sans bloquer la boucle
- Scanner les deux bus I2C de la Due (Wire sur 20/21, Wire1 sur 70/71)
Code examples for this board
clignotante.inoArduinobouton-led.inoArduinodac-onde.inoArduinoadc-12-bits.inoArduinopwm-12-bits.inoArduinogps-serial1.inoArduino