saisie-serie-vers-clavier.ino

Arduino Leonardo — Board Fiche · File : saisie-serie-vers-clavier.ino · Language : Arduino · Use case : Recevoir du texte sur le port série et le retaper au clavier

This bridge showcases the 32U4 double USB personality: serial port for the computer, keyboard for… the same computer. Send text to the serial monitor and it appears in any input field.

Hardware required

An Arduino Leonardo (ATmega32U4, 16 MHz, 5 V) with native USB: the board can pose as a keyboard or a mouse. None.

Wiring

A single USB cable (the 32U4 enumerates serial + keyboard on the same cable).

How it works

Each received byte becomes a HID keystroke; newline becomes Enter. The short delay keeps the USB buffer from overflowing. In the Arduino IDE, select « Arduino Leonardo » and the port, then Upload. Caution: a sketch that sleeps too fast can « lose » the USB port; hold RESET for 12 s to wipe the bootloader.

Going further

Wire a serial barcode reader to Serial1 (RX1/TX1) for a scan → auto-type pipeline.

saisie-serie-vers-clavier.ino

// Pont serie -> clavier : ce qui arrive sur Serial est "tape".
#include <Keyboard.h>

void setup() {
  Serial.begin(9600);
  Keyboard.begin();
  delay(2000);
  Serial.println(F("Mesurez-vous : je retape tout ce qui arrive."));
}

void loop() {
  while (Serial.available()) {
    char c = Serial.read();
    if (c == '\n') { Keyboard.write(KEY_RETURN); }
    else if (c == '\r') { /* ignore */ }
    else { Keyboard.write(c); }
    delay(2); // laisse le tampon HID respirer
  }
}

← Back to the board