What Is SPI?

SPI stands for Serial Peripheral Interface. It is a synchronous serial communication interface commonly used by microcontrollers to communicate with sensors, displays, memory chips, ADCs, DACs, and other peripheral devices.

SPI is popular in embedded systems because it can provide fast communication with relatively simple hardware. Unlike UART, SPI uses a clock signal shared between the communicating devices.

A typical SPI connection uses separate lines for the clock, data sent from the controller, data received by the controller, and device selection.

What Does SPI Do?

SPI allows a controller device to exchange digital data with one or more peripheral devices.

For example, a microcontroller might use SPI to read data from a temperature sensor, write data to an external flash memory chip, or send pixels to a display.

SPI transfers data serially, meaning individual bits are transmitted one after another over the data lines.

What Does SPI Stand For?

SPI stands for Serial Peripheral Interface.

It was originally developed for communication between processors and peripheral devices and has become a widely used interface in embedded electronics.

Why Is SPI Synchronous?

SPI is called synchronous because the devices use a shared clock signal to control when data bits are transmitted and sampled.

The controller generates the clock, and the peripheral uses that clock to determine the timing of data transfers.

This makes SPI different from asynchronous interfaces such as basic UART communication.

The Main SPI Signals

A traditional SPI connection commonly uses four signals: SCLK, MOSI, MISO, and CS.

SCLK

SCLK stands for Serial Clock. The controller generates clock pulses on this line to synchronize the transfer.

MOSI

MOSI stands for Master Out, Slave In in traditional terminology. It carries data from the controller toward the peripheral.

Many modern specifications use terms such as Controller Out, Peripheral In instead of Master Out, Slave In.

MISO

MISO stands for Master In, Slave Out. It carries data from the peripheral back toward the controller.

CS

CS stands for Chip Select. It is commonly used by the controller to select which peripheral should participate in a transfer.

CS may also be called SS, meaning Slave Select, depending on the hardware or documentation.

How SPI Devices Are Connected

A basic SPI connection between one controller and one peripheral can use four signal lines.

The controller provides SCLK and CS, sends data through MOSI, and receives data through MISO.

The peripheral receives the clock and chip-select signals and uses MOSI and MISO for data exchange.

SPI Is Usually Full-Duplex

SPI can transmit and receive data at the same time because it normally has separate MOSI and MISO data lines.

While the controller sends one bit to the peripheral on MOSI, the peripheral can send one bit back on MISO during the same clock cycle.

This makes SPI useful when fast bidirectional data transfers are required.

What Is the SPI Clock?

The SPI clock is generated by the controller and consists of a sequence of electrical transitions between logic levels.

Each clock cycle provides timing for transferring one or more bits, depending on the SPI implementation.

The peripheral samples and changes data according to the selected SPI clock mode.

What Is Chip Select?

Chip Select is a control signal used to tell a peripheral that it should participate in an SPI transaction.

A controller commonly drives the selected device's CS line to its active state before starting communication and returns it to its inactive state after the transaction.

The exact active level depends on the device, but active-low chip select is very common.

SPI With Multiple Devices

A single controller can communicate with multiple SPI peripherals.

The SCLK, MOSI, and sometimes MISO lines can be shared among multiple peripherals, while each peripheral has its own chip-select signal.

The controller activates only the CS line belonging to the peripheral it wants to communicate with.

Multiple SPI Devices Example

Imagine a microcontroller connected to a display, flash memory chip, and sensor.

All three devices can share the SPI clock and data connections, while the microcontroller uses separate CS signals to select each device.

When communicating with the display, the display CS is activated while the other devices remain unselected.

What Are SPI Modes?

SPI defines four commonly used clock modes. The mode determines the clock polarity and the clock edge on which data is sampled.

CPOL

CPOL stands for Clock Polarity. It determines the clock's idle state when no data is being transferred.

CPHA

CPHA stands for Clock Phase. It determines which clock edge is used for data sampling and shifting.

SPI Mode 0

SPI Mode 0 uses CPOL = 0 and CPHA = 0. The clock normally idles low, and data is sampled on the first active clock edge.

SPI Mode 1

SPI Mode 1 uses CPOL = 0 and CPHA = 1. The clock idles low, but the sampling relationship is shifted to the opposite clock edge.

SPI Mode 2

SPI Mode 2 uses CPOL = 1 and CPHA = 0. The clock idles high and uses the corresponding first active edge for sampling.

SPI Mode 3

SPI Mode 3 uses CPOL = 1 and CPHA = 1. The clock idles high and uses the opposite edge relationship from Mode 2.

Why Are SPI Modes Important?

The controller and peripheral must use compatible SPI clock settings.

If the wrong mode is selected, the receiver may sample data at the wrong time, producing corrupted or unexpected values.

When configuring SPI, the peripheral's datasheet normally specifies the required clock polarity and phase.

How Does an SPI Transfer Work?

A typical SPI transaction begins when the controller selects a peripheral using its CS line.

The controller then generates clock pulses while data is shifted through the MOSI and MISO lines.

After the required number of bits have been transferred, the controller stops the clock and releases the CS line.

Step 1: Select the Peripheral

The controller activates the CS line associated with the target peripheral.

The selected peripheral recognizes that an SPI transaction is beginning.

Step 2: Generate the Clock

The controller starts generating SCLK pulses according to the configured SPI frequency and clock mode.

The clock provides the timing reference for the data transfer.

Step 3: Send Data on MOSI

The controller places data bits on MOSI according to the selected SPI timing configuration.

The peripheral samples the data on the appropriate clock edge.

Step 4: Receive Data on MISO

At the same time, the peripheral can place response data on MISO.

The controller samples the MISO signal on the appropriate clock edge and reconstructs the received bits.

Step 5: End the Transaction

After the required data has been transferred, the controller stops the clock and deactivates the CS line.

The peripheral can then return to its inactive state.

Does SPI Send Bytes or Bits?

SPI physically transfers bits one at a time, but software commonly works with bytes or larger words.

For example, software may ask the SPI peripheral to transmit a byte such as 0xA5. The SPI hardware then shifts the individual bits across the data line.

What Is an SPI Shift Register?

SPI hardware commonly uses shift registers to move transmitted and received bits.

As clock pulses arrive, bits are shifted out through one data line while incoming bits are shifted into the receive side.

After the configured number of clock cycles, the received bits can be presented to the processor as a byte or word.

SPI Data Order

SPI devices can define whether the most significant bit or least significant bit is transmitted first.

Most significant bit first is common, but the correct setting depends on the peripheral's specification.

What Is SPI Clock Speed?

SPI clock speed determines how quickly clock pulses are generated and therefore how quickly bits can be transferred.

The maximum supported speed depends on the controller, peripheral, PCB layout, wiring, signal quality, and the requirements specified by the peripheral manufacturer.

SPI can often operate at significantly higher data rates than simple UART communication because it uses a dedicated clock and direct synchronous signaling.

SPI Does Not Define a Universal Packet Format

SPI defines the basic electrical and timing relationship for transferring bits, but it does not define one universal command or packet format for all devices.

Each SPI peripheral can define its own commands, register addresses, data formats, status values, and transaction lengths.

The controller must therefore follow the device's datasheet when constructing SPI transactions.

SPI Memory Example

External flash memory is a common SPI peripheral.

A microcontroller can send a command over MOSI, provide an address, and then receive stored data from the memory chip over MISO.

The exact sequence depends on the memory device's command set.

SPI Display Example

Many small displays use SPI to receive commands and pixel data from a microcontroller.

The microcontroller can select the display with CS and then send a sequence of bytes containing display commands or graphical data.

SPI Sensor Example

A sensor may expose internal registers through SPI.

The microcontroller can send a register address and command through MOSI, while the sensor returns the requested measurement or register value through MISO.

SPI and DMA

Microcontrollers often support Direct Memory Access, or DMA, for SPI transfers.

DMA can move data between memory and the SPI peripheral without requiring the CPU to handle every byte individually.

This can be particularly useful for large transfers such as display updates, audio data, or communication with external memory.

SPI Interrupts

SPI peripherals can also generate interrupts when events such as transmission completion, receive availability, or error conditions occur.

Interrupt-driven communication allows software to respond to SPI events without continuously polling the peripheral.

What Is Daisy-Chained SPI?

Some SPI devices support a daisy-chain configuration in which the output of one device connects to the input of another.

A single chip-select signal can then control a chain of compatible devices, with data shifted through the devices during a longer transfer.

Daisy chaining is not supported by every SPI peripheral, so the device documentation must be checked.

SPI vs UART

SPI and UART are both serial communication interfaces, but they have important differences.

SPI

SPI uses a clock signal, commonly supports full-duplex communication, and can provide high-speed communication with peripherals.

UART

UART is normally asynchronous and does not use a shared clock. It is often convenient for point-to-point communication and serial debugging.

SPI vs I2C

SPI and I2C are both widely used for communication between microcontrollers and peripherals.

SPI

SPI generally requires more signal lines, especially when multiple chip-select signals are needed, but it can provide high-speed transfers and full-duplex communication.

I2C

I2C typically uses only two shared bus lines and provides device addressing, making it convenient for connecting multiple peripherals without a separate chip-select line for every device.

Advantages of SPI

SPI has several advantages that make it popular in embedded systems.

High Speed

SPI can support high clock rates on suitable hardware and short connections.

Simple Hardware

