C12 · Lesson 28 · Serial communication and the UART
Bytes, one bit at a time, on a single wire
The oldest and most common way for a microcontroller to talk is the UART: one wire out, one wire in, no clock, and both ends agreeing in advance on how fast bits go by. It carries debug messages to a terminal, commands from a computer, and readings from GPS modules, modems and sensors. Each byte travels as a frame: a start bit, eight data bits, and a stop bit. Because there's no shared clock, the receiver times each bit from the start bit's edge, so both ends' baud rates must match to within a few percent.
- The program sends a greeting to the terminal, then echoes back anything typed. The terminal types "hi" at 2 ms.
- Zoom in on the first byte of TX: it idles high, drops for the start bit, then sends 'H' (0x48) least significant bit first, then a high stop bit. Each bit lasts 8.7 µs at 115,200 baud.
- Change
BAUDto 9600 and run again: the same text takes twelve times as long, and its frames stretch across the timeline.
Predict
At 115,200 baud with 8 data bits and one stop bit, how many bytes per second can a UART send at most?
11,520: every byte travels as 10 bits (a start bit, 8 data bits, a stop bit), so the byte rate is the baud rate divided by 10. The start and stop bits are the price of not sharing a clock.
Name it
The frame
The line idles at 1. A frame is:
- a start bit, 0: its falling edge tells the receiver a byte is coming;
- eight data bits, least significant first;
- a stop bit, 1, returning the line to idle.
Each bit lasts ; a frame lasts . Common rates: 9600, 115,200 and up to a few megabaud. Some links add a parity bit or a second stop bit.
Matching rates
The receiver samples each bit near its middle, timing from the start bit's edge. By the last bit it's nine and a half bit-times in, so a rate mismatch of about 5% (between the two ends combined) shifts the sample out of its bit. Keep each end within about 2%. This board divides its clock:
Using it
| C | wait while UART_STATUS & UART_TX_FULL, then write UART_DATA; when UART_STATUS & UART_RX_READY, read UART_DATA |
| MicroPython | u = UART(0, 115200); u.write('text'), u.any(), u.read(n), u.readline() |
The transmitter holds one byte waiting while another shifts out. The receiver holds just one received byte: if another arrives before the program reads it, the first is lost and UART_OVERRUN is set (lesson 29 fixes that with interrupts and a buffer).
Levels
This board's pins use 3.3 V logic ("TTL serial"). A computer connects through a USB-to-serial adapter; the old RS-232 standard uses ±3 to ±15 V and an inverting driver chip.
Worked example
Draw the frame for 'A' (0x41).
Show the solution
- Write it in binary.
- Send it LSB first. The data bits are 1, 0, 0, 0, 0, 0, 1, 0.
- Frame it. On the wire: idle 1, start 0, then 1 0 0 0 0 0 1 0, then stop 1.
- Time it. That's ten bit-times of 8.68 µs each at 115,200 baud.
Answer idle 1, start 0, data 1 0 0 0 0 0 1 0, stop 1: ten bit-times of 8.68 µs.
Worked example
What BAUDDIV gives 115,200 baud, and how far off is it?
Show the solution
- Divide the clock by the baud rate. so BAUDDIV = 87.
- The rate it gives.
- How far off. That's 0.22% slow, comfortably inside the 2% budget.
Answer BAUDDIV = 87, which gives 114,943 baud, 0.22% slow.
Worked example
How many bytes per second does a 115,200-baud link carry with 8N1 framing, and with 8N2 (two stop bits)?
Show the solution
- 8N1. Each byte is 10 bit-times:
- 8N2. Each byte is 11: 9% less.
- When it's worth it. The extra stop bit only helps receivers that need more time between bytes.
Answer 11,520 bytes/s with 8N1 and 10,473 bytes/s with 8N2.
Check
How long is one bit at 57600 baud?
Write units the way you normally would: 1.5 µs, 1500 ns and 0.0015 ms are the same answer.
Check
How long does it take to send 1 bytes at 9600 baud, back to back?
Write units the way you normally would: 1.5 ms, 1500 µs and 0.0015 s are the same answer.
Check
What BAUDDIV comes closest to 38400 baud on the 10 MHz board?
Check
With BAUDDIV rounded, how far off 230400 baud is the board, in percent (negative if slow)?
Check
The byte 4B is sent. Write its eight data bits in the order they appear on the wire, first bit on the left, as a binary number.
Check
What's the most bytes per second a UART can send at 4800 baud, with 8 data bits and one stop bit?
Check
A terminal shows garbled characters from a board. What's the most likely cause?
Check
Send the text N= followed by the number N (307) in decimal and a newline, at 115,200 baud. (Convert the number to digits yourself.)
C on the course's RISC-V board (10 MHz, no floating point). #include "board.h" for the registers; print(x) shows a number. Tab indents; press Esc and then Tab to move on.
Check
Wait for a line from the terminal and send it back in capitals.
MicroPython on the course's board: machine (Pin, PWM, ADC, DAC, UART, I2C, SPI, Timer, WDT) and time. Each call into the hardware takes about 40 µs. Tab indents; press Esc and then Tab to move on.
Check
The UART runs at baud; 57600 baud is wanted.
(a)
What BAUDDIV?
(b)
How far off is the actual rate?
Check
A UART at 115200 baud uses 8 data bits, no parity and 1 stop bit(s).
(a)
How long does one byte take?
Write units the way you normally would: 1.5 µs, 1500 ns and 0.0015 ms are the same answer.
(b)
How many bytes per second?
Explain why
Why does a UART frame need a start bit and a stop bit when the data is only eight bits?
There's no clock wire, so the receiver must find where each byte begins and time its bits itself. The idle line sits at 1, so a start bit of 0 makes a falling edge the receiver can't miss; it measures every bit from that edge, sampling each in its middle. The stop bit of 1 returns the line to idle, guaranteeing the next start bit makes a fresh falling edge, so the receiver resynchronizes on every byte. That resynchronization is why small baud-rate differences don't accumulate: they only have to stay within half a bit over the ten bits of one frame.
Practice
Keep practicing
12 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.