What is the UART communication protocol

In the world of devices, communication protocols are what languages are to humans – a way to communicate with each other. Throughout technology’s rapid development, many communication protocols have been created. Too many to mention. Thankfully, you’ll only need to know a handful when working with Arduinos and electronics. One of them is UART, likely the easiest to understand.
UART in simple terms
UART is short for the universal asynchronous receiver/transmitter. It defines a set of rules for exchanging serial data between two devices. As the name suggests, it both sends and receives data. UART is one of the simplest communication protocols there is. It requires only two wires for the devices to communicate with each other. The transmitter (TX) wire on device 1 is connected to the receiver (RX) wire of device 2. Likewise, the transmitter (TX) wire on device 2 is connected to the receiver (RX) wire of device 1. Ground (GND) wire is needed to keep both devices at the same reference voltage. This wire is almost always present in any type of communication between devices.
There are three systems of communication in UART: simplex, half-duplex and full-duplex. In simplex, data is sent in only one direction. In half-duplex, each side can send data, but only one at a time. In full-duplex, both sides can send data simultaneously. This data is sent in form of frames.
Baud rate
As the letter A in the acronym indicates, UART is asynchronous. The transmitter and receiver devices don’t share a common clock signal. While this simplifies the protocol, it sets up particular requirements on both devices. Mainly, they have to have the same baud rate or prearranged speed to communicate. Otherwise, the message will get distorted and misinterpreted.
The baud rate is, simply put, the rate at which information is transferred in a communication channel. The most common UART baud rates today are 4800, 9600, 19200, 57600, and 115200. The higher the baud rate, the more sensitive the cable used becomes to the quality installation. For high baud rates, it is recommended to use shorter cables.
But what exactly would the 9600 baud rate mean? It means a device can send up to 9600 bits, or ones and zeros, per second. For UART and most other serial communications, the baud rate must be the same on both transmitter and receiver devices.
How UART works
UART communication is done in the so-called frames. Each frame is made of bits – pieces of information sequentially sent over a line. These bits, in order, include the start bit, data bits, an optional parity bit, and one or two stop bits. Every bit can have either high (indicating logical 1) or low voltage (indicating logical 0). These are sometimes called “mark” for high and “space” for low signal.
The idle state (when no data is being sent or received) is high-voltage. This idea was taken from telegraphy, where the line held high indicated the transmitter was not damaged.
Unlike some other communication protocols, UART doesn’t have the concept of controllers and peripherals. It is the default choice for an easy interface when only two devices need to talk to each other.
Start bit
UART, as the name suggests, is asynchronous. This means that there is no shared clock line, like in the I2C communication protocol . To indicate when the data is about to be sent, a start bit is sent first. Essentially, this means that the signal will go from the idle high state to a low state. After the start bit, the data bits follow immediately.
Data bits
The data bits are the ones that actually carry the message. There can be between 5 and 9 data bits, but 7 or 8 are most commonly used. They are sent over the line with the least significant bit (LSB) first.
Let’s say we wanted to send the first letter of our name, the capital letter S, with UART. The 7-bit ASCII sequence for this character is 1010011. Since we want to send the bits in order from least to most significant, we would reverse our 7-bit sequence. Thus, it would be 1100101.
Looking at that sequence, the first two bits would be high, followed by two low bits. Next are another high, then low, and then high bit again to finish our capital letter S ASCII sequence.
Parity bit (optional)
To check if there are errors in a sequence or if the data was changed, the UART uses an optional parity bit. The data bits can be changed in several ways, such as mismatched baud rates, long-distance data transfers, etc. A parity bit can be found between the last data bit and a stop bit. The value of the bit will depend on the type of parity used, either even or odd.
Both even and odd parity look at the number of ones in a frame. In the even parity, the parity bit is set so the total number of data bits, including the parity bit, is even. Likewise, in the odd parity, this number is supposed to be odd.
Let’s get back to our example of sending capital letter S with UART. The ASCII sequence has three zeroes and four ones. If even parity is used, the parity bit will be 0 because there is already an even number of ones in the frame. If odd parity is used, however, the parity bit must be 1 so the total number of ones is odd.
When both devices agree upon an even parity, the receiver is supposed to get an even number of ones, including the parity bit. If the receiver gets an odd number of ones, the data is corrupted and it will eventually be discarded. The receiver will then wait for a new frame to be sent.
If this confuses you at the moment, just remember: even parity means an even number of ones when looking at both the data bits and the parity bit, and odd parity means an odd number.
When it comes to the parity bit, there are two things to keep in mind. First, it can detect only a single flipped bit. If more than one bit is flipped, those changes cannot be reliably detected. Second, the parity bit is optional in UART and is not always used.
Stop bit(s)
Once the data bits and the optional parity bit are finished, the stop bit comes to finish the frame. Unlike the start bit where the signal goes from high to low, in the stop bit, it comes from low to high. If the signal was high before reaching the stop bit, it will remain high.
At least one stop bit is required in a frame. A second, optional, stop bit can be configured. This gives the receiver more time to get ready for the next frame. This is, however, an uncommon practice and you won’t see it often.
Pros and cons
Compared to some other communication protocols, UART is very simple. Because of that, you’d benefit from using it for smaller projects . This becomes especially apparent if you only have two devices communicating with each other. However, if you have a project with three or more devices, you’ll find more luck looking elsewhere.
Pros
- Uses only two wires
- A clock signal is not necessary
- Parity bit enables checking for errors
- Costs less and is smaller than parallel communication
- Supports the distance of about 15 meters
Cons
- Data frame limited to a maximum of 9 bits
- Doesn’t support multiple controllers and peripherals communication
- Not suitable for applications requiring higher data transmission rates
- Data transfer speed lower than with parallel communication
Devices need to be on the same baud rate
Real-life applications
While it is maybe not used as much today, UART was all the rage back in the day. Protocols like I2C and SPI are slowly replacing UART in chips and components. Most modern computers and peripherals will use USB and ethernet technologies instead.
That doesn’t mean UART is obsolete. Devices using the RS-232 interfaces, such as external modems, are a good example of where UART is still used. Microcontrollers and microcontroller-based gadgets commonly have at least one integrated UART peripheral. Specialized UARTs can be found in cars, SIMs, and smart cards. Because of its simplicity, low cost, and easy implementation, UART is used for lower speed, lower throughput applications. It is mostly used as a form of device-to-device communication.
Now that you have a clear understanding of UART and how it works, we would recommend reading up on I2C and SPI protocols as well. Understanding these three protocols will help you when working with more complicated things along the way.
Products used in this tutorial
No products found