Sub topic 5
Clock Polarity and Phase: The Four SPI Modes
Assume you know Arduino setup() and loop(), digital output, binary numbers, and how to read a basic wiring diagram. This chapter explains how to select the SPI clock mode from a peripheral datasheet and configure it in an Arduino sketch.
The working model
SPI sends one bit for each clock pulse. The clock signal, usually called SCLK or SCK, has two important properties:
- Clock polarity (CPOL) is the clock's idle level.
- CPOL 0 means the clock rests low.
- CPOL 1 means the clock rests high.
- Clock phase (CPHA) determines which clock edge receives the bit and which edge changes the bit.
Each pulse has a leading edge, which is the first transition away from the idle level, and a trailing edge, which returns toward the idle level.
For CPHA 0, the peripheral samples data on the leading edge. For CPHA 1, it samples data on the trailing edge.
| SPI mode | CPOL | CPHA | Idle clock | Sample edge | Change edge |
|---|---|---|---|---|---|
| Mode 0 | 0 | 0 | Low | Rising | Falling |
| Mode 1 | 0 | 1 | Low | Falling | Rising |
| Mode 2 | 1 | 0 | High | Falling | Rising |
| Mode 3 | 1 | 1 | High | Rising | Falling |
The controller and peripheral must use the same mode. If they disagree, both devices still produce clock pulses, but the receiver reads bits at the wrong times.
Reading a datasheet timing diagram
Look for labels such as “SPI mode,” “CPOL,” “CPHA,” or “clock phase.” Some datasheets describe the behavior instead:
- Identify whether SCLK is high or low when chip select becomes active. This gives CPOL.
- Find the edge where the device samples MOSI or changes MISO. This gives CPHA.
- Check whether the device expects the most significant bit or least significant bit first. That setting is separate from the clock mode.
A timing diagram often marks a sampling point with an arrow or shaded region. Data must be stable around that point. The other edge is normally used to prepare the next bit.
Configuring the Arduino library
Pass the mode to SPISettings. The following example uses mode 3, but the mode value must come from the peripheral's datasheet.
#include <SPI.h>
const uint8_t CS_PIN = 10;
SPISettings deviceSettings(1000000, MSBFIRST, SPI_MODE3);
void setup() {
pinMode(CS_PIN, OUTPUT);
digitalWrite(CS_PIN, HIGH);
SPI.begin();
}
void loop() {
SPI.beginTransaction(deviceSettings);
digitalWrite(CS_PIN, LOW);
SPI.transfer(0x9F); // Example command; check the device datasheet.
digitalWrite(CS_PIN, HIGH);
SPI.endTransaction();
delay(1000);
}Common errors
- Choosing mode 0 because it is the default in examples.
- Confusing “first edge” with “rising edge.” The first edge depends on CPOL.
- Changing SCLK manually while the hardware SPI peripheral is active.
- Leaving SCLK at the wrong idle level between transactions.
- Testing only one returned byte when the command format also requires an address or dummy byte.
Exercise
A datasheet says SCLK idles high, and the peripheral samples data on the first edge after chip select goes low. Which SPI mode should you try?
Show answer
CPOL is 1 because SCLK idles high. The first edge is the falling leading edge, and sampling on that edge means CPHA is 0. Use SPI_MODE2.