Robotics and Embedded Systems

Lesson 10: Serial Communication: Talking to Your ComputerNew-age

Serial communication is the conversation between your microcontroller and your computer, carried over the same USB cable that powers the board. It is how you print sensor readings, how you send commands to a robot, and how you find out what your code is actually doing when the robot does something unexpected. This lesson teaches you to use the Serial Monitor, to send and receive data reliably, and to design a simple command protocol that a project can grow with.

Serial is also your best debugging tool. A robot that misbehaves on the bench is a mystery; the same robot printing its sensor readings every half second is a problem you can see. Learn to print early, print often, and print values that mean something — and debugging will stop being guesswork.

Key Terms.
  • Serial communication — sending data one bit at a time over a single wire, usually over USB.
  • Baud rate — the speed of a serial connection in bits per second, such as 9600.
  • Serial Monitor — the tool in the Arduino IDE that shows text the board sends and lets you send text back.
  • Protocol — an agreed format for messages, such as “LED:1” to turn an LED on.
  • Delimiter — a character that marks the end of a message, usually a newline.
  • Buffer — a small memory area that holds incoming data until the program reads it.
  • Handshake — an exchange that confirms both sides are ready to communicate.
  • ASCII — the standard code for text characters, so “A” is sent as the number 65.

Printing values: your debugging superpower

Start serial communication in setup() with Serial.begin(9600), and print with Serial.print() and Serial.println(). The number 9600 is the baud rate — the speed of the connection — and it must match the rate set in the Serial Monitor, or the text will arrive as garbage. Print labels with your values: Serial.print("light: "); Serial.println(light); is useful, while Serial.println(light); alone is a number without a meaning.

Print at sensible intervals. Printing every loop floods the monitor with thousands of lines and can slow the program; printing every 500 ms with a delay or a timer gives a readable stream. Print the values that decide things: the sensor reading, the threshold, and the decision. When a robot misbehaves, the printed values usually show why — a sensor reading of 0 where you expected 500 means a disconnected wire, not a bug in your logic.

Receiving commands from the computer

Communication goes both ways. Serial.available() tells you how many characters have arrived; Serial.readStringUntil('\n') reads a whole line up to the newline. A common pattern is to send short commands such as “F” for forward, “S” for stop, or “LED:1” to turn an LED on. Keep commands short and readable, and always print back what you received — an echo — so you can see whether the command arrived intact.

Never assume a command arrives whole. Serial data arrives byte by byte, and a slow connection can split a message across two reads. Reading until a delimiter — the newline character — is the fix: the program waits until the full line has arrived before acting on it. Without a delimiter, “LED:1” might be read as “LED” and then “:1”, and neither half will match any command.

Designing a simple protocol

A protocol is an agreement about the format of messages. Design yours before you write code: list the commands, their format, and the replies. For a robot, the protocol might be single letters — F, B, L, R, S — each meaning a direction, with the board replying “OK” after each one. For a sensor station, it might be lines such as “TEMP:23.5”, which a computer program can parse and log. Write the protocol down, even for a small project: it is the contract between the two sides, and it saves hours of confusion.

Keep the protocol forgiving. Ignore unknown commands instead of crashing, and reply with an error message such as “ERR:unknown” so the sender knows something went wrong. Add a simple handshake at the start — the board prints “READY” when it starts, and the computer waits for it — so neither side sends commands before the other is listening.

When serial goes wrong

The commonest serial problem is a baud rate mismatch: the board sends at 9600 and the monitor listens at 115200, producing garbage characters. The fix is to set both to the same rate. The second is a busy port: only one program can use a serial port at a time, so close the Serial Monitor before uploading new code, or the upload will fail.

Worked example — a command protocol for an LED

The board accepts two commands over serial: “ON” turns an LED on, “OFF” turns it off, and it replies “OK” or “ERR:unknown”. The code reads a line, trims whitespace, and compares it.

void loop() {
  if (Serial.available()) {
    String cmd = Serial.readStringUntil('\n');
    cmd.trim();
    if (cmd == "ON") { digitalWrite(13, HIGH); Serial.println("OK"); }
    else if (cmd == "OFF") { digitalWrite(13, LOW); Serial.println("OK"); }
    else { Serial.println("ERR:unknown"); }
  }
}

Common mistakes

  • Setting different baud rates on the board and the monitor, so the text arrives as garbage.
  • Printing every loop, flooding the monitor and slowing the program.
  • Acting on part of a message because there was no delimiter. Read until a newline.
  • Leaving the Serial Monitor open while uploading, so the upload fails with a busy-port error.

Discussion questions

  1. Serial prints slow a program down. When is that a problem, and when is it worth the cost?
  2. A protocol could use single letters (F, B, S) or words (FORWARD, BACK, STOP). What are the trade-offs?
  3. Your robot prints sensor values, but they look wrong. How do you decide whether the sensor, the wiring or the code is at fault?

Practice questions

  1. What is a baud rate, and why must it match on both sides? Answer: The speed of the serial connection in bits per second. If the board and the monitor use different rates, the bits are sampled at the wrong times and the text arrives as garbage. Explanation: Both sides must agree on speed.
  2. Why should you print labels with your values? Answer: A bare number has no meaning. “light: 342” tells you what was measured; “342” alone does not. Explanation: Label every value.
  3. What is a delimiter, and why does reading until one matter? Answer: A character marking the end of a message, usually a newline. Without it, a message split across two reads is acted on in pieces. Explanation: Wait for the whole message.
  4. The Serial Monitor shows garbage characters. What is the most likely cause? Answer: A baud rate mismatch between the board and the monitor. Set both to the same rate. Explanation: Check the baud rate first.
  5. Why should a protocol reply “ERR:unknown” for a command it does not recognise? Answer: So the sender knows the command failed instead of assuming it worked. Silent failure is the hardest kind to debug. Explanation: Errors should be visible.

✅ Check your understanding

Quick recall check, generated from this lesson’s key terms. It is practice only and is not graded.

Keep reading to complete this lesson