The basic SPI interface is straightforward, using clock and data signals plus device selection.

Full Duplex

Separate MOSI and MISO lines allow data to move in both directions during the same clock cycles.

Flexible

SPI is used with many types of peripherals, including sensors, displays, memory, ADCs, DACs, and communication modules.

Limitations of SPI

SPI also has limitations.

It generally requires more signal lines than I2C, especially when multiple peripherals need individual chip-select signals.

SPI does not provide a universal addressing mechanism or standardized packet format, so the controller must understand each peripheral's protocol.

Longer connections and high clock speeds can also create signal-integrity problems.

Does SPI Need a Common Ground?

SPI devices normally need a compatible electrical reference, so a common ground is generally used when connecting devices with compatible single-ended logic signaling.

The exact electrical requirements depend on the hardware and interface design.

Can SPI Work With Different Voltage Levels?

SPI logic levels must be electrically compatible between the connected devices.

If one device operates at 5 V and another operates at 3.3 V, appropriate level-shifting hardware may be required depending on the direction and electrical characteristics of the signals.

What Happens If the SPI Mode Is Wrong?

If the controller uses the wrong CPOL or CPHA settings, the peripheral may interpret the data incorrectly.

Typical symptoms include incorrect register values, corrupted data, unexpected device responses, or communication that appears completely nonfunctional.

What Happens If SPI Wiring Is Wrong?

Incorrect wiring can prevent SPI communication or cause incorrect data.

Common problems include connecting MOSI and MISO incorrectly, selecting the wrong CS pin, missing the clock connection, using incompatible voltage levels, or failing to provide a suitable electrical reference.

How Do You Debug SPI?

SPI communication can be debugged by checking the wiring, clock configuration, chip-select behavior, data order, and peripheral-specific command sequence.

A logic analyzer or oscilloscope can be especially useful because it can show the SCLK, MOSI, MISO, and CS signals directly.

The captured waveform can then be compared with the timing diagrams in the peripheral's datasheet.

SPI Logic Analyzer Example

Suppose a microcontroller reads a register from a sensor but receives the wrong value.

A logic analyzer can show whether CS becomes active, whether the expected number of clock pulses are generated, whether the command appears correctly on MOSI, and whether the sensor responds on MISO.

This can quickly reveal whether the problem is caused by software configuration, wiring, timing, or the peripheral itself.

SPI in Microcontrollers

Many microcontrollers contain one or more SPI peripherals.

Software typically configures the SPI clock frequency, clock mode, bit order, data size, and communication direction before starting transfers.

The microcontroller's GPIO pins may also need to be configured for the appropriate alternate-function SPI signals.

SPI Controller and Peripheral Roles

Traditional SPI terminology commonly describes the device generating the clock as the master and the selected device as the slave.

Modern documentation increasingly uses controller and peripheral terminology instead.

The controller normally initiates transactions and controls the clock and chip-select signals.

Can Two SPI Controllers Communicate?

SPI is traditionally designed around a controller initiating communication with peripherals.

More advanced systems can implement other arrangements, but they require additional coordination and are not part of one universal SPI standard.

Is SPI a Protocol?

SPI is often described as a protocol, but it is more accurately a communication interface with defined signaling and timing conventions.

There is no single universal SPI command language shared by all devices. Individual peripherals define their own higher-level commands and data formats.

SPI Applications

SPI appears in many embedded and electronic systems.

Common applications include external flash memory, SD cards, displays, touch controllers, sensors, ADCs, DACs, radio modules, digital potentiometers, and other peripheral devices.

The Complete SPI Journey

A simplified SPI transaction can be represented as: application requests data → controller selects peripheral → CS becomes active → controller generates SCLK → data is shifted through MOSI and MISO → peripheral processes the command → response data is returned → required clock cycles finish → CS becomes inactive → application receives the result.

Why Understanding SPI Matters

Understanding SPI is important for embedded developers because many sensors, displays, memories, and other peripherals use it.

Knowing how SCLK, MOSI, MISO, CS, clock modes, timing, and data formats work makes it much easier to connect devices and diagnose communication problems.

In simple terms, SPI is a synchronous serial interface where a controller generates a clock and exchanges data with a selected peripheral using dedicated data lines.

The simplest way to understand SPI is this: SPI is a fast synchronous communication interface that lets a controller exchange data with peripheral devices using a shared clock and serial data lines.

The controller selects a peripheral using chip select, generates the clock, sends data through MOSI, and receives data through MISO. The exact commands and data format are defined by each peripheral.

Note: Tip: After learning SPI, explore I2C, UART, GPIO, PWM, SPI clock modes, CPOL and CPHA, DMA, logic analyzers, RS-232, RS-485, flash memory, SD cards, and embedded communication protocols.