Two Arduinos over Serial
One Arduino's button switches another Arduino's LED: two boards, two sketches, one serial link between them.
Communication · Intermediate
In a real product: Smart home hub (Smart home): Two boards cooperate over a serial link.
Most real devices are more than one microcontroller: a remote and a receiver, a sensor node and a gateway. Here two Unos share a serial link. Each runs its own sketch: sketch.ino on the first board, uno2.ino on the second.
What you'll learn
- Crossing TX and RX between two boards, and why their grounds must be joined
- Sending commands as text lines and answering them
- SoftwareSerial on one side and the hardware Serial on the other
How it works
The controller (uno) keeps its hardware Serial for the monitor and talks to the other board through SoftwareSerial: its pin 11 (TX) goes to uno2's RX (pin 0), and uno2's TX (pin 1) comes back to pin 10. A button press sends ON or OFF, and every second it sends PING. The light (uno2) reads a line at a time on its own Serial, switches the LED on pin 9 and answers; the controller prints every answer, so its monitor shows both sides of the conversation.
Pick the board in the serial monitor's list to see each one's Serial. Both run at 9600 baud: with different rates, each byte arrives as garbage.
Try this
- Press the button and watch both monitors. Then set
uno2toSerial.begin(19200)and see what arrives. - Remove the wire between the two grounds: on real boards the link stops working, as each pin's level means nothing without a shared reference.
- Add a second command,
BLINK, that blinks the LED three times.
sketch.ino
// The controller: a button on pin 2 switches the other Arduino's LED, through SoftwareSerial on pins 10 (RX) and 11
// (TX). Every second it sends PING; it prints whatever the other board answers.
#include <SoftwareSerial.h>
SoftwareSerial link(10, 11); // RX, TX
bool ledOn = false;
bool lastButton = HIGH;
unsigned long lastPing = 0;
void setup() {
Serial.begin(9600);
link.begin(9600);
pinMode(2, INPUT_PULLUP);
Serial.println("Press the button to switch the other board's LED");
}
void loop() {
bool button = digitalRead(2);
if (button == LOW && lastButton == HIGH) {
ledOn = !ledOn;
link.println(ledOn ? "ON" : "OFF");
Serial.print("sent ");
Serial.println(ledOn ? "ON" : "OFF");
delay(30); // let the contacts stop bouncing
}
lastButton = button;
if (millis() - lastPing >= 1000) {
lastPing = millis();
link.println("PING");
}
while (link.available()) Serial.write(link.read());
}uno2.ino
// The light: listens on its hardware Serial (pins 0 and 1, wired to the controller) for ON, OFF and PING, switches
// the LED on pin 9 and answers each.
String line;
void setup() {
Serial.begin(9600);
pinMode(9, OUTPUT);
}
void loop() {
while (Serial.available()) {
char c = Serial.read();
if (c == '\r') continue;
if (c != '\n') { line += c; continue; }
if (line == "ON") { digitalWrite(9, HIGH); Serial.println("LED is on"); }
else if (line == "OFF") { digitalWrite(9, LOW); Serial.println("LED is off"); }
else if (line == "PING") { Serial.print("PONG, up "); Serial.print(millis() / 1000); Serial.println(" s"); }
else { Serial.print("unknown command: "); Serial.println(line); }
line = "";
}
}