I2C, SPI and UART
I2C, SPI and UART are serial buses, ways for chips to send data one bit at a time: UART uses two wires and no clock, I2C two wires shared by many devices, and SPI four. At 115,200 baud, a UART sends a byte in about 86.8 µs.
tbyte = 10 ÷ baud
where tbyte is a UART’s time per byte: a start bit, 8 data bits and a stop bit, so 9,600 baud takes about 1.04 ms.
SPI at 8 MHz moves a byte in 1 µs.
What are I2C, SPI and UART?
I2C, SPI and UART are the three most common ways for chips to send data to each other one bit at a time over a few wires, such as an Arduino reading a sensor or driving a display.
Sending bits one after another takes far fewer wires than sending a byte’s eight bits at once on eight. Each bus frames the digital bits its own way, as Figure 1 shows: UART adds start and stop bits, SPI a clock and a chip select, and I2C a clock, an address and acknowledgments.
SPI: CS falls to pick the chip, then each SCK pulse moves one bit of “A”, 0x41, along MOSI, most significant first, 0 1 0 0 0 0 0 1. MISO, not drawn, brings a byte back at once.
What is UART?
UART, a universal asynchronous receiver-transmitter, sends bytes on one wire in each direction with no clock, so both ends must agree on a speed, the baud rate, such as 9,600 or 115,200 bits a second.
Each device’s TX, or transmit, pin connects to the other’s RX, or receive. The line rests high; a start bit pulls
it low, eight data bits follow, least significant first, and a stop bit returns it high: a frame called 8N1, for 8
data bits, no parity and 1 stop bit. The receiver times each bit from the start bit’s edge, so both ends’ clocks
must match within a few percent. An Arduino’s Serial commands use its
UART.
What is I2C?
I2C, short for inter-integrated circuit, is a two-wire bus on which one controller talks to many devices over a shared data line, SDA, and clock line, SCL, picking each device by its 7-bit address.
Philips created it in 1982; it is also written I²C. The MPU-6050 accelerometer, small OLED displays and clock chips use it, usually at 100 kHz, standard mode, or 400 kHz, fast mode. Seven address bits make 128 addresses, 16 of them reserved, so up to 112 devices can share the two wires.
How does I2C work?
I2C works by having the controller pull SDA low while SCL is high to start, clock out a device’s address and a read or write bit, then send or receive bytes, each answered by an acknowledge bit.
Data changes only while SCL is low, so SDA changing while SCL is high marks a start or, rising, a stop. The receiver of each byte pulls SDA low on a ninth clock to acknowledge it; a missing device leaves SDA high. Devices only pull the lines low or let go, so pull-up resistors, often 4.7 kΩ, pull them high again, and two devices can never short the bus.
What is SPI?
SPI, the serial peripheral interface, is a four-wire bus on which a controller clocks bits out on MOSI and in on MISO at the same time, choosing each device with its own chip-select line, CS.
Motorola introduced it in the 1980s. SCK is the clock; MOSI carries data to the device and MISO back, now often labeled COPI and CIPO. Pulling a chip’s CS low wakes it while the rest ignore the bus. With no addresses or acknowledgments, a byte takes 8 clocks, often at 10 MHz or more, so SD cards, flash memory, fast displays and radio modules use SPI.
What is the difference between I2C and SPI?
I2C uses two shared wires and picks devices by address, while SPI uses at least four wires, with a chip select for each device, but runs many times faster and sends data both ways at once.
Figure 2 counts the wires: I2C stays at two however many chips share it, while SPI needs 3 + n for n chips. Choose I2C for many slow sensors on few pins, and SPI for speed, such as a display or memory card. I2C’s acknowledgments also confirm that a device answered; SPI has none.
I2C, 3 chips: 2 signal wires, SDA and SCL, shared by all 3 chips, each answering only to its own address. Two pull-up resistors, not drawn, hold the lines high.
What is the difference between UART and I2C?
UART links just two devices with no clock and no addresses, at an agreed baud rate, while I2C carries its own clock and lets one controller reach up to 112 devices on the same two wires.
Either end of a UART can talk whenever it likes, which suits a GPS module, a Bluetooth module or a computer’s serial port; I2C devices speak only when asked. UART also reaches farther: over a kilometer with an RS-485 driver at each end, while I2C is meant for one board or a short cable.
| Bus | Wires | Clock | Devices |
|---|---|---|---|
| UART | 2 | No | 2 |
| I2C | 2 | Yes | 112 |
| SPI | 3 + n | Yes | 1 per CS |
How fast are UART, I2C and SPI?
SPI is usually fastest, at several MHz, I2C runs at 100 or 400 kHz, and a UART at 115,200 baud sends a byte about every 86.8 µs, since each byte takes 10 bits.
Example 1: a UART’s speed in bytes
An Arduino sends text at 9,600 baud, 8N1. How long does each byte take, and how many go each second?
- Given
- a UART at 9,600 baud, 8N1: 10 bits a byte
- Find
- the time per byte, and bytes per second
- Solution
- tbit = 1 ÷ 9,600 ≈ 104 µstbyte = 10 × 104 µs ≈ 1.04 msbytes a second = 9,600 ÷ 10 = 960
- Answer
- 1.04 ms a byte, 960 bytes a second
Example 2: a display frame over I2C and SPI
A 128 × 64 monochrome OLED display stores 1 bit a pixel. How long does a full frame take over I2C at 400 kHz, ignoring command bytes?
- Given
- a 128 × 64 OLED at 1 bit a pixel, I2C at 400 kHz
- Find
- the time for one frame, and the most frames a second the bus can carry
- Solution
- bytes = 128 × 64 ÷ 8 = 1,024clocks = 1,024 × 9 = 9,216t = 9,216 ÷ 400,000 ≈ 23 msframes a second = 400,000 ÷ 9,216 ≈ 43.4
- Answer
- 23 ms a frame, at most 43.4 frames a second
Summary
Key equations
- tbit = 1 ÷ baud a UART bit
- tbyte = 10 ÷ baud a UART byte, 8N1
- t = 9 × bytes ÷ f I2C at clock f
- t = 8 × bytes ÷ f SPI at clock f
Key terms
- Baud rate
- A UART’s bits a second.
- SDA, SCL
- I2C’s data and clock.
- Chip select
- Picks an SPI device.
- Acknowledge
- A receiver’s “got it” bit.
Practice problems
-
How many signal wires does I2C use?
SDA for data and SCL for the clock, shared by every device.
-
A UART runs at 9,600 baud, 8N1. How many bytes can it send each second?
Each byte takes 10 bits: 9,600 ÷ 10 = 960.
-
Which bus picks a device with its own chip-select line instead of an address?
SPI has no addresses: the controller pulls one chip’s CS low.
-
How long does SPI at 8 MHz take to send one byte?
8 clocks ÷ 8,000,000 a second = 1 µs.
-
How many addresses can 7 address bits make?
2⁷ = 128, of which I2C reserves 16, leaving 112 for devices.
FAQ
What do I2C, SPI and UART stand for?
Inter-integrated circuit, serial peripheral interface and universal asynchronous receiver-transmitter. Asynchronous means a UART sends no clock.
Is UART the same as RS-232?
No. A UART frames bits at the chip’s logic levels; RS-232 is an electrical standard using about ±3 to ±15 V, inverted, so a converter chip such as the MAX232 sits between.
How many devices can share an I2C bus?
Up to 112 with 7-bit addresses, since 16 of the 128 are reserved; the bus’s capacitance, at most 400 pF, often limits it first.
What is a baud rate?
Symbols sent each second. On a UART each symbol is a bit, so 9,600 baud is 9,600 bits a second, or 960 bytes.
Which Arduino Uno pins are I2C, SPI and UART?
UART is pins 0 (RX) and 1 (TX), I2C is A4 (SDA) and A5 (SCL), and SPI is pins 10 (CS), 11 (MOSI), 12 (MISO) and 13 (SCK).