Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/manish04-mu/TheEmbeddedInsights/llms.txt

Use this file to discover all available pages before exploring further.

These four sketches each transmit the byte 0x62 (binary 0110 0010) MSB-first across all four SPI clock modes. The payload and wiring stay completely identical between sketches — only the dataMode argument passed to SPISettings changes. Loading each sketch in turn and capturing the result on a logic analyzer makes the CPOL/CPHA timing differences immediately visible: you can watch the SCK idle state flip and the sampling edge shift without touching a single wire.

Common Configuration

All four sketches share the same hardware setup and the same byte value. Only the dataMode argument inside SPISettings(...) differs:
SettingValue
CS PinPin 10
Clock Speed100000 (byte)
Bit OrderMSBFIRST
Sent Byte0x62
SPI.transfer(sentValue) performs a full-duplex exchange, sending 0x62 on MOSI while capturing the slave’s MISO response into receivedValue, which is then printed to the Serial Monitor in hexadecimal every second.

Mode 0 — CPOL=0, CPHA=0

The clock idles LOW. Data is driven onto MOSI on the falling edge and sampled on the rising (leading) edge. This is the most commonly supported mode and the default for many SPI peripherals.
SPI_Echo_MSB_Mode0.ino
#include <SPI.h>

/*
Pin Description: 
CS  - Chip Select         : Pin-> 10
MOSI- Master Out/Slave In : Pin-> 11
MISO- Master In/Slave Out : Pin-> 12
SCK - Clock Signal        : Pin-> 13
*/

/*
Logic Analyser:
CH0: SCK
CH1: Enable/Chip Select
CH2: MOSI
CH3: MISO
*/


#define CS 10     //Chip Select, Here no Slave is used but logic analyser need Enable signal to decoded the message.

byte ClockSpeed = 100000; //1MHz: Arduino Support upto 16MH. Keep it within the sampling rate of Logic Analyser as well. 

byte sentValue = 0x62;

byte receivedValue;

void setup() {

  Serial.begin(9600);  //Serial mode is used to print the received value in MISO, in case, the logic analyser is not present.

  pinMode(CS, OUTPUT); //Define the Chip Select Pin to Output (Master --> Slave)
  digitalWrite(CS, HIGH);   // Keep slave deselected, usually teh CS is active low (We need to check the slave datasheet for this information.)

  SPI.begin();

}

void loop() {

  SPI.beginTransaction(SPISettings(ClockSpeed, MSBFIRST, SPI_MODE0));
  digitalWrite(CS, LOW);          // Select slave 
  receivedValue = SPI.transfer(sentValue);
  digitalWrite(CS, HIGH);         // Deselect slave

  SPI.endTransaction();           // Release the SPI bus

  Serial.print("Received Value: ");
  Serial.println(receivedValue, HEX);

  delay(1000);

}

Mode 1 — CPOL=0, CPHA=1

The clock idles LOW. Data is driven onto MOSI on the rising edge and sampled on the falling (trailing) edge. The SCK idle state looks identical to Mode 0 on the analyzer, but the MOSI bit transitions now occur one half-cycle later relative to the sampling point.
SPI_Echo_MSB_Mode1.ino
#include <SPI.h>

/*
Pin Description: 
CS  - Chip Select         : Pin-> 10
MOSI- Master Out/Slave In : Pin-> 11
MISO- Master In/Slave Out : Pin-> 12
SCK - Clock Signal        : Pin-> 13
*/

/*
Logic Analyser:
CH0: SCK
CH1: Enable/Chip Select
CH2: MOSI
CH3: MISO
*/


#define CS 10     //Chip Select, Here no Slave is used but logic analyser need Enable signal to decoded the message.

byte ClockSpeed = 100000; //1MHz: Arduino Support upto 16MH. Keep it within the sampling rate of Logic Analyser as well. 

byte sentValue = 0x62;

byte receivedValue;

void setup() {

  Serial.begin(9600);  //Serial mode is used to print the received value in MISO, in case, the logic analyser is not present.

  pinMode(CS, OUTPUT); //Define the Chip Select Pin to Output (Master --> Slave)
  digitalWrite(CS, HIGH);   // Keep slave deselected, usually teh CS is active low (We need to check the slave datasheet for this information.)

  SPI.begin();

}

void loop() {

  SPI.beginTransaction(SPISettings(ClockSpeed, MSBFIRST, SPI_MODE1));
  digitalWrite(CS, LOW);          // Select slave 
  receivedValue = SPI.transfer(sentValue);
  digitalWrite(CS, HIGH);         // Deselect slave

  SPI.endTransaction();           // Release the SPI bus

  Serial.print("Received Value: ");
  Serial.println(receivedValue, HEX);

  delay(1000);

}

