Home / Learn Chip Programming / UART Serial Communication
UART Serial Communication
UART is the simplest way for two chips to talk: two wires, no clock, agreed speed. It is how you print debug messages from a chip.
Theory in plain words
What it is
UART (Universal Asynchronous Receiver Transmitter) sends data one bit at a time over a wire with no shared clock. Each side uses its own timer set to the same speed, the baud rate.
Why we use it
It needs only TX, RX and ground, is supported by every microcontroller and is the base of debug consoles, GPS modules, Bluetooth modules (HC-05), RS-485 and the Arduino Serial Monitor.
How it works
The idle line is high. A falling edge is the start bit. The receiver then samples the line in the middle of each bit time: 8 data bits (LSB first), optional parity, and a high stop bit. 9600 baud means 9600 bits per second.
Where it is used
- Debug output (printf over serial)
- GPS, GSM, Bluetooth and Wi-Fi modules
- Talking to a PC over USB-serial
- RS-232, RS-485 and Modbus industrial links
One 8N1 frame
8N1 means 8 data bits, No parity, 1 stop bit. Sending the letter "A" (0x41 = 01000001) takes 10 bit times: 1 start + 8 data + 1 stop.
Try 115200 baud: a character takes about 87 us. Try the letter "a" and "A" to see that only bit 5 differs (upper and lower case differ by 0x20).
Baud rate and clock error
The chip makes the baud rate by dividing its clock. The divider is an integer, so the real rate can be slightly off. If the error is over about 2 to 3 percent, characters become garbled.
At 16 MHz and 115200 baud the normal mode error is about 3.5 percent (too much) and double speed mode about 2.1 percent (borderline), which is why people choose another crystal. Change the clock to 14745600 and the error becomes 0.
UART in C (AVR)
#include <avr/io.h>
#define F_CPU 16000000UL
#define BAUD 9600
#define UBRR_VAL ((F_CPU / (16UL * BAUD)) - 1)
void uart_init(void) {
UBRR0H = (UBRR_VAL >> 8); UBRR0L = UBRR_VAL;
UCSR0B = (1 << TXEN0) | (1 << RXEN0); // enable transmitter and receiver
UCSR0C = (1 << UCSZ01) | (1 << UCSZ00); // 8 data bits, no parity, 1 stop bit
}
void uart_putc(char c) { while (!(UCSR0A & (1 << UDRE0))) { } UDR0 = c; } // wait until the data register is empty
void uart_puts(const char *s) { while (*s) uart_putc(*s++); }
int main(void) { uart_init(); uart_puts("Hello from the chip\r\n"); while (1) { } }| Standard | Voltage levels | Range | Notes |
|---|---|---|---|
| UART TTL (3.3 V / 5 V) | 0 V and VCC | short, same board | what microcontroller pins give |
| RS-232 | +/-3 to 15 V | up to ~15 m | old PC COM ports; needs a MAX232 level shifter |
| RS-485 | differential pair | up to 1200 m | industrial, many devices on one bus |
Quick quiz
1. How many bits does one 8N1 character take on the wire?
Start + 8 data + stop = 10.
2. What must both UART sides agree on?
There is no clock wire, so speed must match.
3. Which data bit is sent first?
UART normally sends the least significant bit first.
Common questions
How many bits does one 8N1 character take on the wire?
10. Start + 8 data + stop = 10.
What must both UART sides agree on?
The baud rate and frame format. There is no clock wire, so speed must match.
Which data bit is sent first?
LSB. UART normally sends the least significant bit first.