Sub topic 5
Parity, Framing Errors and Overrun
Assume you know the basic Arduino setup() and loop() functions, bytes, and digital HIGH and LOW levels. This chapter focuses on errors detected by a UART receiver. A UART is an asynchronous serial interface, so the receiver must infer timing and frame boundaries from the incoming signal.
What the receiver checks
A UART frame normally contains a start bit, data bits, an optional parity bit, and one or more stop bits. The receiver checks the parity and stop bits while it converts the signal into a byte.
| Error | What the receiver expected | Typical cause |
|---|---|---|
| Parity error | The parity bit matched the data bits | Noise or different parity settings |
| Framing error | The stop bit was HIGH at the expected time | Wrong baud rate, noise, or wrong frame format |
| Overrun | Software read each received byte in time | Code was too slow or interrupts were delayed |
These errors describe different problems. A parity error means the received data probably changed. A framing error means the receiver lost the expected frame timing or format. An overrun means a valid byte arrived but the receiver had nowhere to put it.
Parity errors
Parity is a simple error-detection method. With even parity, the parity bit makes the total number of 1 bits in the data and parity bit even. With odd parity, the total becomes odd.
For example, suppose an 8-bit value contains three 1 bits:
- Even parity adds a 1, giving four 1 bits.
- Odd parity adds a 0, leaving three 1 bits.
Parity can detect many single-bit errors, but it cannot correct an error. It also cannot reliably detect an even number of changed bits. A receiver may still deliver the byte to software while setting an error flag.
Both devices must use the same parity setting. Common formats include:
SERIAL_8N1: 8 data bits, no parity, 1 stop bitSERIAL_8E1: 8 data bits, even parity, 1 stop bitSERIAL_8O1: 8 data bits, odd parity, 1 stop bit
The default Arduino format is usually SERIAL_8N1. You can select another format when starting a hardware serial port:
void setup() {
Serial.begin(9600, SERIAL_8E1);
}
void loop() {
Serial.println("Even parity");
delay(1000);
}The other device must also use 9600 baud, 8 data bits, even parity, and 1 stop bit. If it expects SERIAL_8N1, the link will not be compatible even though the baud rate matches.
Framing errors
A UART receiver looks for a LOW start bit, samples the data bits at calculated times, and then checks that the stop bit is HIGH. If the stop bit is LOW when expected, the receiver reports a framing error.
A baud-rate mismatch is a common cause. For example, if one device sends at 9600 baud and the other receives at 115200 baud, the receiver samples at the wrong times. It may interpret data bits as start or stop bits.
A framing error can also result from:
- A mismatch such as 7 data bits versus 8 data bits.
- A mismatch in the number of stop bits.
- Electrical noise or a damaged connection.
- An incorrect logic-level converter.
- A missing common ground.
- A line held LOW continuously.
A framing error does not identify the exact cause. It tells you that the receiver could not find the expected frame ending.
Overrun errors
The UART hardware usually has a small receive buffer. When a complete byte arrives, the hardware stores it and may raise an interrupt. Your program must eventually read it.
An overrun occurs when another byte arrives before the previous byte has been removed from the hardware buffer. Depending on the microcontroller, the new byte or the older byte may be lost. The exact behavior is hardware-specific, but the result is the same: the byte stream is incomplete.
Long delays, disabled interrupts, heavy printing, and blocking sensor libraries can cause overruns. A loop that reads only one byte per pass is also risky when data arrives continuously.
Read all currently available bytes before doing slower work:
void setup() {
Serial.begin(115200);
}
void loop() {
while (Serial.available() > 0) {
int value = Serial.read();
if (value >= 0) {
// Process this byte quickly.
Serial.write(static_cast<uint8_t>(value));
}
}
}The Arduino Serial API gives you portable access to received bytes, but it does not provide one common function for parity, framing, or overrun flags on every board. AVR-based boards expose hardware status registers, while other Arduino cores use different registers or APIs. If you need those flags, consult the documentation for the exact board and serial implementation.
A practical diagnosis order
When errors appear, check the settings and physical link before changing the parser:
- Confirm baud rate and frame format at both ends.
- Confirm TX connects to RX, RX connects to TX, and grounds are shared.
- Confirm both devices use compatible voltage levels.
- Reduce the baud rate to see whether noise or timing is involved.
- Remove long delays and read the receive buffer frequently.
- Use board-specific status registers only after the basic link is correct.
Exercise
A sender uses 9600 baud and SERIAL8E1, but the receiver uses 9600 baud and SERIAL8N1. Which setting is wrong, and what errors might appear?
Show answer
The parity setting is different. The receiver does not expect the sender's parity bit, so it can misread the frame and report parity or framing errors. Both devices must use the same complete frame format.