Written by SOLO Team
01/06/2022
What is UART

Introduction

Communication and data transfer is an inevitable part of every electronic device. For example, when you want to connect your mouse or printer to your PC, you need to provide a suitable path in order to make the communication possible. The data in an electronic system is a stream of bits, which is to be sent from the origin to the destination. There are two main approaches to sending digital data which are parallel data transmission and serial data transmission. In parallel data transmission, data is sent through more than a wire. For example, eight parallel paths are used to transfer each bit of a byte simultaneously. But in serial data transmission, just one path or wire is used to transmit the data. In this way, data is sent one bit at a time. In old times, when the frequency of data transmission was lower, parallel data transfer was preferred. But today, serial data transfer is by far the most preferred and used. Though serial data transfer increases the data transfer rate, it has some issues. In parallel data transmission, more wire is used which results in more costs and more complicated connectors. This causes the risk of failure in connections significantly. The other issue is crosstalk between data lines which while increasing by the length of the cable, it limits the maximum possible length of data transfer. There are many protocols that organize serial data transmission. UART or universal asynchronous receiver transmitter is one of these protocols. UART is a device-to-device communication protocol that describes how data bits are transferred between a pair of devices.

How UART works

UART is asynchronous serial data transmission. It means it doesn’t need a clock signal to transfer data. This feature reduces the number of wires needed to establish the communication in this protocol. But the two devices must know the frequency of the transmission. In addition to the ground, you just need one wire to send data in every direction. So for one-way communication, only one wire of data is needed and for two-way communication two wires are used. In the following image, you can see how two UART devices are wired together.
What is UART 1
Every UART device has two pins or terminals to do the communication. One of them is called Tx which is used to send data to the other UART device. The other one is called Rx and is in charge of receiving data from the other UART device. Obviously, the Tx and Rx pins of two UART devices must connect in cross form. As it is mentioned before, if just a one-way communication is desired, you can just connect the Tx pin of the transmitter device to the Rx pin of the receiver device.

UART Data Transmission

Serial data transmissions are bitstreams that flow from one device to one or more other devices. These bits can not be transmitted in an arbitrary and random order otherwise different devices will fail to interpret the received data. To Standardize the data bitstream, UART has introduced a pattern that forms data packets. Each packet of the data has the following main parts:

  • Start bit
  • Data Frame
  • Parity
  • Stop bit
What is UART 2

Start Bit

This is the first bit in the UART data packet and declares that the transmitter has started the data transmission. When the data lines are in idle mode, meaning no data is transmitted, data lines are held high. To initiate the data transmission, the transmitter device pulls down the line for one period. So the receiver data starts receiving data.

Data Frame

The next section of the packet is the data frame, which contains the main data. This part can be 5 to 8 bits. If no parity is used, it can also be 9 bit long. The data can be in two different orders which are called MSB(Most Significant Bit) first and LSB(Less Significant Bit) first. When data is sent in MSB first order, The most valuable bit of the data is sent first. On the other hand, When the LSB first order is used, the bit with the lowest value is sent first, and then comes the other bits respectively.

Parity

After sending the main data, the parity bit can be sent. The parity bit is used to ensure that data has not changed during the transmission. Using the parity bit is not mandatory and can be skipped. Parity check has two modes which are even and odd modes. In order to use parity, the transmitter counts the number of ones in the main data frame. If the even mode of parity check is used, and the number of ones in the main data frame is even, the transmitter appends a bit with the value 0, and if the number of ones is odd, it appends a bit with the value 1, to make the number one ones even. That is all the same for the odd parity, despite that the number of ones must be odd. When the receiver receives the data, it counts the number of ones in the main data frame including the parity bit. If the even parity bit is enabled and the number of ones is also even, it approves the data, otherwise, it generates an error. For the odd parity, the number of ones counted by the receiver must obviously be odd.

Stop Bits

The last section of a UART data packet is the stop bits section. This section can be one or two bits long. At the end of the main data frame, the transmitter pulls the data line high for one or two cycles, to declare that it’s the end of the data packet.

Baud Rate

Baud rate is actually the speed of serial data transmission and is the number of bits that are transmitted during a communication every second. In UART the baud rate is between 2400 bit/s to 115200 bit/s. 9600 bit/s and 19200 bit/s are the two most convenient baud rates used for UART. Baud rate can be out of this range but it might not work properly.

