Sub topic 2
Anatomy of a UART frame
This chapter assumes you can write a basic Arduino sketch with setup() and loop(). It also assumes you can read binary and hexadecimal bytes, and that you know what HIGH and LOW mean on a digital pin. The previous chapter explained why serial links send bits one at a time. This chapter shows exactly which bits go on the wire for each byte.
Why UART needs frames
A UART (Universal Asynchronous Receiver/Transmitter) link has no clock wire. The transmitter and receiver each run their own timing source. The receiver cannot see where one bit ends and the next begins. It also cannot tell when a byte starts unless the sender marks it.
The solution is a frame: a short, fixed pattern of bits wrapped around every byte. The frame tells the receiver when a byte begins, lets it re-synchronize on every byte, and can add a simple error check. Between frames the line rests in a known state.
A frame has these parts, in this order:
- One start bit.
- Five to eight data bits (usually eight).
- An optional parity bit.
- One or two stop bits.
The idle state and the start bit
When nothing is being sent, the TX line stays HIGH. This is the idle state, also called the "mark" state. The idle level is HIGH for a practical reason: a broken wire or an unpowered transmitter does not look like an endless stream of valid data.
To begin a byte, the transmitter pulls the line LOW for exactly one bit time. This is the start bit. It is always 0. The receiver watches for the HIGH-to-LOW edge and treats it as the start of a frame.
The start bit is what makes the link asynchronous. Each byte brings its own timing reference, which is the falling edge. The receiver measures everything else relative to that edge, so small clock differences cannot accumulate beyond one frame.
Data bits
After the start bit come the data bits. Two rules matter:
- The least significant bit (LSB) is sent first. Bit 0 goes out first and bit 7 goes out last. Many people expect the opposite, so this is a common source of confusion when reading oscilloscope or logic analyzer traces.
- Most links use 8 data bits. The format can also use 5, 6, 7 or 9 bits, and 7 was common for ASCII text. Arduino's
Serialsupports 5 to 8 data bits and does not support the 9-bit mode.
For the letter A (0x41, binary 01000001), the data bits on the wire are the reverse of how you write the number:
| Bit position sent | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
|---|---|---|---|---|---|---|---|---|
| Value | 1 | 0 | 0 | 0 | 0 | 0 | 1 | 0 |
The parity bit
Parity is an optional single bit placed after the data bits. Its job is to let the receiver detect that a single bit was flipped by noise.
- Even parity: the parity bit is chosen so that the total number of 1s in the data bits plus the parity bit is even.
- Odd parity: the parity bit is chosen so that the total is odd.
- None: no parity bit is sent. This is the most common choice.
The byte 0x41 has two 1 bits. With even parity, the parity bit is 0, because the count is already even. The byte 0x37 (00110111) has five 1 bits. With even parity, the parity bit is 1, which makes six.
Parity catches any odd number of flipped bits. It misses two flipped bits, because the count is even again. It also cannot tell you which bit changed. A later chapter covers how the receiver reports parity failures and why stronger checks live at the message level.
Stop bits
The frame ends with one or two stop bits. A stop bit is always 1, which is the same level as idle. This guarantees that the line returns HIGH after every byte. The next start bit then produces a clean falling edge.
Stop bits also give the receiver a check. It expects the line to be HIGH at the stop position. If it sees LOW there, the frame does not match the expected shape, and the receiver reports a framing error.
Two stop bits give the receiver a little more time to finish processing a byte before the next one arrives. Modern hardware rarely needs this. One stop bit is the normal choice.
A complete frame, bit by bit
Here is the full 8N1 frame for A. The name "8N1" means 8 data bits, no parity, 1 stop bit.
| Slot | Start | D0 | D1 | D2 | D3 | D4 | D5 | D6 | D7 | Stop |
|---|---|---|---|---|---|---|---|---|---|---|
| Level | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 1 |
That is 10 bit times for one byte of payload. The line is HIGH before the start bit and HIGH again after the stop bit.
Seeing the frame from a sketch
You can print the frame bits for any byte. This sketch builds them with the same rules the UART hardware follows: LSB first, then an even parity bit computed from the count of 1s.
void printFrame(uint8_t b) {
Serial.print(F("start=0 data(LSB first)="));
uint8_t ones = 0;
for (uint8_t i = 0; i < 8; i++) {
uint8_t bit = (b >> i) & 1;
Serial.print(bit);
ones += bit;
}
Serial.print(F(" evenParity="));
Serial.print(ones & 1);
Serial.println(F(" stop=1"));
}
void setup() {
Serial.begin(9600);
printFrame(0x41);
printFrame(0x37);
}
void loop() {
}The Serial Monitor shows:
start=0 data(LSB first)=10000010 evenParity=0 stop=1
start=0 data(LSB first)=11101100 evenParity=1 stop=1The expression ones & 1 gives 1 when the count of 1s is odd, and adding that bit makes the total even. For odd parity you would invert the result.
How the receiver reads a frame
A receiver does not sample the line at random moments. It follows a fixed procedure:
- Wait in the idle state until the line goes LOW.
- Wait half a bit time and check that the line is still LOW. This rejects short noise spikes that are not real start bits.
- Wait one full bit time, so the receiver is now in the middle of data bit 0. Sample the line.
- Repeat every bit time for the remaining data bits and the parity bit.
- Sample the stop bit. If it is HIGH, the frame is valid and the byte is delivered. If it is LOW, report a framing error.
The receiver samples in the middle of each bit, where the signal is most stable and farthest from the edges. Real UART hardware usually runs an internal clock at 16 times the baud rate to find the middle. The ATmega328P in the Uno takes several samples near the center of each bit and uses a majority vote.
How much clock error is allowed
The receiver measures from the start edge. The last sample, the stop bit in 8N1, happens about 9.5 bit times later. If the two clocks differ so much that the accumulated drift reaches half a bit, the receiver samples the wrong bit. Half a bit over 9.5 bit times is roughly 5% in the ideal case.
In practice the margin is smaller. Edge distortion, sampling delay and the two clocks' errors in opposite directions all eat into it. Aim for a combined mismatch under about 2%. The Baud rate chapter shows how to calculate the actual error for a given crystal.
Common frame formats
The format is written as data bits, parity letter, stop bits. These are the ones you will meet:
| Format | Data bits | Parity | Stop bits | Bits per frame | Payload efficiency |
|---|---|---|---|---|---|
| 8N1 | 8 | None | 1 | 10 | 80% |
| 8E1 | 8 | Even | 1 | 11 | 73% |
| 8O1 | 8 | Odd | 1 | 11 | 73% |
| 8N2 | 8 | None | 2 | 11 | 73% |
| 7E1 | 7 | Even | 1 | 10 | 70% |
| 5N1 | 5 | None | 1 | 7 | 71% |
Efficiency is data bits divided by bits per frame. Even the best case spends 20% of the line time on framing overhead. This matters for throughput: at 9600 baud with 8N1, one byte takes 10 / 9600 s, about 1.04 ms, which gives at most 960 bytes per second.
Setting the format on Arduino
Serial.begin() takes the baud rate and an optional second argument for the frame format. If you leave it out, you get SERIAL_8N1. Names follow the pattern SERIAL_<data><parity><stop>, for example SERIAL_8E1, SERIAL_7O2 or SERIAL_5N1.
void setup() {
Serial.begin(115200, SERIAL_8E1);
}
void loop() {
}Both ends must use the same format as well as the same baud rate. A mismatch does not always produce obvious errors. If one side sends 8N1 and the other expects 8E1, the receiver reads the first stop bit as a parity bit and computes the check on the wrong bit. About half of all bytes then fail the parity test, and the stop bit position is off by one, so the data is unreliable.
Edge cases and gotchas
The break condition
If the transmitter holds the line LOW for longer than one whole frame, the receiver sees a start bit, all-zero data and a LOW stop bit. This is called a break. Some protocols use it deliberately as a signal, such as DMX512 and LIN. Otherwise a break often means the sender has been disconnected or the line is stuck LOW. A receiver sees it as a framing error with a data byte of 0x00.
Gaps between frames
The idle time between frames can be any length, from zero up to hours. Two frames can follow each other with no gap, so the stop bit of one frame is immediately followed by the start bit of the next. The receiver does not need the gap. It only needs the stop bit to end HIGH.
The letter U is a good test byte
The byte 0x55 is the character U. Its bits are 01010101. Sent LSB first in 8N1, the whole frame becomes 0 1 0 1 0 1 0 1 0 1, an alternating pattern. On an oscilloscope this gives a square wave at half the baud rate, which makes the bit time easy to measure.
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.write('U');
}Measure the width of one pulse on the TX pin. At 9600 baud it should be about 104 µs.
Reading bits in the wrong order
If you decode a logic analyzer capture by hand, read the data bits right to left on paper, or reverse them, to get the byte value. Most analyzer software does this automatically. Doing it by hand with the wrong order produces values like 0x82 where you expected 0x41.
Summary
- The line idles HIGH. A LOW start bit marks the beginning of every byte.
- Data bits follow, LSB first, then an optional parity bit, then one or two HIGH stop bits.
- 8N1 uses 10 bits per byte, so 20% of the line time is overhead.
- The receiver finds the start edge, samples the middle of each bit and checks that the stop bit is HIGH.
- Both devices must agree on baud rate and frame format.
Exercise
A device sends the byte 0x2C (binary 00101100) with the format 8E1. Write out the bits on the wire in order, from the start bit to the stop bit, and say how many bit times the frame takes.
Show answer
Data bits, LSB first, are 0 0 1 1 0 1 0 0. The byte has three 1 bits, which is odd, so even parity needs a parity bit of 1 to make the total four. The frame is: start 0, data 0 0 1 1 0 1 0 0, parity 1, stop 1. That is 1 + 8 + 1 + 1 = 11 bit times.
Exercise
You connect two boards. Board A uses Serial.begin(9600) and board B uses Serial.begin(9600, SERIAL_8E1). Board A sends a steady stream of bytes. Name two things Board B is likely to see, and say why.
Show answer
Board B treats the stop bit of A's 8N1 frame as its parity bit, so about half of the bytes (those where the computed parity does not match) are flagged as parity errors. Board B then expects a stop bit one bit time later, which lands on the next frame's start bit region (LOW), so it also sees framing errors or shifted data. The received data is unreliable even though the baud rate matches.
Exercise
How long does a single 8N1 byte take at 9600 baud, and what is the maximum number of payload bytes per second?
Show answer
A frame is 10 bits, and one bit takes 1/9600 s, so a frame takes 10/9600 s, about 1.04 ms. The maximum is 9600 / 10 = 960 bytes per second.