Serial communication: how Arduino talks to a computer
Serial communication is the simplest way for two devices to exchange data: send characters one after another over a single wire, at an agreed speed (the baud rate, e.g. 115200).
Debugging with serial
The first use is printing. When your code misbehaves, print what it is doing:
void setup() { Serial.begin(115200); }
void loop() {
int d = analogRead(A0);
Serial.print("distance: ");
Serial.println(d);
delay(100);
}Open the Serial Monitor and you see the values live. This is the single most useful debugging tool on Arduino.
Boards talking to boards
In a real robot, one board often reads sensors and sends the values to a more powerful computer. A simple, robust approach is a text protocol: each line is one message.
S1 340
S2 512
G1 12.4 3.1
The receiver reads a line, splits it, and knows exactly what each number means. Text protocols are easy to debug because you can read them with your own eyes.
Two rules that save hours
- Match the baud rate on both ends, or you get garbage characters.
- Handle bad lines. Serial data can arrive corrupted. Always guard your parsing so one malformed line does not crash the program.
Serial is old, simple and everywhere. Master it and you can connect almost anything to anything.