Try It: SPI Mode Timing Visualizer
Try Keep Data byte (hex) as A5 and SPI Mode on Mode 0, then select Mode 1 to compare sampling markers.
Observe Mode 0 shows clock idle LOW, rising-edge sampling and MOSI bits 10100101; the same byte remains visible as SPI Mode changes.
Explain SPI Mode determines CPOL and CPHA; together they choose rising or falling sampling, with shifting assigned to the opposite edge.
Calculate SPI edges
Reset
Technical boundaries. The display assumes eight bits drawn most significant bit first at fixed visual spacing; it has no clock-rate or device-delay model. Unparseable hex falls back to A5, while parsed values supply their low eight bits without a byte-range validation check.
Try It: SPI Mode Timing Visualizer
Use clock edges and data timing to see where the receiver samples a bit.
Select an SPI mode and see how CPOL and CPHA affect the clock signal and data sampling edges. Understanding which clock edge is used for sampling helps you debug corrupted sensor readings.
viewof spiModeSelect = Inputs. radio (["Mode 0" , "Mode 1" , "Mode 2" , "Mode 3" ], {value : "Mode 0" , label : "SPI Mode" })
viewof spiVisDataByte = Inputs. text ({label : "Data byte (hex)" , value : "A5" , placeholder : "e.g. A5" })
spiModeNum = spiModeSelect === "Mode 0" ? 0 : spiModeSelect === "Mode 1" ? 1 : spiModeSelect === "Mode 2" ? 2 : 3
spiCPOL = spiModeNum >= 2 ? 1 : 0
spiCPHA = spiModeNum % 2
spiIdleStr = spiCPOL === 0 ? "LOW" : "HIGH"
spiSampleEdge = (spiCPOL === 0 && spiCPHA === 0 ) ? "Rising" :
(spiCPOL === 0 && spiCPHA === 1 ) ? "Falling" :
(spiCPOL === 1 && spiCPHA === 0 ) ? "Falling" : "Rising"
spiShiftEdge = spiSampleEdge === "Rising" ? "Falling" : "Rising"
// Parse hex data byte
spiParsedByte = parseInt (spiVisDataByte. replace (/ ^ 0x/i , "" ), 16 )
spiByteBits = isNaN (spiParsedByte) ? [1 , 0 , 1 , 0 , 0 , 1 , 0 , 1 ] : Array . from ({length : 8 }, (_, i) => (spiParsedByte >> (7 - i)) & 1 )
html `<div style="background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%); border-radius: 12px; padding: 20px; margin-top: 0.5rem; font-family: 'Courier New', monospace; color: white;">
<div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 16px;">
<span style="color: #3498DB; font-weight: bold; font-size: 1.1em;">SPI ${ spiModeSelect} </span>
<span style="color: #7F8C8D; font-size: 0.85em;">CPOL= ${ spiCPOL} | CPHA= ${ spiCPHA} </span>
</div>
<div style="margin-bottom: 12px;">
<div style="color: #E67E22; font-size: 0.85em; margin-bottom: 4px;">SCK (Clock idle ${ spiIdleStr} )</div>
<div style="display: flex; height: 40px; align-items: flex-end;">
${ spiByteBits. map ((_, i) => {
const highFirst = (spiCPOL === 0 && spiCPHA === 0 ) || (spiCPOL === 1 && spiCPHA === 1 );
const clkHigh = spiCPOL === 0 ;
return html `<div style="display: flex; width: 50px;">
<div style="width: 25px; height: ${ (highFirst ? clkHigh : ! clkHigh) ? '36px' : '12px' } ; background: #3498DB; border: 1px solid #2980B9;"></div>
<div style="width: 25px; height: ${ (highFirst ? ! clkHigh : clkHigh) ? '36px' : '12px' } ; background: #3498DB; border: 1px solid #2980B9;"></div>
</div>` ;
})}
</div>
</div>
<div style="margin-bottom: 12px;">
<div style="color: #16A085; font-size: 0.85em; margin-bottom: 4px;">MOSI (Data: 0x ${ isNaN (spiParsedByte) ? "A5" : spiParsedByte. toString (16 ). toUpperCase (). padStart (2 , "0" )} )</div>
<div style="display: flex; height: 40px; align-items: flex-end;">
${ spiByteBits. map ((bit, i) => html `<div style="width: 50px; height: ${ bit ? '36px' : '12px' } ; background: #16A085; border: 1px solid #1ABC9C; display: flex; align-items: center; justify-content: center;">
<span style="font-size: 11px; color: white; font-weight: bold;"> ${ bit} </span>
</div>` )}
</div>
</div>
<div style="margin-bottom: 8px;">
<div style="color: #E74C3C; font-size: 0.85em; margin-bottom: 4px;">Sample Points ( ${ spiSampleEdge} edge)</div>
<div style="display: flex; height: 20px;">
${ spiByteBits. map ((_, i) => {
const offset = spiCPHA === 0 ? "12px" : "37px" ;
return html `<div style="width: 50px; position: relative;">
<div style="position: absolute; left: ${ offset} ; top: 2px; width: 0; height: 0; border-left: 6px solid transparent; border-right: 6px solid transparent; border-top: 10px solid #E74C3C;"></div>
</div>` ;
})}
</div>
</div>
<div style="margin-top: 16px; padding: 12px; background: rgba(255,255,255,0.08); border-radius: 6px; font-family: sans-serif; font-size: 0.85em;">
<p style="margin: 0 0 6px 0;"><strong style="color: #3498DB;">Clock idle state:</strong> <span style="color: #E9ECEF;"> ${ spiIdleStr} (CPOL= ${ spiCPOL} )</span></p>
<p style="margin: 0 0 6px 0;"><strong style="color: #E74C3C;">Data sampled on:</strong> <span style="color: #E9ECEF;"> ${ spiSampleEdge} edge (CPHA= ${ spiCPHA} )</span></p>
<p style="margin: 0 0 6px 0;"><strong style="color: #16A085;">Data shifted on:</strong> <span style="color: #E9ECEF;"> ${ spiShiftEdge} edge</span></p>
<p style="margin: 0; color: #7F8C8D;">Common sensors: ${ spiModeNum === 0 ? "MAX31855, BME280, most SPI sensors (default)" : spiModeNum === 1 ? "Some ADCs, less common" : spiModeNum === 2 ? "Rare, some legacy devices" : "ADXL345 accelerometer, MPU-6000 IMU" } </p>
</div>
</div>`
Back to the chapter