Serial buses: UART, I2C and SPI
Chips on a board talk over a few wires by sending bits one after another. Watch a microcontroller send one byte over each of the three common serial buses: UART's start and stop bits at 115200 baud, I2C's shared wires, addresses and ACK bit, and SPI's clock, chip select and full-duplex data. A timing screen shows each wire's voltage with real timings.
5 quick questions
Pick an answer to see why. Your score saves with your progress.
01A UART sends the letter A (0x41) with 8 data bits, no parity and 1 stop bit at 115200 baud. How long does the whole frame take?
The frame has 10 bits: a start bit, 8 data bits and a stop bit. Each bit lasts 1/115200 s = 8.68 µs, so 10 bits take 86.8 µs.
02Why does an I2C bus need pull-up resistors?
I2C outputs are open drain: a chip either pulls the wire low or lets go. The pull-up resistor makes the wire 1 when nobody pulls it.
03On I2C, how does the microcontroller know that the chip at address 0x76 heard it?
After the 8 bits of a byte, the receiver pulls SDA low during the ninth clock pulse. That 0 is the acknowledge (ACK) bit.
04What does full duplex mean for SPI?
MOSI and MISO are separate wires, so on each tick the microcontroller sends a bit and receives a bit at the same time.
05You need to connect eight slow temperature sensors using as few microcontroller pins as possible. Which bus fits best?
I2C uses just two wires for all of them, as long as each sensor has its own address (one model often offers only one or two address choices, so many identical chips may need an address mux). SPI would need a CS wire per sensor, and UART links only two devices.
Glossary
- Serial bus
- A set of shared wires plus rules for sending bits one after another, instead of many bits at once on many wires.
- Microcontroller
- A small computer on one chip: processor, memory and input/output pins, used to control devices.
- UART
- Universal asynchronous receiver-transmitter: two data wires (TX and RX), no clock wire; both ends agree on a baud rate.
- Baud rate
- How many symbols a line sends per second. For UART each symbol is one bit, so 115200 baud is 115200 bits per second, even when several bits in a row are the same and the line does not change.
- Start and stop bits
- In a UART frame, a 0 that marks the start of a byte and a 1 that marks its end, so the receiver can find each byte.
- I2C
- Inter-Integrated Circuit: two shared wires, SDA (data) and SCL (clock); each chip has a 7-bit address and answers with an ACK bit.
- Open drain and pull-up
- An output that can only pull a wire to 0 or let go; a pull-up resistor makes the wire 1 when nobody pulls it.
- SPI
- Serial Peripheral Interface: SCLK (clock), MOSI (out), MISO (in) and one chip-select (CS) wire per chip; full duplex and fast.
Notes and sources
- A serial bus sends one bit at a time on each data wire; a parallel bus sends several bits at once on several wires. Fewer wires means fewer pins and simpler boards.
- UART frames here use 8 data bits, no parity bit and 1 stop bit (written 8N1). Data bits go least significant bit first (Analog Devices, UART: A Hardware Communication Protocol).
- I2C timings and rules are from NXP's I2C-bus specification (UM10204): Standard-mode runs up to 100 kbit/s and Fast-mode up to 400 kbit/s. Each byte is followed by an ACK bit, so it takes nine clock pulses.
- SPI has no single official standard; this figure uses the common mode 0 (clock idle low, data read on the rising edge), most significant bit first (Analog Devices, Introduction to SPI Interface).
- The sensor is modelled on the Bosch BME280, which speaks both I2C and SPI: its I2C address is 0x76 or 0x77, its ID register 0xD0 reads 0x60, and its SPI clock may run up to 10 MHz.
- The figure plays in slow motion. Real bits do not travel as little packets: the voltage on the whole wire changes almost at once, and the receiver reads it at the right moment.
- NXP UM10204: I2C-bus specification and user manual
- Texas Instruments SLVA704: Understanding the I2C bus
- Analog Devices: UART, a hardware communication protocol
- Analog Devices: Introduction to SPI interface
- Bosch Sensortec BME280 datasheet (I2C address, chip ID, SPI)
- MicroPython: machine.UART
- MicroPython: machine.I2C
- MicroPython: machine.SPI
Quiz Unlocks after the last step.