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:
#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);
}
}