Sub topic 14
Debugging SPI with a Logic Analyzer
Assume you know the SPI signals, the four clock modes, Arduino SPISettings, and basic digital wiring. Debugging is easier when you separate physical problems from protocol problems. First verify that signals reach the analyzer. Then verify timing. Finally compare decoded bytes with the peripheral's datasheet.
Start with a known-good capture
Connect the logic analyzer ground to the Arduino ground. Connect probes to SCLK, MOSI, MISO, and the selected chip-select line. Use a sampling rate several times higher than the SPI clock. For a 4 MHz clock, start with at least 20 MHz sampling.
Set chip select high while the bus is idle. Trigger on chip select falling low, because that usually marks the beginning of a transaction.
A useful capture should show chip select low, a regular clock, and the expected number of clock pulses. For an eight-bit command followed by an eight-bit reply, expect 16 clock pulses. If chip select rises halfway through, the peripheral may treat the exchange as two incomplete commands.
Read the waveform before trusting the decoder
Check these signals in order:
- Chip select: Is the correct device selected? Is it stable high when unused? A floating input can randomly select a peripheral.
- SCLK: Does it have the expected idle level? Does its frequency match your configured rate?
- MOSI: Does the command match the datasheet, including read/write flags and address bits?
- MISO: Does the peripheral drive a response? A constant
0x00or0xFFoften means wrong chip select, missing power or ground, incorrect wiring, or a device that needs more time. - Voltage levels: Are the analyzer and peripheral compatible with the Arduino's logic voltage?
Configure the decoder with the correct bit order, clock mode, and bits per word. A decoder can display convincing but incorrect bytes when the mode is wrong. Compare the raw edges with the datasheet timing diagram instead of changing settings randomly.
| Observation | Likely cause | Next check |
|---|---|---|
| No clock pulses | SPI not started or wrong pins | Call `SPI.begin()` and verify board pin mapping |
| Clock exists, no response | Wrong CS or wiring | Trace CS, MISO, power, and ground |
| Bytes are shifted | Wrong CPHA or sampling edge | Compare mode with the timing diagram |
| Works slowly only | Excessive clock rate or poor wiring | Lower speed and shorten connections |
Use controlled changes
Change one variable at a time. First run a low-speed transaction, such as 100 kHz, with short wires. If it works, increase the speed gradually. If it fails at a particular rate, inspect ringing, long ground paths, weak pull-ups, and the peripheral's maximum clock specification.
For a register read, capture both the command byte and the dummy bytes used to clock the reply. Many devices do not return data during the command byte. The analyzer should show the response at the position documented by the device protocol.
Exercise
Exercise
A capture shows the correct command on MOSI, but MISO remains high (0xFF) and the clock mode appears correct. Name two checks to make next.
Show answer
Check that chip select reaches the intended peripheral and that the peripheral has power and a shared ground. Then verify MISO wiring and whether another device is driving the shared MISO line.