Search

Mastering UART Communication Basics

Do you recall the era when printers, mice, and modems were connected by thick cables featuring large, cumbersome connectors that had to be physically screwed into your computer? Those devices likely relied on UARTs to communicate with your computer. While USB has largely supplanted those bulky cables and connectors, UARTs remain relevant today. They are commonly employed in do-it-yourself electronics projects to link GPS modules, Bluetooth modules, and RFID card reader modules to devices like Raspberry Pi, Arduino, and other microcontrollers.

UART, short for Universal Asynchronous Receiver/Transmitter, is not a communication protocol like SPI and I2C, but rather a physical circuit within a microcontroller or a standalone integrated circuit. Its primary function is to transmit and receive serial data.

One of UART’s notable advantages is its utilization of only two wires for data transmission between devices. While the fundamentals of UART are straightforward, if you’re unfamiliar, it might be beneficial to start with part one of this series, Mastering the Basics of SPI Protocol Communication.

UART Communication Overview

UART communication involves direct interaction between two UARTs. The process begins with the transmitting UART converting parallel data from a controlling device, such as a CPU, into serial format. This serial data is then transmitted to the receiving UART. Upon reception, the receiving UART converts the serial data back into parallel form for the receiving device. Remarkably, only two wires are required to facilitate data transmission between these two UARTs. Data moves from the Tx (transmit) pin of the transmitting UART to the Rx (receive) pin of the receiving UART.

UARTs transmit data asynchronously, indicating that there is no clock signal orchestrating the output of bits from the transmitting UART to the sampling of bits by the receiving UART. Instead of relying on a clock signal, the transmitting UART incorporates start and stop bits into the data packet being transmitted. These bits delineate the beginning and end of the data packet, enabling the receiving UART to ascertain when to commence reading the bits.

Upon detecting a start bit, the receiving UART initiates the reading process for the incoming bits at a predetermined frequency known as the baud rate. Baud rate serves as a metric for the speed of data transfer, typically expressed in bits per second (bps). It’s imperative for both UARTs to operate at approximately the same baud rate. Deviation in baud rate between the transmitting and receiving UARTs should not exceed approximately 10% to prevent significant timing discrepancies between bits.

Additionally, both UARTs must be configured to transmit and receive the same data packet structure for successful communication.

How UART Operates

The transmitting UART, responsible for sending data, retrieves information from a data bus. This data bus serves as the conduit through which another device, such as a CPU, memory, or microcontroller, sends data to the UART. Initially in parallel form, data is transferred from the data bus to the transmitting UART. Upon receiving this parallel data, the transmitting UART augments it with a start bit, a parity bit, and a stop bit, effectively constructing a data packet. Subsequently, the data packet is serially output, bit by bit, from the Tx pin. On the receiving end, the receiving UART sequentially reads the data packet, bit by bit, at its Rx pin. It then converts the data back into parallel form while stripping away the start bit, parity bit, and stop bits. Finally, the receiving UART transfers the data packet in parallel back to the data bus on the receiving end.

Data transmitted via UART is structured into packets. Each packet comprises one start bit, 5 to 9 data bits (depending on the UART), an optional parity bit, and one or two stop bits.

Start Bit

Ordinarily maintained at a high voltage level when not transmitting data, the UART data transmission line experiences a transition from high to low for one clock cycle to initiate data transfer. Upon detecting this voltage transition, the receiving UART commences reading the bits in the data frame at the baud rate frequency.

Data Frame

The data frame encapsulates the actual data being transmitted. It can range from 5 to 8 bits in length if a parity bit is employed, or 9 bits if no parity bit is utilized. Typically, data is transmitted with the least significant bit first.

Parity

Parity refers to the evenness or oddness of a number. The parity bit serves as a means for the receiving UART to discern if any data has undergone alteration during transmission, which can occur due to factors like electromagnetic interference, mismatched baud rates, or long-distance data transfers. Upon reading the data frame, the receiving UART tallies the number of bits with a value of 1 and verifies if the total is even or odd. In the case of even parity (parity bit is 0), the sum of 1 bits in the data frame should be even; for odd parity (parity bit is 1), it should be odd. A match between the parity bit and the data indicates error-free transmission. Conversely, if the parity bit is 0 and the total is odd, or if the parity bit is 1 and the total is even, the UART detects changes in the data frame.

Stop Bits

To denote the conclusion of the data packet, the sending UART transitions the data transmission line from a low voltage to a high voltage for a minimum of two bit durations.

UART Transmission Process

The transmitting UART retrieves data in parallel from the data bus.

The transmitting UART supplements the data frame with the start bit, parity bit, and stop bit(s).

The complete packet is transmitted serially from the transmitting UART to the receiving UART. Meanwhile, the receiving UART samples the data line at the predetermined baud rate.

Upon reception, the receiving UART removes the start bit, parity bit, and stop bit from the data frame.

Subsequently, the receiving UART converts the serial data back into parallel format and transfers it to the data bus on the receiving end.

Advantages and Disadvantages of UARTs

No communication protocol is flawless, but UARTs demonstrate considerable effectiveness in their operations. Here are some advantages and disadvantages to consider when evaluating their suitability for your project:

Advantages:

  • Utilizes only two wires for communication.
  • Absence of a clock signal requirement simplifies implementation.
  • Incorporates a parity bit for error detection.
  • Flexible data packet structure, provided both ends are configured accordingly.
  • Well-documented and widely adopted communication method.

Disadvantages:

  • Maximum data frame size is capped at 9 bits.
  • Lacks support for multiple slave or multiple master systems.
  • Baud rates between UARTs must be within 10% of each other to maintain synchronization.

To delve deeper into electronic device communication methods, proceed to part three of this series, Exploring I2C: Essential Communication Protocol Basics. Alternatively, if you haven’t already, explore part one, Mastering the Basics of SPI Protocol Communication.

Feel free to share your questions or insights in the comments section! If you found this article informative and wish to receive similar content, consider subscribing to our newsletter for timely updates on new posts.

Related article

Leave a Comment