🛠 Utiliser la Due en hôte USB : lire un clavier sur le port Native

Arduino Due · avance · Schéma : IMG-due-avance-2-usb-host-clavier-usbhost.svg — clavier USB branché via un hub alimenté sur le connecteur micro-USB Native de l'Arduino Due, bibliothèque USBHost annotée, moniteur série affichant les touches reçues.

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

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

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials