Atlas Robotics
Hardware InterfacesBasic

What is SPI? High-speed communication for sensors and displays

Atlasbot · 16/8/2026 · 11 min read

What is SPI? High-speed communication for sensors and displays
SPI — one master, multiple slaves, each slave has its own CS wire

When I2C isn't fast enough — for example, needing to continuously read data from an IMU sensor at high frequency, or push images onto a TFT display — SPI is the next choice: significantly faster than I2C and UART, at the cost of needing more wires.

Key concepts

SPI (Serial Peripheral Interface) uses 4 signal wires:

  • SCLK (Serial Clock) — clock, generated by master
  • MOSI (Master Out, Slave In) — data from master sent to slave
  • MISO (Master In, Slave Out) — data from slave sent back to master
  • CS/SS (Chip Select / Slave Select) — select which device is being talked to

Because there are two separate data lines for both directions (MOSI and MISO), SPI transmits and receives simultaneously — full-duplex — while I2C and UART typically only transmit one way at a time.

No address — select device using separate CS wire

Unlike I2C, which uses addresses to distinguish devices on the same two wires, SPI does not have the concept of address: each slave has a separate CS wire connected directly to the master. To talk to a device, the master pulls the correct CS wire of that device low before transmitting — other devices (CS is high) will ignore all data on the bus.

In short: SPI is many times faster than I2C thanks to full-duplex and does not need a complicated addressing/ACK mechanism — in return, the more devices, the more CS pins are needed, not saving GPIO pins like I2C.

How it works

Master (MCU)
   SCLK ──┬─────────┬─────── (common to all slaves)
   MOSI ──┼─────────┼─────── (common to all slaves)
   MISO ──┼─────────┼─────── (common to all slaves)
   CS1  ──┘         │
   CS2  ────────────┘
         │                  │
     IMU sensor TFT screen
     (select when CS1=0) (select when CS2=0)

Example of reading/writing a byte using HAL on STM32 — SPI always transmits and receives simultaneously in the same command:

HAL_GPIO_WritePin(CS1_GPIO_Port, CS1_Pin, GPIO_PIN_RESET); // Drag CS1 low, select device

uint8_t tx_data = 0x8F;   // Bytes want to send
uint8_t rx_data;          // Place to store bytes received at the same time
HAL_SPI_TransmitReceive(&hspi1, &tx_data, &rx_data, 1, HAL_MAX_DELAY);

HAL_GPIO_WritePin(CS1_GPIO_Port, CS1_Pin, GPIO_PIN_SET);   // Release CS1, ending the transaction

Technical details

Formulas

Since SPI transmits exactly 1 bit per clock pulse, the theoretical throughput is calculated directly from the SCLK frequency:

Throughput (bytes/sec) = SCLK Frequency / 8

VD: SCLK = 10MHz
Throughput = 10,000,000 / 8 = 1,250,000 bytes/sec (1.25 MB/s)

Reading a 14-byte IMU data block (accel + gyro + temp) takes:
14 / 1,250,000 ≈ 11.2µs — fast enough to read at several kHz for stable control loop

Parameters

Maximum SCLK speed varies depending on slave device type — exceeding the limit of the slowest device on the same bus causes data read errors even if the wiring is correct:

EquipmentTypical maximum SCLK speed
SD memory card (SPI mode)~25MHz (standard), some support up to 50MHz
IMU sensor over SPI1 – 10MHz (depending on chip)
TFT screen20 – 40MHz

Register / Bit / Byte

SPI has 4 "modes" (Mode 0-3) defined by 2 configuration bits: CPOL (Clock Polarity — whether the idle clock level is high or low) and CPHA (Clock Phase — data is sampled on the rising or falling edge of the clock). Master and slave must be in the same mode — incorrect configuration (eg: using Mode 0 while the sensor requires Mode 3) causes the data read to be completely wrong even though all the wires are correct, one of the most difficult reasons to diagnose when first pairing SPI with an unfamiliar chip.

Practical applications

SPI often appears when high bandwidth is needed: reading the IMU sensor at a frequency of a few hundred Hz to a few kHz for the control loop to stabilize the robot (according to the read time calculation in the Recipe section), reading/writing an SD memory card to log the journey for offline debugging after the robot has run a test run, or controlling the TFT screen displaying the robot status needs to push tens of thousands of pixels per frame — things where the slower speed of I2C will become a bottleneck.

Practical example

// Read the WHO_AM_I register of an IMU over SPI to confirm correct communication
uint8_t tx[2] = {0x75 | 0x80, 0x00};  // bit 7 = 1 indicates read command, register address 0x75
uint8_t rx[2];

HAL_GPIO_WritePin(CS_GPIO_Port, CS_Pin, GPIO_PIN_RESET);
HAL_SPI_TransmitReceive(&hspi1, tx, rx, 2, HAL_MAX_DELAY);
HAL_GPIO_WritePin(CS_GPIO_Port, CS_Pin, GPIO_PIN_SET);

printf("WHO_AM_I = 0x%02X\n", rx[1]);
Output (correct sensor datasheet, eg MPU9250):
WHO_AM_I = 0x71

Reading the correct WHO_AM_I value according to the datasheet is the first confirmation step when pairing a new SPI sensor — if the reading value is wrong or always 0x00/0xFF, it is almost certainly the wrong SPI mode (CPOL/CPHA) or wrong CS/MISO/MOSI wire, not a logic error reading the actual sensor data.

Common mistakes

  • Wrong SPI mode (CPOL/CPHA) — as mentioned in the Register/Bit/Byte section, this is the most common error when pairing a new SPI sensor, always check the correct mode according to the datasheet before suspecting the code.
  • Forgot to pull CS low before transmitting, or forgot to release it after finishing — the device only "listens" to the bus when CS is low; Forgetting a CS operation causes the device to ignore all data or mistakenly receive data intended for another device.
  • Exchange MISO and MOSI — These two wires are symmetrical in name (Master Out/Slave In vs Master In/Slave Out) so it's easy to misconnect — always connect MOSI-MOSI, MISO-MISO (not diagonal like UART).
  • Run SCLK beyond the maximum speed of the slowest device on the bus — if multiple devices share SCLK but have different maximum speeds, you must configure it according to the speed of the slowest device, or use the SPI peripheral to support flexible speed changes for each CS.

Conclusion

SPI trades more pins (each device has its own CS wire) for superior full-duplex speed compared to I2C — the right choice for high-frequency sensors, memory cards, and displays. Mastering the concept of CPOL/CPHA and always checking WHO_AM_I first is the fastest way to debug when pairing a new SPI device. The next article in the column is about RS232 — an older serial voltage standard than UART TTL, still popular in industrial equipment.

Get new articles and lessons by email

When a new technical article or course module is published, Atlas Robotics sends you a short email. No spam.

Unsubscribe any time with the link in each email. See our Privacy policy.

What is SPI? High-speed communication for sensors and displays — Atlas Robotics