Sub topic 7
Buffers and Why Bytes Get Lost
Assume you know the basic Arduino sketch structure, bytes, and digital HIGH and LOW levels. This chapter focuses on what happens after a UART byte reaches the Arduino. You will learn how Arduino stores received bytes, why unread data can overflow a buffer, and how transmission can also be delayed.
The receive buffer
A UART receives bits in hardware. After it reconstructs a byte, an interrupt routine usually copies that byte into a receive buffer. A buffer is a temporary area of memory that holds data until your sketch reads it.
Your loop() does not need to run at the exact time each byte arrives. The UART and its interrupt routine collect bytes while your code performs other short tasks.
The Arduino Serial object exposes the receive buffer through three common functions:
| Function | Purpose |
|---|---|
| `Serial.available()` | Returns the number of unread bytes |
| `Serial.read()` | Removes and returns the next byte, or `-1` if empty |
| `Serial.peek()` | Returns the next byte without removing it |
On many AVR-based Arduino boards, a hardware serial buffer is often about 64 bytes. The exact size depends on the board and core implementation. It is a finite ring buffer, not unlimited storage.
A ring buffer has read and write positions. The interrupt writes new bytes at the write position. Your sketch reads bytes from the read position. When either position reaches the end, it wraps around to the beginning.
How bytes get lost
Bytes are lost when incoming data arrives faster than your sketch removes it. This can happen in several ways:
- The sender transmits a long burst.
loop()contains a longdelay().- A sensor-reading function takes too long.
- The sketch waits for a complete message before reading anything.
- Interrupts are disabled for too long.
- The receive buffer is already full when another byte arrives.
Consider a 64-byte receive buffer. If the sender sends 100 bytes while your sketch does not read the port, the first bytes occupy the buffer. Once it is full, later bytes cannot be stored. Depending on the board's serial implementation, the new byte is discarded or an overflow condition is recorded internally. Your application usually receives no automatic notification that a particular byte disappeared.
A buffer protects you from small timing differences. It does not solve a permanent rate mismatch. If the sender continuously sends 100 bytes per second and your sketch processes only 80 bytes per second, the backlog eventually fills the buffer.
Read promptly, process later
The safest pattern is to remove all available bytes quickly, then process complete data outside the byte-reading loop. Keep the receive path non-blocking.
const byte MAX_LEN = 32;
char line[MAX_LEN];
byte len = 0;
void handleLine(const char *text) {
Serial.print(F("Got: "));
Serial.println(text);
}
void setup() {
Serial.begin(9600);
}
void loop() {
while (Serial.available() > 0) {
char c = Serial.read();
if (c == '\n') {
line[len] = '\0';
handleLine(line);
len = 0;
} else if (c != '\r') {
if (len < MAX_LEN - 1) {
line[len++] = c;
} else {
// Discard the rest of an overlong line.
len = 0;
}
}
}
// Other short, non-blocking work can run here.
}The while loop drains the receive buffer during each pass through loop(). The fixed-size application buffer prevents a long line from overwriting memory. The code reserves one byte for the terminating null character, which makes line a valid C string.
A common error is using readString() or a similar function with a long timeout. Such functions can block while waiting for a delimiter. During that wait, incoming bytes continue to fill the UART buffer. A short timeout can reduce the risk, but a state-based parser is usually easier to control.
The transmit buffer
Transmission has a related but different buffer. When you call Serial.print() or Serial.write(), Arduino normally places outgoing bytes in a transmit buffer. An interrupt moves those bytes to the UART as the hardware becomes ready.
If the transmit buffer fills, a write may wait for space. Therefore, a large print operation can block your sketch, especially at a low baud rate. Printing debug messages inside a time-sensitive loop can delay the code that reads incoming data, causing receive-buffer overflow.
Serial.flush() waits until outgoing data has finished transmitting on current Arduino cores. It does not clear unread incoming bytes. Do not use it as a way to empty the receive buffer.
Practical checks
When bytes seem to disappear, check these points:
- Call
Serial.available()often. - Remove bytes before doing slow work.
- Avoid long
delay()calls. - Check every array boundary.
- Use a higher baud rate when both devices support it.
- Reduce message size or transmission frequency.
- Add flow control or acknowledgments if losing data is unacceptable.
Exercise
At 9600 baud using the common 8N1 format, approximately how long does it take to transmit 64 bytes, and why can a 64-byte receive buffer still overflow?
Show answer
8N1 uses about 10 bits per byte, so 64 bytes take about 640 / 9600 = 0.067 seconds, or 67 milliseconds. The buffer can overflow if the sketch does not read it during that interval, or if data continues arriving faster than the sketch processes it.