🛠 Transformer l'Esplora en clavier USB (HID)
Objectives
Leverage the Esplora's signature: like the Leonardo, the ATmega32u4 integrates the USB transceiver and can enumerate as a true HID device. With the Keyboard library, the board becomes a keyboard recognized by any computer without drivers. You will send letters using the diamond buttons and text using the joystick button.
Hardware
- Arduino Esplora + micro-USB cable.
- Esplora and Keyboard libraries (provided with the
arduino:avrcore). - A text editor open on the PC for testing.
Complete code
/* Esplora -> clavier USB HID. Attention (recommandation officielle pour la
classe Leonardo) : testez d'abord une version Serial, un code HID fou
peut prendre le controle du poste de travail ! */
#include <Esplora.h>
#include <Keyboard.h>
void setup {
Keyboard.begin;
Serial.begin(9600); // le CDC cohabite avec le HID sur le 32U4
}
void loop {
if (Esplora.readButton(SWITCH_LEFT) == PRESSED) Keyboard.write('A');
if (Esplora.readButton(SWITCH_DOWN) == PRESSED) Keyboard.write('S');
if (Esplora.readButton(SWITCH_UP) == PRESSED) Keyboard.write('D');
if (Esplora.readButton(SWITCH_RIGHT) == PRESSED) Keyboard.write('F');
if (Esplora.readButton(SWITCH_JOYSTICK) == PRESSED) {
Keyboard.print("Esplora HID depuis l'ATmega32u4 !");
Keyboard.write(KEY_RETURN);
delay(500); // anti-rebond pour une phrase complete
}
// Combinaison : bouton joystick + slider = volume (media key)
if (Esplora.readSlider > 900) {
Keyboard.write(KEY_MEDIA_VOLUME_UP);
delay(200);
}
delay(50);
}
Expected verification
Open a notepad: each press of a diamond button types A, S, D, or F. The central joystick button writes the full sentence followed by an Enter. Pushing the slider to the maximum increases the PC volume (multimedia key). The Device Manager shows a "HID Keyboard Device" connected, with no installation required.
Common errors
- Going HID without a safety net: a sketch that sends keystrokes in a loop can make the PC unusable. Plan for a strict condition (button pressed) and know how to recover the board: Reset then upload within the ~8 s window of the Caterina bootloader, or trigger by opening the port at 1200 bauds.
- Forgetting
Keyboard.begin: without it, nothing is output. - Expecting a real joystick: the Keyboard library is a keyboard; for analog axes in HID, you need the Mouse library (see the next tutorial) or a custom HID descriptor (advanced level).
- Bounce effect: without debouncing, a single press may produce multiple letters; add state tracking (RELEASED→PRESSED) rather than a simple
delayif you want precision. - Looking for the missing serial port: CDC coexists with HID on the same USB enumeration; if you redefine the descriptors yourself, you may lose the serial monitor — only do so with full awareness.
Other tutorials for this board
Code examples for this board
joystick-couleur.inoArduinojeu-boutons.inoArduinoslider-luminosite.inoArduinothermometre-couleur.inoArduinosonometre.inoArduinoaccelerometre-tilt.inoArduino