🛠 Périphérique composite CDC + HID et bootloader Caterina

Arduino Leonardo — Board Fiche · avance · Schéma : IMG-leonardo-avance-8-leonardo-cdc-hid-simultanes.svg — Câble USB de la Leonardo se dédoublant en deux flux étiquetés « Clavier HID » et « Port série CDC » entrant dans un ordinateur, avec la timeline du bootloader Caterina (fenêtre 8 s)

Objectives

Build a composite USB device: the Leonardo simultaneously exposes a CDC serial port (debugging) and a HID keyboard (output execution), thanks to the open HID class in the Arduino core (HID.cpp in ArduinoCore-avr). You will then master the Caterina bootloader: AVR109 protocol over USB CDC, detection via a 1200 baud "touch", an 8-second window, and recovering from a sketch that monopolizes the USB bus at every startup (runaway).

Hardware

Complete Code

// Peripherique composite : CDC (Serial) + clavier HID simultanes
#include <Keyboard.h>

void setup {
  Serial.begin(9600);   // interface CDC : le port serie visible par le PC
  Keyboard.begin;     // interface HID : la carte devient clavier
  delay(3000);          // garde-foc : laisser le temps de placer le curseur
  Serial.println(F("Tapez une ligne : elle sera frappee au clavier."));
}

void loop {
  if (Serial.available > 0) {
    String ligne = Serial.readStringUntil('\n');
    ligne.trim;
    if (ligne.length > 0) {
      Keyboard.print(ligne);   // sortie HID vers l'hote
      Keyboard.releaseAll;
      Serial.println(F("Frappe terminee.")); // echo CDC
    }
  }
}

Expected Verification

After uploading, the computer sees both a keyboard device and a serial port. Place the cursor in an editor, type a line in the serial monitor: the Leonardo "retypes" it into the focused application. The composite descriptor is automatically selected by the core as soon as a USB library (Keyboard/Mouse) is included in addition to Serial.

Common Errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials