I2C and SPI
On this page
A teacher can speak to one pupil in two ways: call a name that the whole room hears, or walk over and touch one pupil on the shoulder. Chips have the same two ways. I2C and SPI are the two common buses, sets of shared wires, on which one chip talks to many sensors: I2C calls a device by its address, and SPI selects it with a wire of its own.
See it
Three sensors of the same model are connected to one I2C bus. All three have the same fixed address. The master asks that address for a reading. What happens?
Time runs from left to right, and each line is one wire, drawn high or low. The dark part has already happened, and the shaded band is the present step. On I2C the master first sends the address 111 0110 on the data wire, and the device with that address answers. On SPI the master pulls the select wire of one device low, then sends and receives one bit on each pulse of the clock.
The idea
A bus is a set of wires that several devices share. One chip, the master, starts every transfer and gives the clock: a wire whose pulses tell every device when to read a bit.
I2C has two wires for all devices: SDA for data and SCL for the clock. Each device has a 7-bit address. The master sends the address first, and only the device with that address answers.
SPI has three shared wires: SCK for the clock, MOSI for data from the master and MISO for data to the master. It adds one chip select wire, CS, for each device. The master pulls one CS low to choose that device.
SPI wires = 3 + number of devices
ground and power are not counted
I2C saves pins, but its clock is 100 kHz or 400 kHz. SPI needs more pins and runs at several megahertz, which suits displays and memory cards. Both are made for short distances inside one box.
One worked number
A controller must talk to 3 devices, and one of them is a display that needs 1,024 bytes for a full picture. How many wires does each bus need, and how long does the picture take on I2C at 400 kHz and on SPI at 18 MHz?
Show the worked solution, 3 steps
- Count the wires
I2C: 2 wires. SPI: 3 + 3 = 6 wires.
- Find the time on I2C
Each byte needs 9 clock pulses: 8 bits and 1 acknowledge. 1,024 × 9 ÷ 400,000 s = 0.0230 s = 23.0 ms.
- Find the time on SPI
Each byte needs 8 clock pulses. 1,024 × 8 ÷ 18,000,000 s = 0.000455 s = 0.455 ms, about 50 times faster. SPI pays for that speed with 4 more wires.
The mistake to avoid
A second trap: two devices with the same address cannot share an I2C bus. On SPI, the same trap is two select wires low at once.
Check yourself
A controller must read 5 sensors. How many wires does it need on I2C, and how many on SPI? Do not count power and ground.
A new board has two good I2C sensors, wired correctly. To save cost, the two resistors from the bus wires to the supply were left out. No sensor answers. Why?
A logger needs a colour display that takes many thousands of bytes for each picture. The chip has pins to spare. Which bus do you choose for the display?
Where you meet it
- Sensors inside a loggerThe temperature, humidity and pressure sensors in a weather station or a cold room logger usually share one I2C bus.
- Displays and memory cardsThe screen on a meter and the card that stores its readings take a large amount of data, so they use SPI.
- Clocks and settingsThe chip that keeps the time of day, and the one that keeps settings while the power is off, are I2C devices.
- Both buses have one master that gives the clock and starts every transfer.
- I2C: 2 wires for all devices, chosen by address. It needs two pull-up resistors.
- SPI: 3 shared wires and 1 select wire for each device. It is much faster.
- Two devices with the same address cannot share an I2C bus.
Discussion
No comments yet. Questions, results from your own build, a better way to explain it: all welcome.
My notes on this page
Kept on this device only. Nothing is sent anywhere.
Tip: select words in the lesson, or tap a figure or an equation, to put a note right there.
Notes on earlier wording
The lesson has changed and these words are no longer in it. Your notes are kept, with the words you chose.