SPI Mode Calculator

Convert between SPI mode number and CPOL/CPHA, see the clock idle state and sample edge, and check the equivalent Arduino, STM32 HAL, and Linux spidev names.

SPI mode calculator

Result

SPI modeSPI Mode 0
Clock idle level-
Data sampled on-
Data shifted on-

Clock timing diagram

Sample edge    Shift / change edge

SPI mode reference by platform

The mode number is the same standard across platforms: mode = (CPOL << 1) | CPHA. Use this table to match your library's naming to the CPOL/CPHA your peripheral's datasheet specifies.

Mode CPOL/CPHA Arduino SPI STM32 HAL Linux spidev
00 / 0SPI_MODE0POLARITY_LOW, PHASE_1EDGESPI_MODE_0
10 / 1SPI_MODE1POLARITY_LOW, PHASE_2EDGESPI_MODE_1
21 / 0SPI_MODE2POLARITY_HIGH, PHASE_1EDGESPI_MODE_2
31 / 1SPI_MODE3POLARITY_HIGH, PHASE_2EDGESPI_MODE_3

What are CPOL and CPHA?

CPOL (Clock Polarity) sets the SPI clock idle level: 0 means idle low, 1 means idle high. CPHA (Clock Phase) sets which clock edge data is sampled on: 0 samples on the leading edge of each clock cycle, 1 samples on the trailing edge. The four combinations of CPOL and CPHA give the four SPI modes, numbered 0 through 3.

How to find the SPI mode your device needs

Check the peripheral's datasheet timing diagram or electrical characteristics section. Many datasheets state the mode number directly; others only show CPOL and CPHA, or a clock waveform with the data valid window marked. Match that waveform against the diagram above to identify CPOL and CPHA, then read off the mode number.

What happens with the wrong SPI mode

If CPOL is wrong, the clock idles at the wrong level and the very first bit can be missampled or shifted by one bit position. If CPHA is wrong, every bit is sampled on the wrong edge, which usually produces consistently garbled or shifted data rather than an intermittent failure, making a mode mismatch one of the first things worth checking when a new SPI device returns nonsense.

FAQ

What is the difference between SPI mode 0 and mode 3?

Mode 0 uses CPOL 0 and CPHA 0, so the clock idles low and data is sampled on the rising edge. Mode 3 uses CPOL 1 and CPHA 1, so the clock idles high and data is also sampled on the rising edge, just relative to a different idle level.

How do I know which SPI mode a sensor or display needs?

Check its datasheet for CPOL and CPHA values, an explicit mode number, or a clock timing diagram. If only a diagram is given, compare the clock idle level and the edge where data changes against the diagram on this page to identify the mode.

Is SPI mode the same across Arduino, STM32, and Linux?

The underlying CPOL/CPHA electrical behavior is identical, and the mode numbering 0 to 3 is the same standard everywhere, but each platform names it differently in code, such as SPI_MODE0 on Arduino or SPI_POLARITY_LOW with SPI_PHASE_1EDGE on STM32 HAL.

What is the most common SPI mode?

Mode 0 (CPOL 0, CPHA 0) is the most common default across many microcontrollers, sensors, and SD cards, but always confirm against the specific device datasheet since several common parts use mode 1, 2, or 3.

Can I convert a mode number back into CPOL and CPHA?

Yes. Since mode equals CPOL shifted left by one bit combined with CPHA, CPOL is the mode number's upper bit and CPHA is its lower bit, which is exactly what the mode buttons on this page compute for you.

Related tools