Built-in project · Arduino Mega 2560 R3
Mega four serial ports
Three of the Mega's four USARTs, each with its TX jumpered back to its own RX, reporting on the fourth. The whole build, an Arduino Mega, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
BeginnerRuns in your browser. Free, and no account needed.
Click to open it in the editor
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
sketch.ino · Arduino Mega 2560 R3
// Four serial ports: the thing a Mega has that an Uno does not.
//
// An Uno has one USART and it is spoken for: it is the channel to the serial
// monitor. A Mega has four. Serial is still the monitor, and Serial1, Serial2
// and Serial3 come out on header pins:
//
// Serial1 TX1 pin 18 RX1 pin 19
// Serial2 TX2 pin 16 RX2 pin 17
// Serial3 TX3 pin 14 RX3 pin 15
//
// Wire each TX back to the RX beside it with a short jumper and every port
// talks to itself, which is the cheapest way to see all three working at once
// with nothing else on the bench. In the simulator those are real wires and
// the bytes are real 8N1 frames on them: start bit, eight data bits least
// significant first, stop bit, at the baud rate each port was opened with.
// Change one port's baud rate and its line comes back as mojibake, exactly as
// it would on the bench.
//
// The three ports run at different rates on purpose, so the frames on the
// three wires are visibly different lengths.
const unsigned long RATE[3] = { 9600, 19200, 38400 };
HardwareSerial *port(int n) {
if (n == 0) {
return &Serial1;
}
if (n == 1) {
return &Serial2;
}
return &Serial3;
}
unsigned long lastSend = 0;
int round_ = 0;
void setup() {
Serial.begin(115200);
Serial1.begin(RATE[0]);
Serial2.begin(RATE[1]);
Serial3.begin(RATE[2]);
Serial.println("Mokxi Arduino Mega 2560: four serial ports");
for (int n = 0; n < 3; n++) {
Serial.print("Serial");
Serial.print(n + 1);
Serial.print(" at ");
Serial.print(RATE[n]);
Serial.println(" baud, TX jumpered to its own RX");
}
}
void loop() {
unsigned long now = millis();
// Once a second, send one line out of each port.
if (now - lastSend >= 1000) {
lastSend = now;
round_++;
for (int n = 0; n < 3; n++) {
port(n)->print("ping ");
port(n)->println(round_);
}
}
// Anything a port hears, it heard from itself. Report it with the port it
// arrived on, so three lines a second appear in the monitor.
for (int n = 0; n < 3; n++) {
while (port(n)->available() > 0) {
int c = port(n)->read();
if (c == '\r') {
continue;
}
if (c == '\n') {
Serial.println();
continue;
}
if (c == 'p') {
// The start of a line: label it with the port it came back on.
Serial.print("Serial");
Serial.print(n + 1);
Serial.print(" heard: ");
}
Serial.print((char)c);
}
}
}
Parts list
1 part, plus the jumper wires. Every one is in the editor's parts bin.
How it is wired
3 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.
- Arduino Mega 2560 R3 pin 14; Arduino Mega 2560 R3 pin 15
- Arduino Mega 2560 R3 pin 16; Arduino Mega 2560 R3 pin 17
- Arduino Mega 2560 R3 pin 18; Arduino Mega 2560 R3 pin 19
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.