Sub topic 2
The four signals: SCLK, MOSI, MISO and chip select
This chapter assumes you can write a basic Arduino sketch with setup and loop, use pinMode and digitalWrite, and read a pin diagram well enough to wire a breadboard. It covers what each SPI wire does, who drives it, how to find it on your board, and which wiring mistakes cause trouble. Clock timing, modes and framing each get their own chapter.
One shared clock, two data lines, one select line
SPI (Serial Peripheral Interface) connects one controller to one or more peripherals with four signals. The controller is the device that starts every transfer and generates the clock. In an Arduino project, the controller is the microcontroller board.
| Signal | Full name | Driven by | Purpose |
|---|---|---|---|
| SCLK (also SCK) | Serial clock | Controller | Marks when each bit is valid |
| MOSI | Master Out, Slave In | Controller | Carries data from controller to peripheral |
| MISO | Master In, Slave Out | Peripheral | Carries data from peripheral to controller |
| CS (also SS, SSn) | Chip select | Controller | Tells one peripheral that it is being addressed |
The wire names describe direction relative to the controller. MOSI is an output on the controller and an input on the peripheral. MISO is the reverse.
Terminology you will meet
Older datasheets and the Arduino core call the roles "master" and "slave". Since 2021 the Open Source Hardware Association recommends COPI (controller out, peripheral in) instead of MOSI, CIPO instead of MISO, and CS instead of SS. Some vendors also write SDI and SDO, meaning serial data in and out from that chip's point of view. On a peripheral labeled SDI, you connect the controller's MOSI to it. Always translate a label into "does this pin receive or transmit?" before wiring.
SCLK: the clock that everything follows
SPI is synchronous. The controller produces a clock on SCLK, and both sides read or change data in step with its edges. No baud rate has to be agreed in advance, as it does with UART. This is why SPI can run at several megahertz on a board where UART tops out far lower.
Three practical points follow from this:
- The peripheral only needs the clock to be within its rated maximum. The controller sets the speed, and the peripheral follows.
- When SCLK is idle, it rests at a fixed level (low or high, depending on the mode). A glitch on an idle clock line can be read as a bit.
- Without clock pulses, no data moves in either direction. A peripheral can never send data on its own. The controller must clock it out, even when it only wants to read.
MOSI and MISO: two one-way data lines
Because data has a dedicated wire in each direction, SPI can send and receive at the same moment. Each clock pulse moves one bit on MOSI and one bit on MISO. The next chapter explains the shift-register mechanism. For now, remember that a "read" and a "write" are not separate hardware operations. Every transfer does both, and your code chooses to ignore one half.
MISO is shared, so it must be tri-stated
If several peripherals connect their outputs to the same MISO wire, only one may drive it at a time. A peripheral that is not selected must put its MISO pin into a high-impedance (tri-state) condition. In that state the pin neither pulls high nor low, so it does not disturb the line.
Most SPI chips do this correctly. Some do not, and they keep driving MISO while deselected. The symptom is a second peripheral that returns garbage only when the first chip is also connected. Check the datasheet for a "MISO tri-state when CS high" statement. If a chip lacks it, put a buffer such as a 74HC125 on its MISO line.
Chip select: choosing who listens
Each peripheral has its own CS line from the controller. It is usually active low: the peripheral listens when CS is low and ignores the bus when CS is high. The controller drives CS from an ordinary GPIO pin, so you can use any free pin. (The next chapter covers framing in detail.)
Two points affect wiring now:
- Idle CS must be high. A floating CS input can drift low and cause the peripheral to react to bus noise. Set the pin high in
setupbefore anything else, and add a 10 kΩ pull-up resistor to the peripheral's supply if the board may boot with the pin undefined. A GPIO is an input until your sketch sets it as an output, and that gap can cause trouble. - A peripheral with CS low and no clock does nothing. Holding CS low does not start a transfer. Clock pulses do.
Finding the pins on your board
The Arduino core defines the controller's hardware SPI pins as the constants SCK, MOSI, MISO and SS. Hardware SPI uses fixed pins on most AVR boards, so you cannot move them.
| Board | SCK | MISO | MOSI | SS |
|---|---|---|---|---|
| Uno, Nano | 13 | 12 | 11 | 10 |
| Mega 2560 | 52 | 50 | 51 | 53 |
| Leonardo | ICSP header only | ICSP header only | ICSP header only | not broken out |
Other boards, including ESP32 and RP2040 boards, define their own defaults, and many can remap SPI to other pins. Check your board's pinout. The ICSP header exposes the same SPI signals on a six-pin connector. On an Uno the ICSP pins are wired to pins 11, 12 and 13, which is why some shields use it for compatibility between boards.
The SS pin trap on AVR boards
On AVR-based boards, the hardware SPI block watches the SS pin (10 on the Uno). If SS is configured as an input and goes low, the hardware assumes another controller wants the bus and silently switches itself to peripheral mode. Your transfers then stop working with no error.
To avoid this, always set pin 10 (or 53 on a Mega) as an OUTPUT, even when you use a different pin for chip select. The sketch below does so by using pin 10 as CS.
A first run: reading the pin constants and testing with a jumper
This sketch prints the pin numbers for your board, then sends one byte and prints what came back. Connect a jumper wire from MOSI to MISO. Every bit the controller sends on MOSI then returns on MISO, so you should read back exactly what you sent. This is a quick way to check that the controller side of the bus works before you add a peripheral.
#include <SPI.h>
const uint8_t CS_PIN = 10; // also the AVR SS pin on an Uno
void setup() {
Serial.begin(115200);
pinMode(CS_PIN, OUTPUT);
digitalWrite(CS_PIN, HIGH); // deselected before anything else happens
SPI.begin();
Serial.print(F("SCK=")); Serial.println(SCK);
Serial.print(F("MISO=")); Serial.println(MISO);
Serial.print(F("MOSI=")); Serial.println(MOSI);
Serial.print(F("SS=")); Serial.println(SS);
}
void loop() {
SPI.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE0));
digitalWrite(CS_PIN, LOW);
uint8_t reply = SPI.transfer(0xA5);
digitalWrite(CS_PIN, HIGH);
SPI.endTransaction();
Serial.print(F("sent 0xA5, received 0x"));
Serial.println(reply, HEX);
delay(1000);
}With the jumper in place, the output is received 0xA5. If you remove the jumper, MISO floats, and you will see an unstable value or a constant 0x00 or 0xFF. A constant 0xFF is the classic sign of a MISO line that is pulled high or not connected. A constant 0x00 suggests a line held low, a shorted wire, or a dead peripheral.
Wiring rules for each signal
- Share SCLK, MOSI and MISO across all peripherals. Give each peripheral its own CS pin.
- Match voltage levels. A 5 V controller driving a 3.3 V peripheral can damage it. Use a level shifter on SCLK, MOSI and CS. MISO needs shifting too when the peripheral's output is lower than the controller's input threshold.
- Keep wires short. Long jumpers at several megahertz ring and distort edges. The signal integrity chapter returns to this.
- Share ground. Every device on the bus needs a common ground reference, or the logic levels have no meaning.
Common faults tied to the signals
| Symptom | Likely cause |
|---|---|
| Reads always return `0xFF` | MISO unconnected, peripheral unpowered, or CS never goes low |
| Reads always return `0x00` | MISO shorted to ground or CS wired to the wrong pin |
| Works alone, fails with a second device | Other peripheral's CS floating, or its MISO not tri-stated |
| Worked, then stops after a few calls | AVR SS pin left as an input and driven low |
| Garbled values at higher speed | Wires too long or clock rate beyond the device limit |
Exercise
A temperature sensor's datasheet labels its pins SDI, SDO, SCK and CSB. Which Arduino SPI signals do you connect to each, and why is the label "SDI" ambiguous without that context?
Show answer
SDI connects to the controller's MOSI, SDO connects to MISO, SCK connects to SCK, and CSB connects to a GPIO you drive as chip select (active low, as the "B" suffix indicates). SDI and SDO are named from the sensor's point of view: serial data in to the sensor and serial data out of the sensor. Those names are the reverse of what you would guess if you read them from the controller's side, so you map them by direction, not by name.
Exercise
You wire a peripheral to an Uno, use pin 8 as chip select, and leave pin 10 unused and unconfigured. Transfers work for a while, then return nothing. What is the most likely cause and the fix?
Show answer
On AVR boards, pin 10 is the hardware SS pin. If it is an input and a signal pulls it low, the SPI hardware switches itself to peripheral mode and stops acting as controller. Fix this by setting pin 10 as an OUTPUT in setup (for example pinMode(10, OUTPUT);), even though you use pin 8 for chip select.