Introduction to UART communication, how UART works

Author: Release time:2022-09-09 Source: Font: Big Middle Small View count:248

Remember the old days of printers, mice and modems? They all had huge, bulky connectors and thick cables and had to be screwed into your computer. These devices use the UART protocol to communicate with your computer.


While USB has almost completely replaced the old cables and connectors, the UART is not obsolete. You will find GPS modules, Bluetooth modules, RFID reader modules, etc. connected to a Raspberry Pi, Arduino or other microcontroller using UARTs in many projects today.


The Universal Asynchronous Receiver/Transmitter, often called a UART, converts the data to be transmitted between serial and parallel communication. The UART is usually integrated into other communication interfaces as a chip that converts parallel input signals to serial output signals.


It is not a communication protocol like SPI and I2C but a separate physical circuit or a separate IC in the microcontroller.


The best thing about UART is that it uses only two wires to transfer data between devices, and the principle behind UART is easy to understand.


Introduction to UART communication


In UART communication, two UARTs communicate directly with each other. The transmitting UART converts the parallel data from the control device, such as the CPU, into serial form and sends it serially to the receiving UART, which then converts the serial data back to the parallel data of the receiving device. Only two wires are required to transfer data between the two UARTs. Data flows from the transmitting UART's Tx pin to the receiving UART's Rx pin.

image.png


The UART sends data asynchronously, meaning there is no clock signal to synchronize the bit output of the sending UART with the bit sampling of the receiving UART. Instead of clocking signals, the transmit UART adds start and stop bits to the transmitted packet. These bits define the start and end of the packet, so the receive UART knows when to start reading bits.


When the receive UART detects the start bit, it begins reading the input bits at a specific frequency called the baud rate. Baud rate measures data transfer speed expressed in bits per second (bps). Both UARTs must operate at approximately the same baud rate. The baud rates between the transmit and receive UARTs can only differ by about 10%.


UARTs must also be configured to send and receive the same packet structure.

image.png


How UARTs work


The UART relies on the UART bus, which sends data to the UART through other devices such as the CPU, memory or microcontroller. Data is sent to the UART in parallel from the data bus. after the UART gets the parallel data from the data bus, it adds the start bit, parity bit and stops bit to create the data packet. Next, the packet is output serially bit by bit on the Tx pin, and the UART receiver reads the packet bit by bit on its Rx pin. Then, the receive UART converts the data back to parallel form and removes the start bit, parity bit and stop bit. Finally, the receive UART transfers the packets in parallel to the data bus at the receiver.

image.png


The data transmitted by the UART is organized into packets. Each packet contains 1 start bit, 5 to 9 data bits (depending on the UART), optional parity bits and 1 or 2 stop bits.

image.png


1. Start bit


When the UART data transmission line is not transmitting data, it is normally held at a high voltage level. To start data transfer, the transmitting UART pulls the transmission line from high level to low level for one clock cycle. When the receive UART detects a high to low voltage transition, it starts reading bits in the data frame at the baud rate frequency.


2. Data Frame


The data frame contains the actual data to be transmitted. It can be 5 bits to 8 bits long if parity bits are used. If no parity bits are used, the data frame can be 9 bits long. In most cases, the data is sent first with the least significant bit.


3. Parity bits


Parity describes the evenness or oddness of a number. Parity bits are a way for the receiving UART to determine if any data has changed during transmission. Data can change during electromagnetic radiation, mismatched baud rates, or long-distance transmission. After the receiving UART reads the data frame, it counts the number of bits with value 1 and checks whether the total is even or odd.


If the parity bit is 0 (even parity), the 1 bits in the data frame should total an even number. If the parity bit is 1 (odd parity), the 1 bits in the data frame should total an odd number. When the parity bit matches the data, the UART knows that the transmission is error-free. However, if the parity bit is 0, 1 bit should total to odd; or if the parity bit is 1 and 1 bit should total to even, the bits in the data frame have changed.


4. Stop bit


To notify the end of a transmitted packet, the UART transmitter drives the data transmission line from a low voltage to a high voltage for at least two-bit durations.


UART transmission steps


1) Send UART receives data in parallel from the data bus ↓↓

image.png


2) The transmitting UART adds the start bit, parity bit and stop bit to the data frame ↓↓

image.png


3) the entire data packet is sent serially from the transmitting UART to the receiving UART. the receiving UART samples the data lines at a pre-configured baud rate↓↓

image.png


4) The receive UART discards the start, parity and stop bits from the data frame ↓↓

image.png


5) The receive UART converts the serial data back to parallel and transfers it to the data bus at the receiver side↓↓

image.png


Advantages and disadvantages of UART


Pros: uses only two wires; does not require a clock signal; has a parity bit; can change the structure of the packet once both sides are set; is fully documented and has wide use.


Disadvantages: data frame size is limited to a maximum of 9 bits; multiple enslaved persons or multiple master systems are not supported; baud rate per UART must be within 10%.


Hot News

Hot product