Sub topic 13
Serial Buffers and the Cost of Printing
Assume you know the Arduino setup() and loop() functions, can upload a sketch, and understand variables and if statements. This chapter explains why serial output can delay a sketch, disappear when buffers fill, or make timing unreliable.
How serial data moves
Serial communication sends data one character at a time. When your sketch calls Serial.print(), the Arduino usually places the data in a transmit buffer. The serial hardware then sends the buffered bytes at the selected baud rate.
A buffer is temporary memory that holds data while another part of the system processes it. The Arduino also has a receive buffer for characters arriving from the computer.
The buffer has limited capacity. If the sketch produces output faster than the serial connection can send it, the transmit buffer fills. A later Serial.print() call may then wait until enough space becomes available. This waiting is called blocking because other sketch work cannot proceed during the wait.
Baud rate limits output
The baud rate is the communication speed, measured in symbols per second. For common Arduino serial connections, one character often uses about 10 bits, including start and stop bits. At 9600 baud, the practical rate is roughly:
| Baud rate | Approximate character rate |
|---|---|
| 9600 | 960 characters per second |
| 115200 | 11,500 characters per second |
A message containing labels, numbers, and line endings can use many bytes. Printing a long diagnostic message every loop iteration can fill the buffer quickly.
const unsigned long PRINT_INTERVAL = 100;
unsigned long lastPrint = 0;
void setup() {
Serial.begin(115200);
}
void loop() {
unsigned long now = millis();
if (now - lastPrint >= PRINT_INTERVAL) {
lastPrint = now;
Serial.print(F("time="));
Serial.print(now);
Serial.print(F(" sensor="));
Serial.println(analogRead(A0));
}
// Time-sensitive work can run here.
}This sketch limits output to once every 100 milliseconds. It also uses F() for fixed text. On AVR-based boards such as the Uno, F() keeps string literals in program memory instead of copying them into scarce SRAM.
Printing has a timing cost
Serial output costs time in two ways:
- Formatting numbers and copying bytes uses processor time.
- Sending bytes consumes communication time and can block when the transmit buffer is full.
Avoid printing inside a tight loop unless you deliberately want a high-rate data stream. Add labels while debugging, but remove unnecessary labels when measuring timing or sending data to another program.
Serial output can also affect the behavior you are trying to observe. This is called an instrumentation effect: the act of measuring changes the system. A loop that works without logging may miss deadlines when it prints every iteration.
Receive buffers and unread input
Characters sent from the Serial Monitor enter the receive buffer. If the sketch does not read them, they remain there until the buffer fills. Check Serial.available() before calling Serial.read(), and process input regularly.
When a buffer is full, new data may be discarded or the sender may appear to stop, depending on the connection and library behavior. Read commands promptly and avoid long blocking operations in loop().
Exercise
Why can frequent Serial.println() calls make a sensor-control loop respond slowly?
Show answer
Printing formats data, sends bytes at a limited baud rate, and can block when the transmit buffer is full. Limiting the print rate reduces that delay.