TX → RX pin 0ring buffer buf[8] in SRAM16 MHzPC · USBATmega328PUBRR0UDR0loop()[0][1][2][3][4][5][6][7]lost01UBRR0 = 207; // 9615 baud, U2X0 on2ISR(USART_RX_vect) {3 uint8_t c = UDR0;4 uint8_t n = (head + 1) % 8;5 if (n == tail) lost++; // full6 else { buf[head] = c; head = n; } }7if (head != tail) { // in loop()8 c = buf[tail]; tail = (tail + 1) % 8; }headtail0000101101
Back: the PC, the clock and the ATmega328P with its UART registers; loop() on the rightMiddle: the 8-slot ring buffer with head and tail; front: the driver codeTokens: clock ticks, the bits of one byte, and bytes moving in and out of the ringAn Arduino Uno receives text from the PC on RX pin 0 at 9,615 baud (UBRR0 = 207, 16 MHz clock). Each byte lands in UDR0; the receive interrupt moves it into an 8-slot ring buffer at head, and loop() takes bytes out at tail. Both indexes wrap from 7 to 0. If loop() stops reading, the ring fills at 7 bytes and the interrupt drops the rest, counting them in lost.

The wire and the baud rate

  1. UART sends bytes over one wire, the sender's TX to the receiver's RX, plus ground. There is no clock wire, so both ends must agree on a speed: the baud rate. On an Uno the PC reaches RX pin 0 through a USB chip.

Interrupt and ring buffer

When the ring is full

What happens

;; step 1/10 · link
UART   universal asynchronous receiver-transmitter
wires  TX -> RX, plus a shared ground
clock  none on the wire: both ends agree a baud rate
Uno    PC -> USB chip (ATmega16U2) -> RX pin 0 of the ATmega328P
the Hardware lesson "Serial buses: UART, I2C and SPI" covers the wires

The card shows the core lines only. The full sketch also turns on double speed (U2X0), the receiver and the RX interrupt in UCSR0B, declares buf, head, tail and lost as volatile, and wraps the read in uart_read(), which returns -1 when the ring is empty. Do not use Serial in the same sketch: the Arduino core's Serial already defines this ISR. One slot always stays empty, so 8 slots hold at most 7 bytes: that way head == tail can only mean empty. The drawing blanks a slot once it is read; in real memory the old byte stays until it is overwritten. A power-of-two size lets the compiler do % 8 as a cheap bit mask.