🛠 Périphérique composite CDC + HID et bootloader Caterina
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
- 1 Arduino Leonardo + micro-USB cable
- A second USB port or hub useful for recovery in case of a runaway sketch
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
- Upload runaway: A sketch that crashes or hangs at startup can reclaim the USB bus at every reconnection, preventing any new upload. Recovery: press the reset button and start the upload within the bootloader's ~8-second window, or hold reset while plugging in the cable. The previous sketch continues to run upon reconnection because the bootloader is skipped at power-on.
- Manually triggering the bootloader: Opening the port at 1200 baud (the "touch") forces Caterina mode (PID 0x0036, versus 0x8036 in normal operation); the IDE does this automatically, a standard serial monitor does not.
- Modified fuses: The original values are low 0xFF, high 0xD8, extended 0xCB; erasing them removes Caterina — you will need to reprogram via ICSP.
- Ignored enumeration sequence: The COM port disappears/reappears during upload; scripting a reconnection is necessary for automation.
- Runaway HID: Never flood the host with keystrokes without condition; keep the CDC open to cut off remotely.
Other tutorials for this board
- Faire clignoter la LED intégrée (broche 13)
- Bouton et console série : Serial (USB) vs Serial1 (broches 0/1)
- Lire un capteur analogique : potentiomètre et ADC 10 bits
- Clavier USB : taper du texte avec la bibliothèque Keyboard
- Souris USB : déplacer le curseur avec la bibliothèque Mouse
- Asservir un servo moteur avec un potentiomètre (PWM)
Code examples for this board
clignotante.inoArduinobouton-led.inoArduinoclavier-auto-tape.inoArduinosouris-cercle.inoArduinotelecommande-media.inoArduinobouton-clic-souris.inoArduino