🛠 Transformer le Micro en clavier USB (HID) avec bouton de sécurité

Arduino Micro · intermediaire · Schéma : IMG-micro-intermediaire-1-micro-hid-clavier.svg — Arduino Micro branchée en USB à un ordinateur, vue comme périphérique clavier, avec un bouton de sécurité sur la broche 2 vers GND et un éditeur de texte ouvert à l'écran

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

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

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials