Sub topic 3
Baud Rate and Bit Timing
This chapter assumes you know the parts of a UART frame (idle line, start bit, data bits, optional parity bit, stop bit). It also assumes you can read a basic Arduino sketch and convert between bytes and bits. Here you will learn how baud rate fixes the length of every bit, how that turns into throughput, how the receiver samples bits, and how much clock error a link tolerates.
Baud rate versus bits per second
Baud is the number of signal changes (symbols) per second that a line can carry. A UART line has two levels, so each symbol carries exactly one bit. For UART, 9600 baud therefore means 9600 bits per second on the wire.
This does not mean 9600 bits of your data per second. Start, stop and parity bits take part of that capacity. The figure that matters for your program is the throughput, which is calculated later in this chapter.
UART has no clock line, so baud rate is a setting that both ends must choose in advance. Nothing on the wire announces it.
Bit time
Every bit lasts the same amount of time, called the bit time:
bit time = 1 / baud rate| Baud rate | Bit time | Time per 8N1 byte (10 bits) | Max bytes per second (8N1) |
|---|---|---|---|
| 9600 | 104.17 µs | 1.042 ms | 960 |
| 19200 | 52.08 µs | 521 µs | 1,920 |
| 57600 | 17.36 µs | 174 µs | 5,760 |
| 115200 | 8.68 µs | 86.8 µs | 11,520 |
| 1,000,000 | 1.00 µs | 10 µs | 100,000 |
These numbers set the pace for everything else in a serial design. At 115200 baud on a 16 MHz AVR, one byte lasts about 1,390 CPU cycles. At 1 Mbaud it lasts only 160 cycles, so the receive interrupt handler has little room to spare.
From bit time to throughput
Throughput depends on how many bits one character occupies on the line:
bytes per second = baud rate / bits per frameThe frame format changes the result.
| Format | Bits per frame | Share of bits that are data |
|---|---|---|
| 8N1 | 10 | 80% |
| 8E1 | 11 | 72.7% |
| 8N2 | 11 | 72.7% |
| 7E1 | 10 | 70% |
Take a text line such as Temp: 23.5\r\n, which is 12 bytes. At 9600 baud in 8N1 it needs 12 × 10 / 9600 = 12.5 ms. At 115200 baud it needs 1.04 ms. If you print one such line every loop pass at 9600 baud, the port limits your loop to roughly 80 passes per second, no matter how fast the CPU is.
Measuring transmit time on Arduino
Serial.write() and Serial.print() copy bytes into a transmit buffer and return quickly. The UART hardware then sends them in the background. Serial.flush() waits until the last bit has left. That makes it possible to measure the real transmit time:
void setup() {
Serial.begin(9600);
delay(100);
const char msg[] = "0123456789"; // 10 bytes
unsigned long start = micros();
Serial.write(msg, 10);
unsigned long queued = micros() - start;
Serial.flush();
unsigned long total = micros() - start;
Serial.println();
Serial.print(F("Queued after us: "));
Serial.println(queued);
Serial.print(F("Fully sent after us: "));
Serial.println(total);
}
void loop() {
}On a 16 MHz Uno the "Fully sent" value should be close to 10,400 µs. That is 10 bytes × 10 bits × 104.17 µs. The "Queued" value should be a small fraction of that. micros() has a 4 µs resolution on 16 MHz boards, so expect small differences. Ten bytes fit in the 64-byte transmit buffer, which is why the call returns early. If the buffer were full, write() would block until space was free.
How the receiver samples bits
The receiver has no clock from the sender. It has only its own clock and the falling edge of the start bit. From that edge it must decide when the middle of each bit occurs.
Most UARTs run an internal counter at 16 times the baud rate (AVR hardware can also use 8 times). The receiver follows these steps:
- It waits for the line to fall from idle HIGH to LOW.
- It counts to the middle of the start bit and checks that the line is still LOW. If it is HIGH again, the receiver treats the edge as a glitch and returns to idle.
- It then samples once per bit time, aiming at the middle of each bit. AVR hardware takes three samples near the middle and uses a majority vote, which filters out short noise spikes.
- After the data bits (and parity, if used) it samples the stop bit. The line must be HIGH. If it is LOW, the receiver reports a framing error.
Sampling at the middle gives the maximum margin on both sides. Edges are never perfectly sharp, and the sender's clock may be a little fast or slow.
Clock tolerance
The receiver re-synchronizes only once per frame, at the start bit. After that, the two clocks drift apart for the whole frame. Sampling errors accumulate bit by bit.
In an 8N1 frame the stop bit is sampled at 9.5 bit times after the falling edge. For that sample to stay inside the stop bit, the accumulated drift must be less than half a bit:
9.5 × error < 0.5
error < 5.26%This 5.26% is the theoretical limit on the total mismatch between the two ends. It is an upper bound, not a design target. Edge detection delay, the 1/16-bit sampling granularity, and slow rise times all use up margin. A common design rule is to keep the combined error of both ends under about 2% to 3%. Longer frames such as 8E2 have less margin because the last sample is later.
How the AVR baud rate is generated
On an ATmega328P (Uno, Nano) the UART derives its bit clock by dividing the CPU clock. It cannot produce every baud rate exactly, because the divisor must be a whole number:
normal mode: baud = F_CPU / (16 × (UBRR + 1))
double speed (U2X): baud = F_CPU / (8 × (UBRR + 1))UBRR is the divisor register. When you call Serial.begin(), the Arduino core calculates it for you and prefers double-speed mode when it gives a smaller error. On 16 MHz boards it also makes an exception for 57600, using normal mode there for bootloader compatibility.
The sketch below calculates the real baud rate and the error for both modes. The formulas are for the ATmega328P, and F_CPU comes from the board definition:
const uint32_t rates[] = {9600, 57600, 115200, 250000, 1000000};
uint32_t actualBaud(uint32_t baud, uint8_t perBit) {
uint32_t scaled = (uint32_t)perBit * baud;
uint32_t divisor = (F_CPU + scaled / 2) / scaled; // equals UBRR + 1, rounded
return F_CPU / (perBit * divisor);
}
void report(uint32_t baud, uint8_t perBit) {
uint32_t actual = actualBaud(baud, perBit);
float err = 100.0f * ((float)actual - (float)baud) / (float)baud;
Serial.print(F(" actual "));
Serial.print(actual);
Serial.print(F(", error "));
Serial.print(err, 2);
Serial.println(F("%"));
}
void setup() {
Serial.begin(9600);
for (byte i = 0; i < 5; i++) {
Serial.print(rates[i]);
Serial.println(F(" baud"));
Serial.print(F(" normal:"));
report(rates[i], 16);
Serial.print(F(" U2X:"));
report(rates[i], 8);
}
}
void loop() {
}For a 16 MHz clock the main results are:
| Requested | Mode | Actual | Error |
|---|---|---|---|
| 9600 | normal | 9615 | +0.16% |
| 57600 | normal | 58,824 | +2.12% |
| 57600 | U2X | 57,143 | −0.79% |
| 115200 | normal | 111,111 | −3.55% |
| 115200 | U2X | 117,647 | +2.12% |
| 250000 | normal | 250,000 | 0% |
| 1,000,000 | normal | 1,000,000 | 0% |
115200 baud is the usual trouble spot. In normal mode the −3.55% error alone is above half of the practical budget. Double-speed mode is better at +2.12%. If the other device has an error of −2% in the opposite direction, the total mismatch is already above 4%.
Round rates such as 250000 and 1,000,000 divide a 16 MHz clock exactly. Boards running at other clock speeds produce different error figures for the same rates.
What a mismatch looks like
Suppose the sender transmits A (0x41) at 9600 baud and the receiver is set to 19200. Data goes out least significant bit first, so the line carries: start 0, then 1 0 0 0 0 0 1 0, then stop 1. Each bit lasts 104 µs. The receiver's bit time is 52 µs.
After the falling edge at t = 0 the receiver samples at 26 µs (start check), then at 78, 130, 182, 234, 286, 338, 390 and 442 µs for the data bits. Compared with the sender's bit slots, those samples read the sender's start bit, bit 0 twice, bit 1, bit 1 again, and so on. Bits 0 to 7 come out as 0, 1, 1, 0, 0, 0, 0, 0. That is the value 0x06. The stop sample at 494 µs lands inside the sender's third data bit, which is 0, so the receiver also reports a framing error.
That outcome is typical. A wrong baud rate gives wrong values and often framing errors. You may see unprintable characters on the terminal. The troubleshooting chapter returns to this symptom.
Choosing a baud rate
Pick the rate from the actual requirements:
- Both ends must be able to produce it. Check the error figure for each side, not only the nominal rate.
- Use the lowest rate that meets the data volume. Lower rates tolerate longer cables and noisier wiring.
- Estimate the volume with the formula. Records per second × bytes per record × bits per frame must stay well under the baud rate. Keep at least 20% headroom.
- Check the CPU load. At high rates the receive interrupt fires very often. If your loop blocks for long, bytes can overflow the buffer.
- Use standard values (9600, 19200, 38400, 57600, 115200) when talking to unknown devices, because their dividers are often tuned for those.
Exercise
A sensor sends 20-byte records in 8E1 format at 38400 baud, back to back. How long does one record take on the wire, and how many records per second is the maximum?
Show answer
8E1 uses 11 bits per frame, so a record is 20 × 11 = 220 bits. At 38400 baud that is 220 / 38400 = 5.73 ms per record. The maximum rate is 38400 / 220 ≈ 174 records per second.
Exercise
A transmitter runs 2.5% fast and a receiver runs 3% slow, so the two ends are about 5.5% apart. Why is an 8N1 link between them likely to fail, even though each error looks small?
Show answer
The errors add up to a 5.5% mismatch. The stop bit is sampled at 9.5 bit times after the start edge, so the sample point drifts by about 9.5 × 5.5% ≈ 0.52 bit. That is past the half-bit limit, so the stop sample lands in a neighboring bit and the receiver reports framing errors or wrong data.