Two Arduino Unos talking over serial
Two Arduino Unos, each with its own sketch, passing "ping" and "pong" lines back and forth over a SoftwareSerial link on pins 2 and 3. The whole build, an Arduino Uno, 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.
Teaching? Assign it to your class in one click.
Intermediate About 15 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $54. On Mokxi they're free.
What you'll learn
- Crossing TX and RX between two boards
- A common ground between boards
- Request and reply so nobody talks over the other
- Reading a line one character at a time
- Difficulty: Intermediate
- Time: About 15 minutes
- Board: Arduino Uno R3
- Parts: 2
How it works
Two Unos talk over a plain serial link on ordinary pins. Uno A's pin 3 goes to Uno B's pin 2, and A's pin 2 to B's pin 3, so each transmitter feeds the other's receiver. Their grounds are joined.
Each board has its own sketch and uses SoftwareSerial at 9600 baud. Uno A sends "ping 1", "ping 2" and so on. Uno B answers each with "pong" and the same number.
SoftwareSerial cannot listen while it talks, so they take turns: A asks, then waits up to 300 ms for the answer. Each board's LED shows activity.
Parts list
2 parts, plus the jumper wires. They retail for about $54; on Mokxi they're free.
Kits that can build it
These 13 starter kits have every part in the circuit. Each kit's page lists what else it can build.
- ELEGOO UNO R3 Super Starter Kit
- ELEGOO UNO R3 Most Complete Starter Kit
- Arduino Starter Kit
- Freenove Ultimate Starter Kit
- ELEGOO UNO R3 Basic Starter Kit
- SunFounder Starter Kit for Arduino Uno R3
- SunFounder Inventor Lab Kit
- REXQualis UNO R3 Super Starter Kit
- LAFVIN Super Starter Kit
- keyestudio Super Starter Kit
- Vilros Arduino Uno 3 Ultimate Starter Kit
- SunFounder Universal Maker Sensor Kit
- Adafruit MetroX Classic Kit
Wiring table
Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.
| Part and pin | Connects to |
|---|---|
| Arduino Uno R3 (2) pin GND | Arduino Uno R3 (1) pin GND |
| Arduino Uno R3 (2) pin 2 | Arduino Uno R3 (1) pin 3 |
| Arduino Uno R3 (2) pin 3 | Arduino Uno R3 (1) pin 2 |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Uno A: sends "ping 1", "ping 2", ... to Uno B over a serial link on
// two ordinary pins, and prints the answer that comes back.
//
// A pin 3 (TX) -> B pin 2 (RX)
// A pin 2 (RX) <- B pin 3 (TX)
// A GND -- B GND
//
// SoftwareSerial is half duplex: while one side sends, it cannot listen.
// So the two take turns, A asks and B answers, and nothing collides.
#include <SoftwareSerial.h>
SoftwareSerial link(2, 3); // RX on pin 2, TX on pin 3
unsigned long count = 0;
char reply[32];
void setup() {
Serial.begin(9600);
link.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
Serial.println("Uno A: a ping every half second");
}
void loop() {
count++;
link.print("ping ");
link.println(count);
digitalWrite(LED_BUILTIN, HIGH);
// Wait up to 300 ms for one line back.
byte len = 0;
bool done = false;
unsigned long start = millis();
while (!done && millis() - start < 300) {
while (link.available()) {
char c = link.read();
if (c == '\n') {
done = true;
break;
}
if (c != '\r' && len < sizeof(reply) - 1) reply[len++] = c;
}
}
reply[len] = 0;
digitalWrite(LED_BUILTIN, LOW);
if (done) {
Serial.print("A heard: ");
Serial.println(reply);
} else {
Serial.println("A heard nothing back. Is B running, and are the grounds joined?");
}
delay(200);
}
// Uno B: listens on pin 2 for a line from Uno A and answers each
// "ping N" with "pong N" on pin 3.
#include <SoftwareSerial.h>
SoftwareSerial link(2, 3); // RX on pin 2, TX on pin 3
char line[32];
byte len = 0;
void setup() {
Serial.begin(9600);
link.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
Serial.println("Uno B: waiting for a ping");
}
void loop() {
while (link.available()) {
char c = link.read();
if (c == '\n') {
line[len] = 0;
Serial.print("B heard: ");
Serial.println(line);
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
// "ping 7" -> "pong 7"
link.print("pong ");
link.println(len > 5 ? line + 5 : "?");
len = 0;
} else if (c != '\r' && len < sizeof(line) - 1) {
line[len++] = c;
}
}
}
Code walkthrough
The key functions of the sketch, and what each one does.
setup sketch.ino
Uno A starts Serial and its link port at 9600 baud, sets the built-in LED as an output and prints that it will ping every half second. Uno B does the same and prints that it is waiting for a ping.
void setup() {
Serial.begin(9600);
link.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
Serial.println("Uno A: a ping every half second");
}loop sketch.ino
Uno A adds one to count, sends "ping" and the count, lights the LED and waits up to 300 ms for a line back. If one arrives it prints "A heard:" and the reply, otherwise a hint to check B and the grounds. Uno B's loop is the mirror: it reads a line, prints "B heard:", toggles its LED and replies "pong" with the number.
void loop() {
count++;
link.print("ping ");
link.println(count);
digitalWrite(LED_BUILTIN, HIGH);
// Wait up to 300 ms for one line back.
byte len = 0;
bool done = false;
unsigned long start = millis();
while (!done && millis() - start < 300) {
while (link.available()) {
char c = link.read();
if (c == '\n') {
done = true;
break;
}
if (c != '\r' && len < sizeof(reply) - 1) reply[len++] = c;
}
}
reply[len] = 0;
digitalWrite(LED_BUILTIN, LOW);
if (done) {
Serial.print("A heard: ");
Serial.println(reply);
} else {
Serial.println("A heard nothing back. Is B running, and are the grounds joined?");
// ... 3 more lines in the editorTroubleshooting
The usual mistakes with this circuit, and the fix for each.
- Uno A says it heard nothing back This project
Check the crossing: A's pin 3 to B's pin 2 and A's pin 2 to B's pin 3, and make sure Uno B is running. - Garbled text or no reply even when crossed This project
Join the two GND pins. Without a shared ground the signal levels mean nothing. - Upload fails or nothing runs after wiring pins 0 and 1 to a module. Arduino Uno R3
Pins 0 and 1 are the USB serial port. Move the module to other pins, or unplug it while uploading. Also check the right board is selected. - Pins misbehave or the board resets when you power motors or servos from it. Arduino Uno R3
Each I/O pin gives about 20 mA, 40 mA max. Power motors and servos from their own supply and join its ground to the Uno's GND.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.