esp32-p4-hydra-usb-host-hid.ino

Adafruit Metro ESP32-P4 Hydra · File : esp32-p4-hydra-usb-host-hid.ino · Language : C++ (Arduino) · Use case : Détecter un clavier USB connecté au port USB-A hôte et afficher les touches.

Turns the board into a USB host to read inputs from a standard keyboard plugged into the USB-A port.

Plug a standard USB keyboard into the board's USB-A port. The code uses the USBHIDKeyboard library. The USB-A port provides the necessary 5V power. The P4's USB controller handles High-Speed negotiation. Pressed keys are displayed on the serial monitor.

esp32-p4-hydra-usb-host-hid.ino

#include <Arduino.h>
#include <USB.h>
#include <USBHIDKeyboard.h>

// La Hydra possède un port USB-A Host High-Speed.
// L'ESP32-P4 supporte le mode USB Host via son contrôleur USB OTG.
// Cette bibliothèque nécessite un core Arduino récent avec support USB Host complet.

USBHIDKeyboard keyboard;

void setup() {
  Serial.begin(115200);
  Serial.println("Metro ESP32-P4 Hydra: USB Host HID Keyboard");

  if (!USB.begin()) {
    Serial.println("USB Host Failed to start");
    return;
  }
  
  keyboard.begin();
  Serial.println("Waiting for USB Keyboard...");
}

void loop() {
  USBTask(); // Nécessaire pour traiter les événements USB Host

  if (keyboard.available()) {
    char c = keyboard.read();
    Serial.print(c);
  }
}

← Back to the board