Skip to content
You Start with one site
Amounts in Start with one site
Sensors & Actuators · Concept

I2C and SPI

One idea Intermediate 5 min read
Found this useful?

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.

I2C2 wires for all devices. Each device has an address.Few pins, slower
SPI3 shared wires, and 1 select wire for each device.More pins, much faster
BothOne chip, the master, gives the clock and starts every transfer.Short distances only

See it

Predict first

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?

See it: one transfer on each bus, step by step

Wires for all devices
How a device is chosen
Clock in this example

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.

I2C wires = 2
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

The question

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
  1. Count the wires

    I2C: 2 wires. SPI: 3 + 3 = 6 wires.

  2. 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.

  3. 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

An I2C device never drives a wire high. It can only pull a wire low or let it go. Two resistors, one on each wire, pull the wires back up to the supply. Leave these pull-up resistors out and the bus does not work at all, although every device is good.

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

Question 1

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.

Question 2

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?

Question 3

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.
Key points
  • 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.
Found this useful?

Discussion

    Comments show your name. Be kind, stay on the topic.