Serial Communication - UART

Many of you would be aware about two types of communications : Serial and Parallel. Coming to Arduino, it communicates with computers or other devices using serial communication. In this type of communication, data is transmitted between two devices one bit at a time. The most common type of serial communication is UART.

Introduction to UART
UART stands for Universal Asynchronous Receiver Transmitter. It takes bytes of data and transmits it in a sequential fashion. At destination, a second UART re-assembles these bits into complete bytes. The communication may be simplex ( one direction only), full duplex ( both devices send and receive at the same time), half-duplex ( device takes turns to transmit and receive).

For UART communication we need COM ( for communication ) ports. But modern computers does not include them and we need external serial to USB converter when using other micro-controllers. Arduino has an on board serial to USB converter so we can directly connect it to the computers. Hence we can send and receive data through the USB connection. This data can be monitored using the Serial monitor in the IDE.

Serial.begin(baud rate) function initiates the serial communication and is also used to set the data transfer rate for the communication. The baud rate indicates how many bits we are going to transfer in one second. Serial.read() tells the Arduino to read data coming from the computer and Serial.println() transmits data from the Arduino to the computer which is displayed on the Serial monitor.

The universal serial bus or USB has four pins in it : RX (receive), TX (transmit), 5V supply and the ground. The 5V line and ground for powering up the Arduino and other two for USART communication. It is one of the best things of UART that it requires just two wires !

If we look at Uno, pin 1 is the TX and pin 0 is the RX for transmitting and receiving data serially. When we call serial functions in the Arduino programming we use these pins to talk back and forth between the computer and Uno. When we do so, the TX and RX LEDs on the Arduino will flash indicating a transfer of data.

It should be noted that the when two devices communicate serially the RX on one is connected to the TX of other and vice-versa. Most serial communications are carried out at either TTL (transistor-transistor logic) level (0-5V) or RS-232 ( -13V ,+13V ).

So, for this type of communication we use two wires. The speed can go to 115200 baud which is 14.4 Kbps. The type of communication is asynchronous as the name suggests and only two devices can interact with each other at a particular time.

How it works ?
UART transmit data asynchronously. Instead of using a clock signal, the transmitting UART adds start and stop bits to data being transferred. It is very similar to the start and stop codon of the DNA when a protein in our body is being synthesized !

The data to be transferred is sent to the transmitting UART via data bus.This data is in parallel form. This UART adds a start bit, parity bit and stop bit to the data creating a data packet. This data packet is output bit by bit at the TX pin. Upon identifying start bit, receiving UART reads this data packet bit by bit at its RX pin. It removes the start bit,parity bit and stop bit and converts the data back to its parallel form. Finally, it transfers this data to the data bus at the receiving end.


Start Bit : When it is not transmitting data, the UART data transmission line is usually held at high voltage. The transmitting UART pulls the transmission line from high to low for one clock cycle, indicating a transfer of data. When this transition is detected, the receiving UART starts reading data.

Data Frame : It contains the actual data being transferred. It can be 5 bit to 8 bit long if parity bit is included. If not, up to 9 bits can be sent. In most cases, the least significant bit is sent first.

Parity Bit : It is a way to tell the receiving UART whether or not the data is changed. Bits can be changed by electromagnetic radiation, mismatched baud rates. When the receiving UART reads the data frame, it counts the total number of bits with value 1 and checks if the total is even or odd. Parity bit is 0 for even parity and 1 for odd parity. So, if the total bits with value 1 is even and parity bit is 0 we would know that the data is free from errors.

Stop bit : The transmitting line is pulled from low to high for at least two bit duration to mark the end of data transfer.

Comments

Popular posts from this blog

The move_base ROS node

Three Wheeled Omnidirectional Robot : Motion Analysis

Overview of ATmega328P