0
0
Power-electronicsConceptBeginner · 4 min read

What is UART in Embedded C: Simple Explanation and Example

UART (Universal Asynchronous Receiver/Transmitter) is a hardware communication protocol used in embedded C to send and receive data serially between devices. It converts data between parallel and serial forms, allowing microcontrollers to communicate with other devices using just two wires: one for sending and one for receiving data.
⚙️

How It Works

Imagine two friends passing notes across a table one letter at a time. UART works similarly by sending data bit by bit over a single wire. It uses a start bit to signal the beginning of data, followed by the data bits, an optional parity bit for error checking, and stop bits to mark the end.

This process is asynchronous, meaning both devices agree on a speed (baud rate) but do not share a clock signal. The receiver listens for the start bit and reads the bits at timed intervals to reconstruct the original data.

💻

Example

This example shows how to initialize UART on a microcontroller and send a simple message using embedded C.

c
#include <avr/io.h>
#include <util/delay.h>

void UART_init(unsigned int baud) {
    unsigned int ubrr = F_CPU/16/baud - 1;
    UBRR0H = (unsigned char)(ubrr>>8);
    UBRR0L = (unsigned char)ubrr;
    UCSR0B = (1<<TXEN0); // Enable transmitter
    UCSR0C = (1<<UCSZ01) | (1<<UCSZ00); // 8-bit data, no parity, 1 stop bit
}

void UART_transmit(char data) {
    while (!(UCSR0A & (1<<UDRE0))) ; // Wait until buffer empty
    UDR0 = data;
}

int main(void) {
    UART_init(9600); // Initialize UART at 9600 baud
    const char *msg = "Hello UART!\r\n";
    while (*msg) {
        UART_transmit(*msg++);
        _delay_ms(100);
    }
    while(1) {}
    return 0;
}
Output
Hello UART!
🎯

When to Use

Use UART when you need simple, low-speed communication between microcontrollers, sensors, or computers. It is common in embedded systems for debugging, sending commands, or receiving data from GPS modules, Bluetooth devices, or serial terminals.

UART is ideal when you want to connect devices without complex wiring or clock synchronization, making it perfect for many small electronics projects.

Key Points

  • UART sends data serially using start, data, parity, and stop bits.
  • It is asynchronous and requires both devices to agree on baud rate.
  • Uses two wires: one for transmit (TX) and one for receive (RX).
  • Commonly used for simple device communication and debugging.
  • Easy to implement in embedded C with hardware support on most microcontrollers.

Key Takeaways

UART is a simple serial communication protocol used in embedded C for device data exchange.
It sends data bit by bit asynchronously using start and stop bits without a shared clock.
UART requires only two wires: transmit and receive, making wiring easy.
It is widely used for debugging and connecting sensors or modules in embedded systems.
Embedded C code can initialize UART hardware and send data with simple functions.