Mode 2 — CPOL=1, CPHA=0

The clock idles HIGH. Data is driven onto MOSI on the rising edge and sampled on the falling (leading) edge. On the logic analyzer SCK will be sitting high before CS asserts, dropping low on the first clock edge of each transfer.
SPI_Echo_MSB_Mode2.ino
#include <SPI.h>

/*
Pin Description: 
CS  - Chip Select         : Pin-> 10
MOSI- Master Out/Slave In : Pin-> 11
MISO- Master In/Slave Out : Pin-> 12
SCK - Clock Signal        : Pin-> 13
*/

/*
Logic Analyser:
CH0: SCK
CH1: Enable/Chip Select
CH2: MOSI
CH3: MISO
*/


#define CS 10     //Chip Select, Here no Slave is used but logic analyser need Enable signal to decoded the message.

byte ClockSpeed = 100000; //1MHz: Arduino Support upto 16MH. Keep it within the sampling rate of Logic Analyser as well. 

byte sentValue = 0x62;

byte receivedValue;

void setup() {

  Serial.begin(9600);  //Serial mode is used to print the received value in MISO, in case, the logic analyser is not present.

  pinMode(CS, OUTPUT); //Define the Chip Select Pin to Output (Master --> Slave)
  digitalWrite(CS, HIGH);   // Keep slave deselected, usually teh CS is active low (We need to check the slave datasheet for this information.)

  SPI.begin();

}

void loop() {

  SPI.beginTransaction(SPISettings(ClockSpeed, MSBFIRST, SPI_MODE2));
  digitalWrite(CS, LOW);          // Select slave 
  receivedValue = SPI.transfer(sentValue);
  digitalWrite(CS, HIGH);         // Deselect slave

  SPI.endTransaction();           // Release the SPI bus

  Serial.print("Received Value: ");
  Serial.println(receivedValue, HEX);

  delay(1000);

}

Mode 3 — CPOL=1, CPHA=1

The clock idles HIGH. Data is driven onto MOSI on the falling edge and sampled on the rising (trailing) edge. This is the mirror of Mode 0: the same sampling-edge direction, but with the clock polarity inverted.
SPI_Echo_MSB_Mode3.ino
#include <SPI.h>

/*
Pin Description: 
CS  - Chip Select         : Pin-> 10
MOSI- Master Out/Slave In : Pin-> 11
MISO- Master In/Slave Out : Pin-> 12
SCK - Clock Signal        : Pin-> 13
*/

/*
Logic Analyser:
CH0: SCK
CH1: Enable/Chip Select
CH2: MOSI
CH3: MISO
*/


#define CS 10     //Chip Select, Here no Slave is used but logic analyser need Enable signal to decoded the message.

byte ClockSpeed = 100000; //1MHz: Arduino Support upto 16MH. Keep it within the sampling rate of Logic Analyser as well. 

byte sentValue = 0x62;

byte receivedValue;

void setup() {

  Serial.begin(9600);  //Serial mode is used to print the received value in MISO, in case, the logic analyser is not present.

  pinMode(CS, OUTPUT); //Define the Chip Select Pin to Output (Master --> Slave)
  digitalWrite(CS, HIGH);   // Keep slave deselected, usually teh CS is active low (We need to check the slave datasheet for this information.)

  SPI.begin();

}

void loop() {

  SPI.beginTransaction(SPISettings(ClockSpeed, MSBFIRST, SPI_MODE3));
  digitalWrite(CS, LOW);          // Select slave 
  receivedValue = SPI.transfer(sentValue);
  digitalWrite(CS, HIGH);         // Deselect slave

  SPI.endTransaction();           // Release the SPI bus

  Serial.print("Received Value: ");
  Serial.println(receivedValue, HEX);

  delay(1000);

}

Comparison Table

ModeCPOLCPHAClock IdleSampling EdgeArduino Constant
000LOWRising (leading)SPI_MODE0
101LOWFalling (trailing)SPI_MODE1
210HIGHFalling (leading)SPI_MODE2
311HIGHRising (trailing)SPI_MODE3
0x62 in binary is 0110 0010. Because these sketches use MSBFIRST, bit 7 (value 0) is sent first, followed by bit 6 (1), bit 5 (1), bit 4 (0), bit 3 (0), bit 2 (0), bit 1 (1), and finally bit 0 (0). The resulting MOSI waveform — low, high, high, low, low, low, high, low — is easy to verify at a glance on a logic analyzer, making 0x62 a convenient test value for confirming bit order and mode settings.
Want to see how the same byte looks when the bit order is reversed? Head over to SPI LSB-First Echo Sketches for the LSBFIRST equivalents of all four modes — the MOSI waveform flips, making the bit-order effect immediately obvious when you compare captures side by side.

Build docs developers (and LLMs) love