🚀 Practice real-world tasks & problems for UART to build pro-level skills — Click here

UART Basics 

UART (Universal Asynchronous Receiver/Transmitter) is a full-duplexasynchronous serial communication protocol that uses two wires, Tx and Rx.

Note: RTS/CTS ( Request To Send / Clear To Send) hardware data flow control lines. Those are Optional.

Short-distance communication  <10–50 cmwhen using TTL voltage levels.

Limitations
     1. Point-to-point communication only (not multi-master/slave like I2C/SPI)
     2. Slower speed than SPI/I2C.

UART is commonly used for debugging with serial monitors, communicating with Bluetooth/Wi-Fi modulesGPS receiverssensors, etc.

Baud Rate

  • Baud rate is the number of bits transmitted per second.

For example, the baud rate is  4800, 9600, 19200, 57600, and 115200, etc.

  • The clock signal is not present for synchronization, so the Tx/Rx sides must use the same baud rate for data transfer.
  • Timing formula

1 bit transmission duration = 1 / Baud rate 

E.g., 9600 baud, then  104.167 μs required to transmit 1 bit, and 9600 bits transmitted per second.

Communication Flow

UART is asynchronous(No clock is used), so both devices must use the same baud rate and data format for correct data transmission.

Frame Structure 

Frame Description

Bit NameDescriptions
Idle TX and RX lines stay HIGH when idle
Start bit (ST)
  • 1 bit (always logic 0)
  • The line goes from idle (HIGH) to LOW, the receiver starts reading bits
Data bitsA number of data bits can be 5,6,7,8, or 9.
Parity(P)
  • 1 bit(Either HIGH OR LOW depend on Even/Odd)
  • Used for error checking
  • Optional
Stop Bit (Sp)
  • 1 or 2 bits (always logic 1).
  • Line goes back to HIGH after the last bit, frame complete.

 

UART Signal Logic Levels: TTL UART and  RS-232

The RS-232  is used for long-distance serial communication based on UART between two controllers.

 TTL UARTRS-232
Logic Levels

LOW: 0V

HIGH : 3.3V / 5V

LOW : +3V TO +25V

HIGH : -3V TO -25V

Logic PolarityNon-InvertedInverted
DistanceShort-range (<10 - 50 cm)Longer-range (up to 15m)
InterfaceDirectly with the Tx/Rx pins of a microcontroller. 

Level shifter(e.g.MAX232 ) is used to connect RS-232 to controller

(Directly connecting a controller to RS-232 can damage it due to incompatible voltage levels.)

UsageMicrocontrollers, Embedded SystemsOld PCs, Industrial Devices

Error Detection

  • Parity Bit: Checks if the number of 1’s in the data is even/odd.
    • Even Parity: The Total number of 1s in the data is even.
    • Odd Parity: The Total number of 1s is odd.
    • No Parity: No error-checking bit is sent.
  • Framing Error: If the stop bit is not received correctly
  • Overrun Error: If a buffer is full and new data arrives

 

UART in Microcontroller 

As an important communication protocol, it is present in all controllers, i.e., AVR, PIC, and ARM.

Understanding of UART considering the AVR (ATmega328P)controller.

Generally following registers are used for UART communication.

RegisterDescription
UCSRA (8-bit)USART Control and Status Register A: holds flags for UART transmission and reception status, data register empty, and error flags.
UCSRB (8-bit)USART Control and Status Register B: Controls the enabling/disabling of transmission, reception, and interrupts.
UCSRC (8-bit)USART Control and Status Register C: Configures UART baud rate, frame format, and communication mode
UBRR (16-bit)USART Baud Rate Registers:
Contains the baud rate setting for UART communication
UDR (8-bit)USART Data Register: 
Holds the data to be transmitted or received via UART

Fosc = System clock Frequency, e.g., 16MHz in Arduino UNO

UART Drivers

UART Data Transmission Driver

  1. Set Baud Rate: Configure the baud rate for communication.
  2. Configure Frame Format: Set data bits, parity, and stop bits.
  3. Enable Transmission: Enable the UART transmission to start sending data.
  4. Wait for the Data Register to Be Empty
  5. Write Data to UART data buffer: Write the data byte to the UART data buffer for transmission.
  6. Wait for Transmission to Complete.
  7. Optional: Disable Transmitter.

UART Data Reception Driver

  1. Enable Receiver: Enable the UART receiver to start receiving data.
  2. Wait for Data to Be Received: Wait until data is available in the UART data register.
  3. Read Data from UDR: Read the received data byte from the UART data register.
  4. Check for Errors: Ensure no framing, parity, or overrun errors occurred during reception.
  5. Process Received Data
  6. Optional: Disable Receiver

UART in Arduino UNO 

Pins In Arduino Board

Arduino UNO Functions of UART Protocol

FunctionDescription
Serial.begin(baud)Initialize UART
Serial.print()Send data
Serial.println()Send data with a newline and carriage return
Serial.read()Read 1 byte
Serial.available()Check if data is available
Serial.write()Send raw bytes
Serial.end()Disable UART
Serial.availableForWrite()Get a number of bytes available for writing in the Serial buffer.
Serial.find() Reads data from the serial buffer until the target is found.
Serial.flush()Waits for the transmission of outgoing serial data to complete.
Serial.parseFloat()Returns the first valid floating-point number.
Serial.peek()Returns the next byte in the RX buffer without removing it.

 

Note:
1.The hardware serial library has a 64-byte buffer for data transmission/reception.
2.The serial library uses an interrupt to transmit data.

Example

Send character 'A' from one Arduino UNO to another via UART; on receiving 'A', the second UNO toggles an LED.

Hardware Connection Diagram

Arduino UNO 1 Code

void setup() {
  Serial.begin(9600); // Initialize UART at 9600 baud rate
}
void loop() {
  Serial.write('A');  // Send a character
  delay(1000);        // Send after every 1 second
}

Arduino UNO 2 Code

void setup() {
  Serial.begin(9600); // Initialize UART at 9600 baud rate
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  if (Serial.available() > 0) {
    char receivedChar = Serial.read(); //Read the received Char
    if('A' == receivedChar)
    // Toggle LED when data is received
    digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
   
  }
}