How UART works
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
- Stop bit
RS232 Signals Voltage Level
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
I2C and SPI
UART I2C SPI Comparison Table
Here is possible to see a overview comparison between the protocol
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
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.