Sub topic 2
The Four Signals: SCLK, MOSI, MISO, and Chip Select
Assume you know basic Arduino sketches, digital I/O, binary numbers, and how to read a simple wiring diagram. This chapter focuses on the electrical roles of the four main SPI signals. It does not yet cover the detailed timing modes or device-specific commands.
SPI, or Serial Peripheral Interface, is a synchronous serial bus. Synchronous means that one device supplies a clock so both devices agree when each bit is transferred. The device that controls the clock is the controller. A connected sensor, display, memory chip, or converter is a peripheral.
The four bus signals
| Signal | Full name | Direction from controller | Purpose |
|---|---|---|---|
| SCLK | Serial clock | Output | Provides timing pulses for each bit |
| MOSI | Master Out, Slave In | Output | Carries bits from controller to peripheral |
| MISO | Master In, Slave Out | Input | Carries bits from peripheral to controller |
| CS | Chip select | Output | Selects one peripheral for the current transfer |
Many modern documents use controller and peripheral instead of master and slave. Older pin labels may still say MOSI and MISO.
SCLK is usually idle at either a low or high voltage, depending on the peripheral's required SPI mode. Each clock cycle transfers one bit in each direction. The exact edge used for changing and sampling data must match the peripheral's datasheet.
MOSI and MISO are separate data wires. SPI can therefore send and receive at the same time. If the controller only wants to read, it still sends dummy bits on MOSI to generate clock pulses. If the peripheral only receives, the controller can ignore the returned MISO bits.
CS is often called SS, for slave select. It is commonly active low, meaning the controller selects a peripheral by driving its CS pin LOW. The controller normally drives CS HIGH when the peripheral is idle.
Wiring and idle states
Connect each signal to the matching function, not merely to a pin with a similar name. On an Arduino Uno, hardware SPI commonly uses SCK on pin 13, MOSI on pin 11, MISO on pin 12, and a board-specific SS pin. Other Arduino boards use different pins.
Keep CS HIGH before starting SPI. A floating CS input can randomly select a peripheral and produce apparently corrupt data. With multiple peripherals, share SCLK, MOSI, and MISO, but give every peripheral its own CS wire.
#include <SPI.h>
const uint8_t CS_PIN = 10;
void setup() {
pinMode(CS_PIN, OUTPUT);
digitalWrite(CS_PIN, HIGH); // Peripheral starts deselected.
SPI.begin();
}
void loop() {
digitalWrite(CS_PIN, LOW); // Select the peripheral.
SPI.transfer(0x55); // Send one byte and receive one byte.
digitalWrite(CS_PIN, HIGH); // End the transfer.
delay(1000);
}The received byte from SPI.transfer() is present even when the program ignores it. During the transfer, the peripheral must drive MISO. When CS is HIGH, a well-designed peripheral releases MISO so another selected peripheral can use the shared line.
Exercise
A peripheral is selected with CS LOW. The controller sends one command byte but receives no useful reply. Which signal behavior should you check first?
Show answer
Check that SCLK produces the required clock pulses and that the peripheral drives MISO. A read reply cannot appear without clock pulses, and a deselected or miswired MISO line cannot return data.