user
How to write a serial passthrough from Serial to pins 2(RX) and pin 3(TX) with Arduino ?
alphonsio

To write a serial passthrough from the default Serial (usually USB Serial Monitor) to pins 2 (RX) and 3 (TX) on an Arduino, you'll need to use the SoftwareSerial library to emulate a second serial port.

Here’s a complete example using SoftwareSerial on an Arduino Uno:

✅ Example: Serial Passthrough between USB and Pins D2/D3

#include <SoftwareSerial.h>

// Create a software serial port on pins 2 (RX) and 3 (TX)
SoftwareSerial softSerial(2, 3); // RX, TX

void setup() {
  Serial.begin(9600);         // Start hardware serial (USB)
  softSerial.begin(9600);     // Start software serial on 2 and 3
}

void loop() {
  // USB → 2/3
  if (Serial.available()) {
    char inByte = Serial.read();
    softSerial.write(inByte);
  }

  // 2/3 → USB
  if (softSerial.available()) {
    char inByte = softSerial.read();
    Serial.write(inByte);
  }
}

📝 Notes:

  • Pin 2 is the RX pin of the software serial, which receives data.
  • Pin 3 is the TX pin of the software serial, which transmits data.
  • Make sure the baud rate matches the device you're communicating with.
  • Don’t connect devices to pins 2 and 3 until after uploading the code, as they might interfere with the bootloader.