Skip to content
Tactiverge

Serial communication and UART

How two devices exchange bytes over a single wire each way: baud rate, start and stop bits, parity, and the timing that holds it all together.

Lesson 1 of 6 · 25 min · Self-paced · Free

Why devices need to talk

A robot is rarely one chip. A microcontroller reads sensors and drives motors; a single-board computer runs vision; a GPS module, a motor driver or a wireless radio each have their own processor. They all need to exchange data, and the simplest, oldest and still most common way is serial communication: sending bits one after another down a wire.

Serial is also how you will debug almost everything. The "print a message to the computer" technique from the debugging lesson usually travels over a serial link. Understanding it means understanding the messages you rely on.

Serial versus parallel

You could send a byte over eight wires at once, one per bit. That is parallel communication, and it was common inside older computers. But eight wires (plus ground and control lines) take space, connector pins and cost, and at high speeds the bits on different wires arrive at slightly different times. Sending the bits one at a time on a single wire is simpler and, with good timing, fast enough for most embedded tasks.

UART: serial without a clock wire

A UART (universal asynchronous receiver-transmitter) is a peripheral built into almost every microcontroller. It sends bytes on a transmit pin (TX) and receives them on a receive pin (RX). A two-way link needs just three wires:

  • device A's TX to device B's RX,
  • device B's TX to device A's RX,
  • and a shared ground, so both agree on what 0 V means.

Crossing TX to RX is the single most common wiring mistake: connect TX to TX and nothing works.

"Asynchronous" means there is no shared clock signal. Instead, both sides agree in advance how long each bit lasts. That agreed speed is the baud rate, the number of symbols (signal elements) sent per second; for a basic UART each symbol is one bit, so it equals bits per second. Common values are 9600 and 115200 baud. At 9600 baud each bit lasts 1 / 9600 s ≈ 104.17 µs; at 115200 baud, about 8.68 µs.

The frame: start, data, stop

When nothing is being sent, the line sits high (logic 1); this is called idle. To send a byte, the UART transmits a frame:

  1. Start bit: the line goes low for one bit time. This falling edge tells the receiver "a byte is starting now" and lets it synchronise its timing.
  2. Data bits: usually 8, sent least significant bit first.
  3. Optional parity bit, a simple error check (more below).
  4. Stop bit: the line returns high for at least one bit time, ready for the next start bit.

The most common setting is written 8N1: 8 data bits, no parity, 1 stop bit. Each byte therefore takes 10 bit times: 1 start + 8 data + 1 stop.

Let us encode the letter "A", which is 0x41 or binary 0100 0001:

def uart_frame(byte, parity=None):
    """Return the line levels for one UART frame, in the order they are sent."""
    bits = [0]                                     # start bit (line pulled low)
    data = [(byte >> i) & 1 for i in range(8)]     # least significant bit first
    bits += data
    if parity == "even":
        bits.append(sum(data) % 2)                 # makes the count of 1s even
    bits.append(1)                                 # stop bit (back to idle high)
    return bits

print("A  8N1:", uart_frame(ord("A")))
print("A  8E1:", uart_frame(ord("A"), parity="even"))
print("C  8E1:", uart_frame(ord("C"), parity="even"))

for baud in [9600, 115200]:
    bit_us = 1e6 / baud
    print(f"{baud:6d} baud: {bit_us:6.2f} us per bit, {baud // 10:5d} bytes per second")

Output:

A  8N1: [0, 1, 0, 0, 0, 0, 0, 1, 0, 1]
A  8E1: [0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1]
C  8E1: [0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1]
  9600 baud: 104.17 us per bit,   960 bytes per second
115200 baud:   8.68 us per bit, 11520 bytes per second

("us" stands for microseconds.) Read the first line from left to right as it travels down the wire: a 0 start bit, then the bits of 0x41 starting from the lowest (1, 0, 0, 0, 0, 0, 1, 0), then a 1 stop bit.

Notice the throughput: because each byte costs 10 bits, 115200 baud carries at most 11,520 bytes per second, not 14,400. Framing overhead is the price of not needing a clock wire.

Parity: a one-bit error check

With even parity, the transmitter adds a bit that makes the total number of 1s in the data plus parity even. "A" has two 1s, so its parity bit is 0; "C" (0x43) has three 1s, so its parity bit is 1. If noise flips any single bit, the count becomes odd and the receiver knows the byte is damaged. But if two bits flip, the count is even again and the error slips through. Parity is a weak check, which is why the lesson on protocols adds stronger checksums at the message level.

Timing is everything

With no clock wire, the receiver times each bit with its own clock. After the start bit's falling edge, it waits half a bit time and then samples in the middle of each bit. By the stop bit it has counted about nine and a half bit times, so if the two sides' clocks disagree, the sampling point slides towards the edge of the bit.

As a rough guide, if the sampling point drifts by more than about half a bit over those nine and a half bits, the byte is corrupted. That allows only around 5 % total disagreement between the two ends, so each side should be within a couple of percent. This matters because a microcontroller produces its baud rate by dividing its own clock by a whole number, and the division is not always exact. For example, a 16 MHz clock divided by 16 and then by 9 gives 111,111 baud when you asked for 115200, an error of about −3.5 %, which is risky. Datasheets publish tables of baud-rate errors for this reason. Mismatched baud rates show up as garbage characters, a classic symptom to recognise.

Voltage levels

A microcontroller UART uses its own logic levels, typically 0 V and 3.3 V or 0 V and 5 V. Connecting a 5 V output to a 3.3 V input can damage some chips, so check whether pins are tolerant or use a level shifter. The old RS-232 standard found on some industrial equipment uses much larger voltages, often around ±12 V, and inverted logic. Never connect RS-232 directly to a microcontroller pin; it needs a converter chip. A USB-to-serial adapter at the correct logic level is the usual way to connect a microcontroller to a computer.

Recap

A UART sends bytes one bit at a time with no clock wire, so both ends must agree on the baud rate. Each 8N1 frame is a low start bit, eight data bits least-significant first, and a high stop bit: 10 bit times per byte. Parity catches single-bit errors but not double ones. Baud-rate mismatches of more than a few percent corrupt data, and voltage levels must match. Cross TX to RX, and always share ground.

Project

Paper project: decode a serial waveform

On squared paper, draw the line level (high or low) against time for the two characters "Hi" sent as 8N1, one after the other, with one square per bit time. "H" is 0x48 and "i" is 0x69.

  1. Label the start, data and stop bits of each frame.
  2. Add an even-parity bit to each frame and redraw. Which character's parity bit is 1?
  3. Swap with a friend: each draws a mystery byte (8N1), and the other decodes it to hexadecimal and, if it is a printable character, to a letter.
  4. Calculate how long the two-character message takes at 9600 baud and at 115200 baud.

You can check your drawings with the uart_frame Python function. No hardware is needed.

Deliverables

  • Labelled waveforms for 'Hi' with and without parity
  • A decoded mystery byte
  • Transmission times at both baud rates, with working

Knowledge check

Check your understanding

  1. 1. How many bit times does one byte take with 8N1 framing?
  2. 2. Roughly how many bytes per second can a UART carry at 57600 baud with 8N1 framing?
  3. 3. Two boards are wired TX to TX and RX to RX, with a shared ground. What happens?
  4. 4. With even parity, which data byte gets a parity bit of 1?
  5. 5. A terminal shows random garbage characters when your board sends text. What is the most likely cause?

Go further

Serial communication and UART — Embedded Systems — Tactiverge