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 Runs in your browser. Free, and no account needed.
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;
}
}
}
Parts list
2 parts, plus the jumper wires. Every one is in the editor's parts bin.
- 2 × Arduino Uno R3
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.
- Ground: Arduino Uno R3 (1) pin GND; Arduino Uno R3 (2) pin GND
- Arduino Uno R3 (1) pin 3; Arduino Uno R3 (2) pin 2
- Arduino Uno R3 (1) pin 2; Arduino Uno R3 (2) pin 3
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.