When embedded systems and microcontrollers communicate with each other, how can they achieve that? Let me give an analogy, recently I went to Europe for vacation. There I had to communicate with people to ask for direction, order food or just normally have a chat. In those communications if we don’t speak either Arabic, English or Japanese, which the languages I speak, we could not communicate. I can’t understand them no matter what. English is kind of the default language worldwide but surprisingly a lot of people in Europe cannot speak it.

Anyways, same analogy applies with microcontrollers and embedded systems when they communicate with each other. When microcontrollers agree on a set of rules to form a communication we call that a protocol. And the topic of this article is Universal Asynchronous Receiver Transmitter (UART) protocol, which is widely used in embedded systems.

There are some rules or settings microcontrollers need to agree on before forming a communication (it is the job of the programmer to make sure the settings on both devices are ok). Some examples are: baud rate (speed), transmitter and receiver pins, how many bits to be transmitted at a time, are there bits other than the data itself? And so on.

Until here I introduced the meaning of protocols in embedded systems. But today’s topic is about UART protocol. So let’s talk about it.

UART is Asynchronous

So what is Universal Asynchronous Receiver/Transmitter (UART)? it is simply a serial communication protocol. The protocol is so simple to set up and it requires at least 2 wires for transmit and receive data. As the name show, UART is asynchronous protocol, meaning that the sender and receiver do not share a common clock signal. Not sharing a clock is means that both devices do not have common time/clock, in this case receiver is waiting all the time for the data to come and not knowing exactly when.

Let’s imagine you ordered food online, but you don’t know when the will be delivered and the only way you can know is to go out and check, in this case you need to go and check often enough and hopefully you can catch the delivery one time, this is similar to asynchronous communication. When sender send a byte of data it will send with it a start bit, so the receiver need to check often enough to catch the start bit and the rest of the data. that is why receiver sampling speed is usually set higher than sender speed.

By the way, in case of synchronous communication protocol, you need a dedicated wire for clock so both sender and receiver agree on the same time/clock.

UART Data Frame

So how data in UART is formatted? First we have the data itself which we want to transmit, let’s say 0110 0001 or in Hex 41 and this is one byte. Then we have a starting bit, this is just to let the receiver know that a byte of data is coming. Last we have one or two stop bits to let the receiver know that the byte ended. Sometimes a parity bit is added for error detection. So for each byte of data you need 2 to 4 bits with it which include start bit, stop bit(s) and parity bit. Below is a table to see the whole frame (we call the format to send data with the extra bits a frame)

StartA byteParityStop
00110 000111

One thing to note here is that both sender and receiver need to have the same settings of data frame format.

UART Baud Rate

The baud rate is the speed of data transfer, measured in bits per second (bps). The baud rate determines how fast the transmitter sends bits and how fast the receiver samples bits. The transmitter and the receiver must have the same baud rate settings, otherwise they will not be able to synchronize their timing.

This is different than I mentioned above about sampling the data. In asynchronous communication the receiver doesn’t know exactly when data is coming so by over sampling it can catch the start bit the fast enough and receive the rest of the frame. In UART usually the sampling rate is set 16 times the baud rate, Which means that each serial bit is sampled 16 times.

UART Uses

Since UART is very simple (refer to my sketch in the cover photo to see the cabling). But the it has a limitation which it can only connect two devices. Other protocols can connect multiple devices as we will see in future posts

One area I am interested in is IoT. I saw some people connect for example Bluetooth chip to a microcontroller with UART. Learning this protocol could lead you to build a smart home from simple microcontrollers.

Many development boards like Arduino and STM32 has UART or even multiple UART built in, so you can practice with these.

Summary

UART is serial, asynchronous protocol which is very easy to set up in software and also very few cables. We explained in this post what asynchronous mean, how the frame look like and the baud rate of UART as well. Then we concluded with some uses of UART.