RS232

UART is mostly used for onboard applications or maybe when the distance is not more than a few inches far. When it comes to connecting separate devices, like when you connect your PC to a printer, or you have a remote sensor with digital output that should be connected to the controller or the monitoring system, you need a more reliable connection to ensure low noise, more robust and maintainable communication. Especially in industrial applications, where there are a lot more noise sources and also many environmental conditions might be extreme, reliability and maintainability are more critical. In such cases, RS232 can be a good choice. Although today this protocol has been widely replaced with other communication standards like Ethernet, USB, or RS485, it is still a common choice in some areas, especially when dealing with older devices. RS232 or Recommended Standard 232 was introduced back in 1962. It is a standard that tries to provide a simple and reliable data signal standard to establish a connection between a pair of devices.

RS232 Signals Voltage Level

When we need to connect two distant devices, voltage degradation along the wires can be an issue. Due to the impedance of the wires, voltage drops can cause the receiver to fail correctly receive the data. On the other hand as the length of the wire increases, more noise can be induced in the wires and alters the voltages of the signal. Therefore higher voltage margins seem necessary. So In RS232 higher voltage is used. The receiver interprets a data signal voltage between +3 and +25 as 0, and a voltage between -3 and -25 as 1. Although this is the maximum acceptable range for communication, many devices work in lower voltage ranges. As you can see, there is a sufficient amount of voltage margin to compensate for the voltage degradation along the wires. The following image shows an RS232 signal.
What is UART 3

RS232 Data and Control Signals

RS232 generally deals with the voltage characteristics of the signal. But in almost all the cases it uses UART standards to send data packets. So we can say that RS232 is an asynchronous serial data protocol with data packers just like UART.
This standard, in its full version, uses 25 signals (including ground). But in most cases, all these signals are not used. The most common form uses the following 7 signals and ground.

  • TD: This signal carries the sending data.
  • RD: This signal carries the receiving data.
  • CD: Data carrier detect.
  • DTR – Data Terminal Ready.
  • DSR – Data Set Ready.
  • RTS – Request To Send.
  • CTS – Clear To Send.

The two first signals are responsible for sending and transmitting data respectively. Exactly like what Tx and Rx do in the UART. The other 5 signals are control signals which can be used to handle the communication easier and more efficiently.

RS232 Baud Rate

RS232 can carry signals in longer paths. The practical length for the communication is dependent on the frequency and decreases as the baud rate increases. In 19200 b/s, which is a very likable baud rate, the maximum length is about 50 ft. You can see a table for lower frequencies of baud rate.
What is UART 4

RS485

RS485 is a more advanced standard compared to RS232. This Standard was introduced in 1998. The maximum wire length and data rate in this standard are far better than what we have in RS232. Many communication protocols that are widely used in industrial automation, like Modbus and Profibus, are based on the RS485 standard. In addition, many absolute encoders use RS485 for their communication protocols. Unlike RS232, this standard doesn’t use single-ended signals. In RS485 differential signals are used which are a lot more robust and noise resistant. In a single-ended signal, the amount of voltage of a signal to the reference values or ground is measured. But in differential signals, the difference between the two signals is measured. These two signals have values exactly opposite to each other or we can say they are complement signals. Using differential signals eliminates the common mode noise almost completely, especially since wires carrying the two signals are twisted together. RS485 determines no specific amount of voltage except that the two complementary signals must have more than 200 mV voltage difference, otherwise it can’t interpret the received data.
What is UART 5
Like the UART protocol, RS485 has both half-duplex and full-duplex modes. This means the communication between every two devices can be in just one direction at any moment in time. Using differential signals causes the valid frequency range and data rate, to increase drastically. So this protocol can have a baud rate up to 10 Mbits/s and sends the data over 12 meters.

I2C and SPI

Besides UART, there are some other types of serial data protocols. These serial protocols are mostly used in embedded systems to control data transmission between microcontrollers, sensors, ADCs, etc. Two of these serial protocols are called I2C and SPI. Unlike UART, these two protocols are synchronous. In this chapter we want two discuss these two protocols and compare them to UART.

I2C

