Sub topic 1
Why serial? Sending bits one at a time
Prerequisites
This chapter assumes you already know the following:
- The basic Arduino sketch structure, with
setup()andloop(). - Binary numbers and bytes. A byte is 8 bits and holds values from 0 to 255.
- Digital voltage levels. A pin is either HIGH (near the supply voltage) or LOW (near 0 V).
- Basic C or C++ syntax.
You do not need to know anything about UART yet. This chapter explains why serial links exist and how they differ from parallel ones. The later chapters in this tutorial build on it.
The problem: moving a byte between two devices
Suppose a microcontroller must send the value 0x4B (binary 01001011) to another device. The simplest approach is to give each bit its own wire. Eight wires carry eight bits at the same moment, and the receiver reads all eight at once. This is parallel communication.
The other approach uses a single signal wire. The sender puts the bits on that wire one after another, and the receiver collects them in order. This is serial communication.
Both methods move the same data. They differ in how many wires they use and in how they keep the sender and receiver in step.
| Property | Parallel | Serial |
|---|---|---|
| Data wires for one byte | 8 (plus control lines) | 1 per direction |
| Bits on the wire at one instant | 8 | 1 |
| Cable and connector cost | High | Low |
| Pins used on the microcontroller | Many | Few |
| Timing problem between wires | Yes (skew) | No |
| Typical use today | Short on-board buses, memory | Almost everything else |
Why serial won for most links
Parallel looks faster, since 8 bits move per tick instead of 1. For short distances and slow clocks that is true. Three practical problems limit it.
- Pin and wire count. An 8-bit parallel link needs at least 8 data wires, usually plus clock, strobe or handshake lines. An ATmega328P, the chip on an Arduino Uno, has only 23 usable I/O pins. A parallel link to one peripheral would take a large share of them. A serial link takes two.
- Skew. Wires in a cable are never exactly equal in length or capacitance. Bits sent at the same instant arrive at slightly different times. At low speed this is harmless. As the clock rises, the receiver can no longer tell which bits belong together. Fixing this means slowing the clock, so the speed advantage shrinks.
- Crosstalk and cost. Eight signals switching side by side in one cable interfere with each other. Shielding and extra conductors raise the cost, and the problem grows with cable length.
A serial link has one signal per direction, so none of these problems arise between data lines. The designer can then raise the bit rate on that single line. Modern fast links such as USB, SATA and PCI Express are all serial for this reason.
What "one bit at a time" requires
Sending bits on one wire raises two questions that parallel links answer with extra wires.
- When is each bit valid? The receiver must know the instant at which to sample the wire.
- Where does a byte start? A stream of 1s and 0s has no visible boundaries. The receiver must know which bit is the first of a byte.
Serial links answer these questions in one of two ways. Which one they use is the main way to classify them.
Synchronous serial
A synchronous link adds a separate clock wire. The sender toggles the clock, and the receiver samples the data wire on a clock edge. There is no ambiguity about timing, because the sender tells the receiver exactly when to look.
SPI and I2C are synchronous. SPI uses a clock line (SCK) plus data lines. I2C uses a clock line (SCL) plus one shared data line (SDA). The cost is at least one extra wire, and the sender and receiver must be close enough that the clock arrives cleanly.
Asynchronous serial
An asynchronous link has no shared clock. The two ends agree in advance on a speed, and each keeps its own timer. The sender adds marker bits around every byte so the receiver can detect where a byte begins and resynchronise its timer. This is the approach UART uses.
The name UART stands for Universal Asynchronous Receiver/Transmitter. It is the hardware block inside the microcontroller that turns a byte into a timed sequence of bits and back again.
| Feature | Synchronous (SPI, I2C) | Asynchronous (UART) |
|---|---|---|
| Clock wire | Yes | No |
| Agreement needed in advance | Which device is master | Bit rate and frame format |
| Wires for a two-device link | 3 or more (SPI), 2 (I2C) | 2 data wires plus ground |
| Typical topology | One master, several devices | Point to point |
| Extra bits per byte | None | Start and stop bits |
| Failure if speeds differ | Not applicable, clock is shared | Garbled data |
The last row matters in practice. Because UART has no clock, a mismatch in speed between the two ends does not produce a clear error. It produces nonsense characters. A later chapter on troubleshooting covers this in detail.
Simplex, half duplex and full duplex
A link is also described by the directions in which data can flow.
- Simplex. Data flows one way only. A temperature sensor that only transmits is an example.
- Half duplex. Data can flow both ways, but only one direction at a time. The two ends take turns on shared wires. RS-485 (covered later in this tutorial) is commonly used this way.
- Full duplex. Data flows in both directions at once, because each direction has its own wire.
A basic UART link is full duplex. One wire, called TX, carries data out of a device. A second wire, called RX, carries data into it. Each device's TX connects to the other device's RX. Because the two directions never share a wire, both devices can talk at the same time.
The ground connection is easy to forget. A data wire carries a voltage, and a voltage only has meaning relative to a reference. Without a shared ground, the receiver has no common reference for judging HIGH and LOW. A later chapter on wiring covers this in detail.
A first look at serial on an Arduino
Before looking at UART frames in detail, it helps to see serial communication working. This sketch sends one line of text every second over the Arduino's USB serial connection. Open the Serial Monitor in the Arduino IDE at 9600 baud to see the output.
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println("Hello over serial");
delay(1000);
}Serial.begin(9600) configures the UART hardware to send 9,600 bits per second. Each call to Serial.println hands bytes to the UART, which shifts them out on the TX pin one bit at a time. On an Uno, a USB-to-serial converter chip turns those bits into USB traffic for your computer.
The Serial Monitor must use the same speed. If you set it to a different value, the text appears as garbage, which is the speed-mismatch failure described above. Try this on your own board by changing the monitor's speed to 115200 and watching the output change.
Estimating how long serial takes
Serial communication is slower than people expect when they first measure it, so it is worth a quick estimate now. A later chapter does the full calculation.
At 9,600 bits per second, one bit lasts about 104 microseconds (1 / 9600 s). A common frame carries 8 data bits plus 1 start bit and 1 stop bit, which is 10 bits per byte. One byte therefore takes about 1.04 ms, and the link carries roughly 960 bytes per second.
The message Hello over serial plus the line ending is 19 bytes (17 characters, a carriage return and a newline). That takes about 19.8 ms to send at 9,600 baud. This has a consequence for your code. A long print statement is not instant, and a program that prints often can spend a large part of its time waiting for the UART.
Choosing between serial and the alternatives
Use these criteria when picking a link for a project.
- How many pins can you spare? UART needs two, plus a shared ground. SPI needs at least four. I2C needs two, shared by many devices.
- How many devices must share the link? UART is point to point. I2C and SPI support several devices on one set of lines.
- How far apart are the devices? Plain UART at logic voltage suits distances of tens of centimetres to a few metres. Longer runs need a different electrical standard such as RS-232 or RS-485, which later chapters cover.
- Do you need a human-readable stream? UART is easy to watch with a terminal program, which makes it the usual choice for debug output.
- How fast must data move? SPI generally runs much faster than typical UART settings.
UART is not the best choice for every job, but it is nearly always available. Every Arduino board has at least one hardware UART, and many modules, such as GPS receivers, Bluetooth modules and some sensors, speak it.
Common misconceptions
- "Serial means RS-232." Serial describes the idea of sending bits in sequence. RS-232 is one specific electrical standard for it. Arduino pins use a different, lower voltage level.
- "Asynchronous means the data arrives at random times." It means there is no shared clock wire. Within one byte, the bits are sent at precisely timed intervals.
- "The Serial Monitor is the serial port." The Serial Monitor is a program on your computer. The UART is hardware on the board. The two meet through the USB connection.
- "More baud always means better." Higher speed leaves less timing margin, so it makes errors more likely on poor wiring or with imprecise clocks.
What comes next
The next chapter looks inside one byte on the wire: the start bit, the data bits, the optional parity bit and the stop bit. That frame structure is what lets a receiver with no clock find the byte boundaries.
Exercise
A project needs to link an Arduino to a GPS module using as few pins as possible, over a cable about 30 cm long. Which of these would you choose, and why: an 8-bit parallel link, SPI, or UART? State how many signal wires (not counting ground) the Arduino needs for your choice.
Show answer
UART is the best fit. GPS modules commonly speak UART, the distance is short, and the link only needs to be point to point. The Arduino needs 2 signal wires: TX to the module's RX, and RX from the module's TX (plus a shared ground). A parallel link would need at least 8 data wires and is not supported by typical GPS modules. SPI would need more wires (at least 3 or 4) and gives no benefit here.
Exercise
A UART link runs at 9,600 bits per second with a frame of 1 start bit, 8 data bits and 1 stop bit. About how long does it take to send 100 bytes, and what is the maximum throughput in bytes per second?
Show answer
Each byte takes 10 bits on the wire. 100 bytes is 1,000 bits. At 9,600 bits per second, that takes 1,000 / 9,600, which is about 0.104 seconds (104 ms). The maximum throughput is 9,600 / 10 = 960 bytes per second.
Exercise
Two Arduinos are wired TX to TX and RX to RX, with grounds connected. Nothing is received on either side. What is wrong, and what is the fix?
Show answer
The data lines must be crossed. A device's TX output must go to the other device's RX input. With TX connected to TX, both devices are driving the same line and neither is listening. Connect A's TX to B's RX, and B's TX to A's RX.