0
0
AutocadHow-ToBeginner · 3 min read

How to Communicate Between Two Arduinos Using Serial

To communicate between two Arduinos using Serial, connect their TX and RX pins crosswise (TX to RX and RX to TX) and use Serial.begin() to set the baud rate. One Arduino sends data with Serial.write() or Serial.print(), and the other reads it with Serial.read().
📐

Syntax

To set up serial communication, use Serial.begin(baudRate) in setup(). To send data, use Serial.print() or Serial.write(). To receive data, use Serial.available() to check if data exists and Serial.read() to read it.

  • Serial.begin(9600); starts serial at 9600 bits per second.
  • Serial.print(data); sends readable text.
  • Serial.write(data); sends raw bytes.
  • Serial.available(); returns number of bytes waiting.
  • Serial.read(); reads one byte from the buffer.
arduino
void setup() {
  Serial.begin(9600); // Start serial communication at 9600 baud
}

void loop() {
  if (Serial.available() > 0) { // Check if data is available
    int incomingByte = Serial.read(); // Read one byte
    Serial.print("Received: ");
    Serial.println(incomingByte);
  }
}
💻

Example

This example shows two Arduinos communicating: one sends numbers 0 to 9 repeatedly, the other receives and prints them.

arduino
// Sender Arduino
void setup() {
  Serial.begin(9600);
}

void loop() {
  for (int i = 0; i < 10; i++) {
    Serial.println(i); // Send number as text
    delay(500); // Wait half a second
  }
}


// Receiver Arduino
void setup() {
  Serial.begin(9600);
}

void loop() {
  if (Serial.available() > 0) {
    String received = Serial.readStringUntil('\n'); // Read line
    Serial.print("Received number: ");
    Serial.println(received);
  }
}
Output
Received number: 0 Received number: 1 Received number: 2 Received number: 3 Received number: 4 Received number: 5 Received number: 6 Received number: 7 Received number: 8 Received number: 9
⚠️

Common Pitfalls

  • Wrong wiring: Connect TX of one Arduino to RX of the other, and RX to TX, not TX to TX.
  • Different baud rates: Both Arduinos must use the same Serial.begin() speed.
  • No common ground: Connect grounds of both boards together to share reference voltage.
  • Buffer overflow: Read data quickly to avoid losing bytes.
arduino
// Wrong wiring example (TX to TX)
// This will cause no communication

// Correct wiring example (TX to RX)
// Connect Arduino1 TX pin to Arduino2 RX pin
// Connect Arduino1 RX pin to Arduino2 TX pin
// Connect GND pins together
📊

Quick Reference

FunctionPurpose
Serial.begin(baudRate)Start serial communication at given speed
Serial.print(data)Send readable text data
Serial.write(data)Send raw byte data
Serial.available()Check if data is waiting to be read
Serial.read()Read one byte from serial buffer
Serial.readStringUntil('\n')Read string until newline character

Key Takeaways

Connect TX of one Arduino to RX of the other and share a common ground.
Use the same baud rate on both Arduinos with Serial.begin().
Send data with Serial.print() or Serial.write() and read with Serial.read().
Check Serial.available() before reading to avoid errors.
Read data promptly to prevent buffer overflow and data loss.