I2C is a synchronous serial data transmission protocol. This protocol uses two wires to receive and transmit data. One of them is the data pin which is called SDL. It carries the data packets between the connected devices. The other one is the clock pin and is called SCK. This pin shares the clock signal between devices that are connected to the same I2C bus. Since I2C has just one wire for the transmission, it can only work in half-duplex mode. The data rate can be up to 400 kb/s which is faster compared to UART. It is also a multi-master multi-slave protocol, meaning multiple master and slave devices can be connected to the same bus. Each slave device in the I2C protocol has an address. This address is used by the master/masters to determine, which slave should receive and react to the command. All the slaves receive the address. Each slave device whose address corresponded to the sent address, will then receive the command and respond to the slave. By using this addressing technique, data collision is avoided and multi-device communication on the same bus is possible. The diagram below shows a one master multiple slaves architecture.
What is UART 6

SPI

The other popular and well-known serial protocol is SPI or Serial Peripheral Interface. This is a synchronous serial protocol like I2C but is faster compared to both UART and I2C. SPI is a one-master multi-slave protocl and the number of slaves is almost limitless. SPI protocol uses three main I/O s to communicate. One of them is used to transmit data from master to slave and is called MOSI or Master Output Slave Input. Another is used when the master receives data from a the slave. This pin is called MISO or Master Input Slave Output. Since SPI is a synchronous data protocol, master send the clock to slave/s, so ther is another pin wihch is called SCK. When the device is set as master, this pin sends the clock pulses. On the other hand when a device is set as slave, it recives the colck signal through this pin. In addition to the three I/Os, SPI protocol uses other pin/s to select wicht slave must recive the data. This pins which are called CS or chip select, must be as many as slave devices on the bus. Before sending a data, the master determines which slave must recive the data by lowering its CS pin while other CS pins are kept high.As it has two separate lines for transmitting and receiving data, it can work in full-duplex mode. This communication protocol can have a baud rate of up to a few Mb/s which is faster than UART and I2C. In the diagram below, a one master multi-slave SPI architecture is shown.
What is UART 7

UART I2C SPI Comparison Table

Here is possible to see a overview comparison between the protocol

What is UART 8

UART Conversion

Since different devices may use different ports to exchange data, you should be able to convert different types of serial data to each other. In This section we discuss conversion of UART to some other serial data transmission protocols.

UART to RS232

Since RS232 uses the same packets of the UART protocol, converting them to each other is straightforward. There are some chips in the market that can do it, like MAX8323. This IC is the most convenient chip you can find in the market for the job.

UART to USB

USB or universal serial bus is the most convenient serial interface in many applications, especially in domestic applications. Converting a UART connection to a USB is very useful. For example, you have a sensor that communicates by UART protocol. You can connect the sensor to your PC by using a UART to USB converter. There are some chips in the market that convert USB to UART like CH340G. These chips also have modules that make things a lot easier and you don’t need to be worried about designing a PCB.

UART on Arduino Implementation

In this section, a simple example of UART communication using Arduino is described, so that you get a better understanding of how UART works.
You can use UART to send and receive data between an Arduino and a sensor or between two Arduinos.
Let’s first take a look at some functions we use in Arduino for transmitting and receiving through UART.

  • Serial.begin(speed, config): This method is used to initialize a UART communication. It has two arguments which are “speed” and “config”. You can determine the baud rate of data transfer by the speed argument. The default value is 9600 b/s. The Second argument named “config” is used to determine parity, data length, and the number of stop bits. For example “SERIAL_8N1” means that the length is 8 bits, no parity is used and there is 1 stop bit. “SERIAL_7E1” means that the length is 7 bits, even parity is used and there is 1 stop bit. “SERIAL_5O2” means that the length is 5 bits, odd parity is used and there are 2 stop bits. The default value of the config argument is SERIAL_8N1.
  • Serial.write(val): This method is used to transmit data. The argument “val” can be a byte, string. When you want to send an array of bytes it can be used as Serial.write(buff, val), where buff is the name of the buffer to be sent and val is its length.
  • Serial.write(): Using this method, you can read data received by the UART.
  • Serial.print(val) / Serial.println(val): These two methods print data in “val” in ASCII form so that they are readable for humans. The one with “ln” prefix goes to the next line after printing the data

In the code below, the value of a counter is sent to the PC using a UART communication.

0 Comments

Submit a Comment

Your email address will not be published.

Share This

Share This

Share this post with your friends!