Sub topic 1
What SPI Is and Where It Is Used
Prerequisites
This chapter assumes you can do the following:
- Write a basic Arduino sketch with
setup()andloop(). - Use
pinMode()anddigitalWrite()to control a pin. - Read binary and hexadecimal numbers and use bitwise operators such as
&,|and<<. - Read a simple pin diagram and wire a breadboard circuit.
You do not need any earlier knowledge of serial buses. This chapter gives the overview. Later chapters cover signals, timing modes and the code in detail.
What SPI Is
The Serial Peripheral Interface (SPI) is a synchronous serial bus. It moves data between one controller and one or more peripherals. "Synchronous" means the controller sends a clock signal alongside the data, and both sides use that clock to decide when each bit is valid. Older documents call the controller "master" and the peripheral "slave". You will still see those terms in datasheets, and the pin names MOSI and MISO come from them.
Motorola defined SPI in the 1980s. There is no formal standards body and no single specification document. Each chip vendor describes SPI behavior in its own datasheet. This leads to a lot of variation in details such as clock polarity, bit order and command format. Most of the work of using SPI is checking that your settings match what the datasheet says.
SPI has four properties that explain why it is popular:
- Fast. Clock rates of several megahertz are common. Some parts accept tens of megahertz.
- Full duplex. The controller and the peripheral send data at the same time, one bit each per clock pulse.
- Simple hardware. There is no addressing protocol, no acknowledgment bit and no arbitration. A peripheral needs little more than a shift register and some control logic.
- Push-pull signaling. Every line is actively driven high or low. The bus does not rely on pull-up resistors, so edges are sharp and high speeds are practical.
The Basic Picture
A single controller and a single peripheral connect with four signal lines plus a shared ground:
| Signal | Direction | Purpose |
|---|---|---|
| SCLK (also SCK) | Controller to peripheral | Clock that times every bit |
| MOSI | Controller to peripheral | Data from the controller (Main Out, Sub In) |
| MISO | Peripheral to controller | Data from the peripheral (Main In, Sub Out) |
| CS (also SS) | Controller to peripheral | Chip select, which tells the peripheral to listen |
Some datasheets use newer names. COPI and CIPO replace MOSI and MISO. SDI and SDO ("serial data in" and "serial data out") are also common, and they are named from the point of view of the chip you are reading about. Always check which device an "in" or "out" label refers to before you wire anything.
The next chapters take each signal in turn. Here, the key idea is that the controller owns the clock and the select line. A peripheral never starts a transfer on its own. If a peripheral has news for the controller, for example "a new sample is ready", it usually uses an extra interrupt pin that is separate from SPI.
How a Transfer Looks
A transfer is a sequence of clock pulses while chip select is held low. On each pulse, the controller shifts one bit out on MOSI, and the peripheral shifts one bit out on MISO. After eight pulses, one byte has moved in each direction.
This has a consequence that surprises many beginners: you cannot read without also writing. To receive a byte from the peripheral, the controller must clock eight pulses, and clocking pulses means sending something on MOSI. When you only want to read, you send a dummy byte, commonly 0x00. Likewise, every byte you write also returns a byte from the peripheral. If you do not need it, you ignore it.
Here is the smallest useful Arduino example. It sends one byte and prints the byte that came back.
#include <SPI.h>
const uint8_t CS_PIN = 10;
void setup() {
Serial.begin(115200);
pinMode(CS_PIN, OUTPUT);
digitalWrite(CS_PIN, HIGH); // keep the peripheral deselected
SPI.begin();
}
void loop() {
SPI.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE0));
digitalWrite(CS_PIN, LOW); // select the peripheral
uint8_t reply = SPI.transfer(0x9F); // send one byte, receive one byte
digitalWrite(CS_PIN, HIGH); // end the transfer
SPI.endTransaction();
Serial.print("Reply: 0x");
Serial.println(reply, HEX);
delay(1000);
}SPI.transfer() sends the byte you give it and returns the byte received during the same eight clock pulses. The value 0x9F here is only an example. Many flash chips use it as an "identify" command, but the first byte returned is usually meaningless, because the peripheral has not yet seen the command when it begins to answer. Real devices define which later bytes carry useful data. The chapter on datasheet command formats covers this.
If nothing is connected, the example still runs. With MISO floating or pulled high, you will typically read 0xFF or random values. That is a first hint of a common failure: SPI has no acknowledgment, so a missing peripheral looks the same as a working one from the code's point of view.
Where SPI Is Used
SPI shows up wherever a microcontroller needs to move a lot of data to or from a nearby chip. Typical uses include:
- Displays. Small TFT, OLED and e-paper screens receive pixel data over SPI. A 240 by 320 pixel screen at 16 bits per pixel needs 153,600 bytes per full frame. Fast SPI makes this tolerable.
- Memory. SPI flash chips and FRAM store firmware, logs and fonts. SD cards also accept SPI mode, which is how most Arduino SD libraries talk to them.
- Sensors. Accelerometers, gyroscopes, pressure sensors and thermocouple amplifiers often offer SPI as well as I2C. SPI is the better choice when you sample at high rates.
- Converters. Analog-to-digital and digital-to-analog converters use SPI to move samples quickly and with predictable timing.
- Radios. Many wireless modules, including common 2.4 GHz and LoRa transceivers, use SPI for commands and packet data.
- Port expanders and shift-style chips. LED drivers and I/O expanders add outputs to a board using only a few pins.
Most Arduino boards expose a hardware SPI peripheral on fixed pins. On an Uno-class board with an ATmega328P, SCLK is pin 13, MISO is pin 12, MOSI is pin 11, and pin 10 is the conventional chip select. Other boards use different pins, and some route SPI to a separate header. Check your board's documentation before wiring.
Strengths and Costs
SPI is a good fit when you want speed and simple chips. It has costs, and you should know them before you choose it for a design.
| Strength | Cost |
|---|---|
| High clock rates, often over 1 MHz and sometimes far more | Needs one chip-select line per peripheral, so pin count grows with each device |
| Full duplex with no protocol overhead | No acknowledgment, so a missing or faulty device is silent |
| No address bytes at the bus level | No formal standard, so modes and command formats differ between chips |
| Push-pull outputs give clean edges | No built-in error checking, and long wires cause trouble at high speed |
Because SPI wiring is not defined by a standard, the same sketch can work with one chip and fail with another. The usual cause is a mismatch in one of three settings: the clock mode, the bit order or the clock speed. These three settings are the main subject of the middle chapters, and they are the reason the Arduino library groups them into a single SPISettings object.
Common Early Mistakes
These errors cause most first-time SPI failures. Later chapters explain each one.
- Forgetting to set chip select high at start-up. If a pin is left as an input, it floats and the peripheral may see random select pulses.
- Using the wrong mode. The data arrives shifted by one bit or looks inverted.
- Setting the clock too fast for the peripheral or for the wiring.
- Mixing up MOSI and MISO. Two chips with "in" and "out" pins labeled from different viewpoints make this easy.
- Missing ground connection between boards. Without a shared ground, the logic levels have no common reference.
- Voltage mismatch. A 5 V Arduino driving a 3.3 V peripheral can damage it. Check logic levels first.
How the Rest of This Tutorial Is Organized
The remaining chapters build on this overview in order. They cover the four signals in detail, then how shift registers exchange bits, then chip select and framing. After that come the four SPI modes, bit order, and clock rate limits. With the theory in place, the tutorial turns to the Arduino SPI library, transactions, reading datasheet command formats, sharing a bus, and bulk transfers. It closes with wiring and signal quality, debugging with a logic analyzer, comparison with I2C and UART, and the dual and quad SPI variants.
Exercise
You want to read a single byte from an SPI sensor. Why must you still send a byte on MOSI, and what value do you usually send?
Show answer
SPI is full duplex and the peripheral shifts a bit out on MISO only while the controller clocks a bit in on SCLK. The controller can only generate clock pulses by performing a transfer, and a transfer always sends a byte on MOSI. To read without writing anything meaningful, you send a dummy byte, usually 0x00, and use the byte that SPI.transfer() returns.
Exercise
Your sketch runs with no errors, but every byte read from a new SPI sensor is 0xFF. Name two likely causes that come from this chapter.
Show answer
Likely causes include a missing or loose connection on MISO, power or ground, or a chip-select line that never goes low, so the peripheral never drives MISO. A floating or pulled-high MISO reads as 0xFF. SPI has no acknowledgment, so the code cannot tell a missing peripheral from a working one.
Exercise
A project needs a display, an SD card and a pressure sensor on one Arduino. How many chip-select lines does it need, and why?
Show answer
Three, one per peripheral. SPI has no address on the bus, so the controller picks which peripheral listens by pulling that device's own chip-select line low. The three devices can share SCLK, MOSI and MISO.