Sub topic 1
Serial Communication over UART: Why Serial?
Prerequisites
This chapter assumes you know how to write a basic Arduino sketch with setup() and loop(), that a byte is 8 bits, and that a digital pin reads as HIGH or LOW. You should also know what a serial port is in general terms.
Why serial? Sending bits one at a time
Serial communication sends data one bit at a time over a single wire. Parallel communication sends several bits at once over several wires. Parallel links are faster over short distances, but they need many wires and suffer from timing skew. Serial links need fewer wires and work over longer distances.
UART (Universal Asynchronous Receiver/Transmitter) is a common serial scheme. It uses two data lines: TX (transmit) and RX (receive). It has no shared clock line, so both devices must agree on the speed in advance.
Anatomy of a UART frame
A UART frame is the set of bits that carries one byte. When the line is idle, it sits HIGH. A frame has these parts:
- Start bit: one LOW bit that tells the receiver a frame is beginning.
- Data bits: usually 8 bits, sent least significant bit first.
- Parity bit (optional): a check bit for error detection.
- Stop bit: one or two HIGH bits that return the line to idle.
With the common 8N1 format (8 data bits, No parity, 1 stop bit), one byte takes 10 bit times on the wire.
flowchart LR
A["Idle HIGH"] --> B["Start LOW"] --> C["D0 to D7"] --> D["Stop HIGH"]caption: One 8N1 frame, from idle to stop bit. alt: A left-to-right chain showing idle high, a start bit low, eight data bits, and a stop bit high.
Baud rate and bit timing
Baud rate is the number of bit slots per second. At 9600 baud, each bit lasts 1 / 9600 s, about 104 µs.
Bit time and throughput follow directly from the baud rate:
- Time per bit = 1 / baud rate
- Time per byte (8N1) = 10 / baud rate
- Bytes per second ≈ baud rate / 10
At 9600 baud, one byte takes about 1.04 ms and the link carries about 960 bytes per second. At 115200 baud, the link carries about 11,520 bytes per second.
void setup() {
Serial.begin(9600);
}
void loop() {
if (Serial.available() > 0) {
Serial.write(Serial.read());
}
}Both ends must use the same baud rate and frame format. If they differ, the receiver samples bits at the wrong times and reads garbled data.
Exercise
How long does it take to send 50 bytes at 9600 baud with 8N1 framing?
Show answer
Each byte is 10 bits, so 50 bytes is 500 bits. 500 / 9600 ≈ 0.052 s, or about 52 ms.