SPI is one of the most important communication interfaces in embedded electronics. It is commonly used when a microcontroller needs to exchange data quickly with displays, memory chips, ADCs, DACs, radio modules and other high-speed peripherals.
Compared with I2C, SPI usually requires more wires, but it can be much faster and simpler at the electrical level. This makes SPI a very useful interface for Arduino, ESP32, RP2040, STM32 and many other microcontroller platforms.
What Is SPI?
SPI stands for Serial Peripheral Interface. It is a synchronous serial communication interface, which means that data transfer is controlled by a clock signal.
A typical SPI bus uses four main signals:
- SCLK: Serial Clock
- MOSI: Master Out, Slave In
- MISO: Master In, Slave Out
- CS or SS: Chip Select / Slave Select
The controller generates the clock and selects which peripheral device it wants to communicate with.
SPI Signal Names
Traditional SPI signal names use the words master and slave. Newer documentation may use controller and peripheral terminology instead.
Common equivalent names:
- MOSI may also be called COPI: Controller Out, Peripheral In
- MISO may also be called CIPO: Controller In, Peripheral Out
- SS may also be called CS: Chip Select
- SCLK may also be called SCK or CLK
Different chip manufacturers and libraries may use slightly different names, but the function is the same.
How SPI Communication Works
SPI uses a clock line and separate data lines for each direction.
During communication:
- The controller pulls the selected device's CS line active
- The controller generates clock pulses on SCLK
- Data is shifted out on MOSI
- Data is shifted in on MISO
- The CS line is released when the transaction is finished
Because SPI has separate data lines for sending and receiving, it can support full-duplex communication. This means data can move in both directions at the same time.
SPI Is Not Address-Based Like I2C
Unlike I2C, SPI does not normally use device addresses. Instead, each device usually gets its own chip select line.
This has advantages and disadvantages.
- No address conflicts like I2C
- Protocol overhead is low
- Communication can be very fast
- Each additional device usually needs another CS pin
For small systems with only one or two SPI devices, this is simple. For larger systems, chip select wiring can become more complicated.
SPI Bus Wiring
A basic SPI bus with one peripheral device usually uses:
- SCLK from controller to peripheral
- MOSI from controller to peripheral
- MISO from peripheral to controller
- CS from controller to peripheral
- Common GND
Some SPI devices are write-only and do not need MISO. Many displays are examples of this. Other devices, such as memory chips and ADCs, usually require both MOSI and MISO.
Multiple SPI Devices
Multiple SPI devices can share SCLK, MOSI and MISO, while each device has its own chip select line.
Example:
- Display: CS1
- Flash memory: CS2
- Radio module: CS3
- ADC: CS4
Only one chip select line should be active at a time. If two SPI devices try to drive MISO at the same time, communication errors or bus conflicts can occur.
SPI Speed
SPI can be much faster than I2C. Exact speed depends on the microcontroller, peripheral device, wiring and library.
Typical practical SPI speeds:
- 1 MHz to 4 MHz for conservative wiring and slower modules
- 8 MHz to 20 MHz for many displays and memory devices
- Higher speeds possible with short traces and suitable hardware
The datasheet of the peripheral device should always be checked. Not every SPI device can handle the maximum speed that the microcontroller can generate.
SPI Modes: Clock Polarity and Phase
SPI has four modes based on clock polarity and clock phase. These are usually called Mode 0, Mode 1, Mode 2 and Mode 3.
The two settings are:
- CPOL: Clock polarity
- CPHA: Clock phase
Most Arduino libraries configure the correct SPI mode automatically. But when writing low-level code or troubleshooting a device, the SPI mode from the datasheet matters.
SPI Bit Order
Most SPI devices transfer the most significant bit first, often called MSB first. Some devices can use least significant bit first, called LSB first.
In practical projects, MSB first is the normal default. If communication does not work even though wiring and chip select are correct, checking bit order and SPI mode is important.
SPI Compared with I2C and UART
| Interface | Clock | Typical Wires | Multiple Devices | Typical Speed | Best For |
|---|---|---|---|---|---|
| SPI | Yes | 4 or more | Separate CS lines | High | Displays, memory, fast peripherals |
| I2C | Yes | 2 | Address-based | Low to medium | Sensors, RTCs, I/O expanders |
| UART | No shared clock | Usually 2 | Not built in | Low to medium | Serial modules, GPS, debug ports |
Common SPI Devices
SPI is commonly used with:
- TFT displays
- OLED displays
- SD card modules
- Flash memory chips
- High-speed ADCs
- DACs
- Radio transceiver modules
- Ethernet controllers
- Touchscreen controllers
Many devices that need faster data transfer than I2C use SPI.
3.3V and 5V SPI Compatibility
SPI voltage compatibility must be checked carefully. Many modern microcontrollers are 3.3V devices, while some classic Arduino boards use 5V logic.
Examples:
- ESP32 uses 3.3V logic
- RP2040 uses 3.3V logic
- STM32 usually uses 3.3V logic
- Classic Arduino UNO and Nano use 5V logic
A 5V SPI signal can damage a 3.3V-only device. When mixing voltage levels, use proper level shifting or select modules that already include level shifting.
SPI and Long Wires
SPI is usually intended for short-distance communication on a PCB or between nearby modules. It can work over short cables, but it is not designed as a long-distance bus.
Long wires can cause:
- Signal ringing
- Noise pickup
- Clock edge distortion
- Data errors at higher speeds
For longer distances, reducing SPI speed can help. For industrial or long-cable communication, RS485, CAN Bus or other differential interfaces are usually better choices.
Common SPI Problems
Typical SPI problems include:
- MOSI and MISO swapped
- Wrong chip select pin
- Wrong SPI mode
- Wrong voltage level
- SPI speed too high
- No common ground
- Multiple devices driving MISO at the same time
- Library using a different SPI bus than expected
On microcontrollers with flexible pin mapping, it is also important to confirm which pins are actually used by the library or board definition.
Hardware SPI vs Software SPI
Most microcontrollers include one or more hardware SPI peripherals. Hardware SPI is usually faster and more efficient than software SPI.
Software SPI, also called bit-banged SPI, uses normal GPIO pins and generates the SPI signals in software.
- Hardware SPI is faster and preferred for displays and memory
- Software SPI can be useful when pins are limited
- Software SPI is usually slower
- Some libraries support both hardware and software SPI
For high-speed devices, hardware SPI should be used whenever possible.
SPI on Popular Microcontrollers
SPI is available on almost all modern microcontrollers.
- Arduino UNO / Nano: one main SPI interface
- ESP32: multiple SPI controllers, depending on chip and board
- RP2040: two hardware SPI controllers
- STM32: multiple SPI peripherals on many devices
- nRF52: SPI support for sensors, displays and radios
The exact pins depend on the board and the selected software framework.
When to Use SPI
SPI is a good choice when:
- Higher speed is required
- The peripheral already supports SPI
- Only a few devices need to share the bus
- Full-duplex communication is useful
- The wiring is short and controlled
SPI is often the best choice for displays, SD cards, external memory and fast data converters.
When Not to Use SPI
SPI may not be the best choice when:
- You need to connect many low-speed devices with minimal wiring
- You do not have enough chip select pins
- The cable length is long
- The environment is noisy
- The project only needs simple low-speed sensors
For many sensors and small modules, I2C may be simpler. For long-distance communication, RS485 or CAN Bus is usually more robust.
Debugging SPI
SPI can be debugged very effectively with a logic analyzer.
Useful checks include:
- Is the correct CS line going active?
- Is the clock running?
- Is data visible on MOSI?
- Is the device responding on MISO?
- Does the SPI mode match the datasheet?
- Is the selected speed reasonable?
Unlike I2C, SPI does not have a standard acknowledge response. A device may appear silent if the command format, SPI mode or chip select handling is wrong.
SPI in Modular Electronics
SPI is very useful in modular electronics when higher speed is needed. It is commonly used for displays, SD cards, memory and some radio modules.
In CANABLOX and similar modular systems, I2C is often preferred for simple low-speed modules, while SPI is useful when a module needs faster data transfer or large amounts of data.
Practical Design Tips
- Keep SPI wires short
- Use a common ground
- Check the required SPI mode in the datasheet
- Start with a lower SPI speed when testing
- Use hardware SPI for high-speed devices
- Give each SPI device a reliable chip select signal
- Be careful when sharing MISO between multiple devices
- Use level shifting when mixing 3.3V and 5V devices
Conclusion
SPI is a fast and practical communication interface for many embedded devices. It uses more wires than I2C, but it offers higher speed, simple timing and full-duplex communication.
For sensors and small low-speed modules, I2C is often more convenient. For displays, SD cards, memory chips, radio modules and fast peripherals, SPI is often the better choice.
