pont-uart.ino

Arduino Mega 2560 (Rev3) · File : pont-uart.ino · Language : Arduino · Use case : Relayer deux appareils série entre eux grâce aux UART multiples

The ATmega2560 has four hardware UARTs: perfect for a GPS, GSM module or a second microcontroller without sacrificing the USB port. The bridge copies everything between UART1 and UART2.

Hardware required

An Arduino Mega 2560 (ATmega2560, 16 MHz, 5 V): 54 digital I/O, 15 PWM, 16 analog inputs, 4 hardware UARTs. 2 5 V serial devices (e.g. TTL GPS + second Arduino).

Wiring

Device 1 on RX1=19/TX1=18, device 2 on RX2=17/TX2=16 (cross RX/TX), common grounds.

How it works

Every byte read on one port is written to the other, with a copy on USB to watch the traffic live. In the Arduino IDE, select « Arduino Mega 2560 » and the port, then Upload. PWM on 2-13 and 44-46; UART1=18/19, UART2=16/17, UART3=14/15; SPI on 50-53.

Going further

Filter GPS NMEA sentences ($GPGGA prefix) instead of copying everything, for a position logger.

pont-uart.ino

// Pont serie entre Serial1 (broches 19/18) et Serial2 (17/16).
// Exemple : GPS sur UART1 -> moniteur, et module sur UART2.

void setup() {
  Serial.begin(9600);   // USB (debug)
  Serial1.begin(9600);  // UART1: RX1=19, TX1=18
  Serial2.begin(9600);  // UART2: RX2=17, TX2=16
  Serial.println(F("Pont UART1 <-> UART2 actif"));
}

void loop() {
  if (Serial1.available()) {
    char c = Serial1.read();
    Serial2.write(c);   // appareil 1 -> appareil 2
    Serial.write(c);    // copie vers le moniteur
  }
  if (Serial2.available()) {
    char c = Serial2.read();
    Serial1.write(c);   // appareil 2 -> appareil 1
  }
}

← Back to the board