🛠 Pont Serial vers Serial1 : passerelle USB vers TTL

Arduino Micro · intermediaire · Schéma : IMG-micro-intermediaire-2-micro-pont-serial-serial1.svg — Arduino Micro en passerelle série : le port USB (Serial, CDC) relié logiquement à l'UART TTL des broches 0 (RX) et 1 (TX) vers un module externe, avec des flèches dans les deux sens

Objectives

Build a serial bridge: everything arriving on the USB port (Serial, CDC) is copied to the UART TTL Serial1 (pins 0/1), and vice versa. Useful for communicating with a GPS, a GSM module, or another 5 V microcontroller from the Serial Monitor.

Hardware

Complete code

// Passerelle bidirectionnelle USB CDC (Serial) <-> UART TTL (Serial1, broches 0/1).
// Memo bug du noyau AVR #267 : sur le port USB 32U4, Serial.available
// ne remonte que 0 ou 1 octet a la fois ; lire en continu, pas par blocs.
void setup {
  Serial.begin(9600);    // USB CDC : ce que voit le Moniteur serie
  Serial1.begin(9600);   // UART materiel sur broches 0 (RX) / 1 (TX)
  while (!Serial) {}     // attendre l'enumeration USB
  Serial.println("Passerelle active : USB <-> broches 0/1");
}

void loop {
  while (Serial.available > 0)    // hote USB -> module TTL
    Serial1.write(Serial.read);
  while (Serial1.available > 0)   // module TTL -> Moniteur serie
    Serial.write(Serial1.read);
}

Expected verification

What you type in the Serial Monitor is output on pin 1 (TX) and appears on the terminal connected to the module; what the module sends to pin 0 (RX) appears in the Monitor. A test loop connecting pin 1 to pin 0 (direct wire) makes each typed character appear.

Common errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials