EmbeddedRelated.com

UART

Category: Protocols | Also known as: serial port, COM port, async serial

A UART (Universal Asynchronous Receiver-Transmitter) is a hardware peripheral that serializes and deserializes data over two wires (TX and RX) without a shared clock signal, framing each byte with start and stop bits at a pre-agreed baud rate. It is one of the oldest and most widely used communication interfaces in embedded systems.

In practice

UARTs are found in virtually every MCU and are the default choice for debug consoles, bootloader interfaces, GPS modules, Bluetooth serial modules (HC-05, RN42), GSM modems, and host-to-device command interfaces. Because no clock line is required, wiring is minimal: connect TX of one device to RX of the other, and share a common ground. Both sides must be configured to the same baud rate, data bits (almost always 8), parity (usually none), and stop bits (usually 1), collectively written as a format string such as 115200 8N1.

A common pitfall is conflating the UART peripheral with a complete signaling standard. The UART peripheral itself only defines logic-level framing. RS-232, RS-485, and TTL serial are separate electrical standards that sit on top of a UART data stream. Connecting a 3.3 V MCU UART directly to an RS-232 DB9 connector without a level-shifter (such as a MAX3232) will damage the MCU or produce no signal, because RS-232 uses voltages in the range of +/-3 V to +/-15 V.

Because UART is stream-oriented, it has no built-in concept of messages or packets. Received bytes arrive one at a time into a hardware FIFO or a single holding register, and it is the software's responsibility to assemble them into meaningful frames. Without a proper framing protocol layered on top, it is easy to lose sync, especially on reset or reconnect. The blog post "Help, My Serial Data Has Been Framed: How To Handle Packets When All You Have Are Streams" covers practical strategies for this problem.

When a dedicated UART peripheral is unavailable, the protocol can be implemented in software using GPIO toggling, a technique known as bit-banging. This approach is sensitive to interrupt latency and timing accuracy, and its trade-offs are discussed in "Bit-Banged Async Serial Output And Disciplined Engineering". Hardware UARTs are always preferred when available.

Discussed on EmbeddedRelated

Frequently asked

What is the difference between a UART and RS-232?
UART refers to the peripheral and framing mechanism (start bit, data bits, optional parity, stop bits). RS-232 is an electrical signaling standard that uses higher voltages and inverted logic levels. A UART produces TTL or CMOS logic-level signals; a level-shifter IC is required to interface it with RS-232 hardware.
What causes framing errors, and how do I fix them?
A framing error occurs when the receiver does not detect a valid stop bit at the expected time. The most common cause is a baud rate mismatch between the two devices. Check that both sides are configured to exactly the same baud rate, and verify the actual output frequency with an oscilloscope or logic analyzer if the mismatch is subtle (for example, a poorly trimmed internal RC oscillator).
How do I receive UART data reliably in an embedded application?
Use the receive interrupt (RXNE or equivalent) to move each incoming byte into a circular buffer as it arrives, then process the buffer in the main loop or a lower-priority task. Polling the status register in a tight loop risks missing bytes at higher baud rates, especially when interrupts or DMA are also active. DMA-based reception into a ping-pong buffer is the most robust approach for high-throughput or timing-sensitive designs.
UART vs USART: what is the difference?
A USART (Universal Synchronous/Asynchronous Receiver-Transmitter) is a superset of a UART that adds a synchronous mode, using a shared clock line (CK) alongside TX and RX. In practice, USART peripherals are almost always configured in asynchronous (UART) mode, and the terms are used interchangeably in most contexts. The blog post 'An Iterative Approach to USART HAL Design using ChatGPT' illustrates this in the context of STM32-style peripherals.
Does UART transmit the most-significant bit or the least-significant bit first?
Standard UART transmits the least-significant bit (LSB) first. This is worth keeping in mind when manually decoding captures or when implementing a bit-bang transmitter. The blog post 'Endianness and Serial Communication' explores how bit and byte order interact in serial protocols.

Differentiators vs similar concepts

UART is often confused with the complete set of terms around asynchronous serial communication. UART is the hardware peripheral and framing layer only. RS-232 and RS-485 are electrical standards that use a UART data stream but specify voltage levels, connectors, and cable characteristics. USART is a superset peripheral that also supports synchronous clocked mode. "Serial port" and "COM port" are OS-level or connector-level names that typically imply RS-232 signaling. TTL serial refers to UART-framed data at logic-level voltages (3.3 V or 5 V) without any additional line driver.