Sub topic 14
Debugging SPI with a logic analyzer
This chapter assumes you can write a basic SPI sketch with SPISettings and beginTransaction. It also assumes you know the four modes, the role of chip select, and how a datasheet describes a register read. When an SPI device returns 0x00 or 0xFF where you expected data, a logic analyzer shows you what the wires carried. You then compare that with what the datasheet says they should have carried.
What a logic analyzer shows
A logic analyzer samples several digital pins at a fixed rate and draws each one as a high or low waveform over time. Most analyzer software also includes a protocol decoder for SPI. You tell it which channel is SCLK, MOSI, MISO and chip select, and which mode, bit order and word size to assume. It then prints the bytes it sees.
You can use a cheap USB analyzer with free software such as Sigrok/PulseView or Saleae Logic. A scope also works and is better for analog problems, but a logic analyzer is easier for reading bytes.
Set these before you capture:
- Sample rate: at least 4 times the SCLK frequency, and preferably 10 times. At 4 MHz SCLK, sample at 16 MS/s or more. Too low a rate makes edges move or vanish.
- Trigger: trigger on the falling edge of chip select. Each capture then starts at the beginning of a transaction.
- Ground: connect the analyzer ground to the board ground. Without it the readings are meaningless.
- Voltage threshold: check that the analyzer accepts your logic level (3.3 V or 5 V).
A systematic debugging order
Work from the cheapest check to the most expensive. Most faults appear in the first three steps.
- Check wiring and power. Confirm that the peripheral has power and ground, that MOSI goes to the peripheral's input (often labeled SDI or DIN), and that MISO goes to its output (SDO or DOUT). Labels such as "MOSI" on a peripheral are inconsistent between vendors, so trust the direction of the signal, not the name.
- Check that the clock exists. Capture SCLK and chip select. If SCLK does not toggle, the sketch is not reaching
SPI.transfer, or SCLK is wired to the wrong pin. - Check chip select. CS must fall before the first clock edge and rise after the last. It must not glitch between bytes of one command.
- Check the bytes on MOSI. Compare the decoded command with the datasheet.
- Check the bytes on MISO. If MOSI is right but MISO is wrong, look at mode, clock speed and the peripheral's state.
Reading the symptoms
The same few symptoms appear again and again. The table maps what you see in the capture to the usual cause.
| What you see | Likely cause | Fix |
|---|---|---|
| MISO stuck high (`0xFF` on every read) | Peripheral not selected, not powered, or MISO unconnected with a pull-up | Check CS pin number and polarity, power, and the MISO wire |
| MISO stuck low (`0x00` on every read) | MISO shorted to ground, peripheral in reset, or wrong CS | Check the wire and the reset pin |
| Data is shifted by one bit, or looks like a bit-rotated version of the right value | Wrong clock phase (CPHA) | Try the other mode with the same CPOL |
| Data is inverted or shifted, and SCLK idles at the wrong level | Wrong clock polarity (CPOL) | Compare SCLK idle level with the datasheet |
| Bytes look reversed, such as `0x01` appearing as `0x80` | Wrong bit order | Change `MSBFIRST` to `LSBFIRST`, or the reverse |
| Right at low speed, garbled at high speed | Clock too fast for the device or the wires | Lower the rate in `SPISettings` and shorten wires |
| First byte of a read is junk | The peripheral returns data only after the command byte, so the first reply byte is meaningless | Ignore it, or send a dummy byte to clock out the real reply |
| Works alone, fails when a second device is added | Floating CS on the second device, or a settings conflict | Pull the unused CS high and use transactions |
Worked example: a read that returns 0xFF
Suppose a sketch reads a device ID register and prints FF. The datasheet says the register at 0x0F holds 0x33. Here is a small test sketch that reads the register and prints both the command and the reply, so you can line it up with the capture.
#include <SPI.h>
const uint8_t CS_PIN = 10;
const uint8_t READ_FLAG = 0x80; // device-specific: many chips use the top bit for read
uint8_t readRegister(uint8_t reg) {
SPI.beginTransaction(SPISettings(500000, MSBFIRST, SPI_MODE0));
digitalWrite(CS_PIN, LOW);
SPI.transfer(reg | READ_FLAG);
uint8_t value = SPI.transfer(0x00);
digitalWrite(CS_PIN, HIGH);
SPI.endTransaction();
return value;
}
void setup() {
Serial.begin(115200);
pinMode(CS_PIN, OUTPUT);
digitalWrite(CS_PIN, HIGH);
SPI.begin();
}
void loop() {
uint8_t id = readRegister(0x0F); // example address; take it from your datasheet
Serial.print("Sent 0x");
Serial.print(0x0F | READ_FLAG, HEX);
Serial.print(", received 0x");
Serial.println(id, HEX);
delay(1000);
}The rate is deliberately slow at 500 kHz. A slow clock removes speed as a suspect and gives the analyzer plenty of samples per bit.
Capture one transaction and read the decoder output. A correct capture shows MOSI 8F 00 and MISO xx 33, where the first reply byte is ignored. Now compare against what you actually see:
- MOSI shows
8F 00and MISO isFF FF: the master is sending the right command but the peripheral is not answering. Check that CS actually falls on the peripheral's pin, that it has power, and that MISO reaches the Arduino. - MOSI shows
0F 00: the read flag is missing or wrong for this device. Re-read the datasheet section on the command byte. - CS never falls:
CS_PINdoes not match the wire. On many boards pin 10 is the default hardware chip select, but a breakout may be wired elsewhere. - MISO has the right data one bit late: change the mode and retry.
Checking the mode from the waveform
You can confirm the mode by eye. Look at SCLK when CS is high. A low idle level means CPOL = 0, a high idle level means CPOL = 1. Then look at where MISO and MOSI change relative to the clock edges. In mode 0 and mode 2 the data is stable at the first clock edge of each bit, and the receiver samples there. In mode 1 and mode 3 the data changes at the first edge and is sampled at the second.
Compare this with the timing diagram in the datasheet. If the datasheet shows the data line stable around the rising edge with SCLK idling low, the device uses mode 0. Match the timing diagram, not the label. Some datasheets list the mode by name, others give only the waveform.
Faults that depend on timing and wiring
Some faults do not show up as wrong bytes in the decoder. They show up as sometimes-wrong bytes.
- Clock too fast: edges become slow and rounded on long wires, and MISO changes too late for the master's sample point. Zoom in on an edge. If the signal has not settled before the sampling edge, lower the clock.
- Ringing and overshoot: a sharp edge on a long wire can overshoot and cause false clock edges. Shorten the wire, add a small series resistor (about 22 to 100 ohms) near the source, and keep SCLK away from other signals.
- Floating chip select: during reset or before
pinMode, a CS pin can float and select the device by accident. A pull-up resistor of about 10 kohm to the supply keeps CS high until the sketch drives it. - Voltage level mismatch: a 3.3 V peripheral on a 5 V board may read the signal but can be damaged over time. Use a level shifter on SCLK, MOSI and CS. MISO from a 3.3 V device may be too low for a 5 V input, so it can read as garbage even though the wiring is correct.
- A second SPI user on the bus: an SD card shield, a display and a sensor all share SCLK, MOSI and MISO. If another device's CS is low or floating, two outputs drive MISO at once and the data is corrupted. Capture all CS lines at once and check that only one is low at a time.
Using the capture to prove a fix
After you change something, capture again and compare. Keep the old capture as a reference. A fix is confirmed when the decoded MOSI and MISO bytes match the datasheet's example for the same command, not when the sketch seems to work. If the datasheet gives an expected reset value for a register, such as the 0x33 ID above, use that as a known-good check before you move on to writes and configuration.
Two habits make later debugging faster. First, put a known register read, such as an ID register, in setup and halt with a clear message if it does not match. Second, label the channels in your analyzer software and save the session file next to your sketch, so you can reload the setup without rewiring it from memory.
Exercise
A capture shows chip select falling, MOSI carrying 8F 00, and MISO carrying FF FF on every attempt. SCLK toggles correctly with the idle level the datasheet requires. Name two checks you would make next, and say why changing the SPI mode is unlikely to be the fix.
Show answer
Check that the chip-select wire reaches the peripheral's CS pin and that the peripheral has power and is out of reset. Also check that MISO is actually wired to the peripheral's output pin. A wrong mode produces shifted or garbled data, not a constant FF. A line stuck high points to a peripheral that is not driving MISO at all, so the cause is selection, power, reset or wiring.
Exercise
Your SPI clock is 8 MHz. What is the minimum sample rate you should set on the logic analyzer, and what do you risk if you set it to 10 MS/s?
Show answer
Use at least 32 MS/s (4 times SCLK), and preferably 80 MS/s. At 10 MS/s you get only about one sample per clock period, so edges are located inaccurately, glitches are missed, and the decoder can show wrong bytes even though the real signals are correct.
Exercise
The decoder shows 0x01 where the datasheet says 0x80, and every other byte is also bit-reversed. Which SPISettings argument do you change, and what do you re-check afterward?
Show answer
Change the bit-order argument between MSBFIRST and LSBFIRST. Afterward, capture again and confirm that both the MOSI command bytes and the MISO reply bytes match the datasheet, because the decoder's bit-order setting must also match.