🛠 Transformer le Micro en clavier USB (HID) avec bouton de sécurité
Objectives
Leverage the Micro's specificity: the ATmega32U4 is itself the USB peripheral (no coprocessor). The board can therefore type on the keyboard via the Keyboard class, provided you keep a physical safety button to never hijack the host's keyboard.
Hardware
- 1 Arduino Micro + micro-USB cable
- 1 push button between pin 2 and GND
- A computer with a text editor open and focused
Complete code
// Micro clavier USB : tape un message quand le bouton (broche 2) est presse.
// Pattern officiel KeyboardMessage : la frappe ne part que sur appui deliberé.
#include <Keyboard.h>
const uint8_t BTN = 2; // bouton vers GND, pull-up interne
void setup {
pinMode(BTN, INPUT_PULLUP);
Keyboard.begin; // demarre l'interface HID clavier
}
void loop {
if (digitalRead(BTN) == LOW) { // actif-low
delay(50); // anti-rebond
if (digitalRead(BTN) == LOW) {
Keyboard.print("Bonjour depuis mon Arduino Micro !");
while (digitalRead(BTN) == LOW) {} // un seul envoi par appui
}
}
}
Expected verification
Upon uploading, the Micro disappears and then reappears as a keyboard device. Pressing the button enters the message into the text editor that has focus. Unplugging the Micro immediately returns control to the real keyboard.
Common errors
- Sending keystrokes in setup without a safety button: the board hijacks the host's keyboard as soon as it starts; unplug it and reprogram.
- Forgetting that the HID keyboard is active from Keyboard.begin: the sketch must remain responsive to the button.
- Selecting the Leonardo board instead of Micro: uploading may work, but the pins (LED, RX_LED) differ.
- Expecting text in the Serial Monitor: here Serial is not used; the USB identity is keyboard (+ CDC depending on the core).
Other tutorials for this board
- Faire clignoter la LED embarquée (et comprendre les LED TX/RX)
- Lire un bouton avec une résistance de pull-up interne
- Lire un capteur analogique sur le convertisseur 10 bits
- Pont Serial vers Serial1 : passerelle USB vers TTL
- Piloter un servo moteur avec le PWM et un potentiomètre
- Sortir des sentiers : registres AVR et interruption externe
Code examples for this board
clignotante.inoArduinobouton-led.inoArduinoclavier-auto-tape.inoArduinosouris-cercle.inoArduinotelecommande-media.inoArduinobouton-clic-souris.inoArduino