🛠 Pont Serial vers Serial1 : passerelle USB vers TTL
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
- 1 Arduino Micro + micro-USB cable
- 1 UART TTL module (GPS, USB-TTL converter...) connected as follows: pin 0 (Micro RX) to module TX, pin 1 (Micro TX) to module RX, common GND
- Warning: 5 V logic on the Micro pins
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
- Believing that the Serial Monitor sees Serial1: it only sees the USB CDC; hence this bridge.
- Swapping RX/TX: connect Micro RX (0) to the module TX, never TX to TX.
- Forgetting the common GND between the module and the Micro: nothing gets through.
- Working with a 3.3 V module that is not 5 V tolerant: the Micro uses 5 V logic, so a level shifter is required.
- Relying on available to know the size of a USB frame: bug #267 in the AVR core limits available to 0 or 1 on the CDC port; process bytes one by one.
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
- Transformer le Micro en clavier USB (HID) avec bouton de sécurité
- 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