SPI Not Working
Espressif ESP32
Severity: ModerateWhat Does This Error Mean?
ESP32 SPI not working is almost always caused by incorrect pin assignments, a wrong chip select (CS) pin, or a clock speed that exceeds the device's maximum. Verify MOSI, MISO, SCK, and CS pins in your SPI.begin() call match your physical wiring.
Affected Models
- ESP32 DevKit
- ESP32-WROOM-32
- ESP32-S2
- ESP32-S3
- ESP32-C3
- ESP32 WROVER
Common Causes
- Wrong GPIO numbers for MOSI, MISO, SCK, or CS in SPI.begin()
- Chip select (CS) pin not driven LOW before transaction — device not selected
- SPI clock frequency too high for the connected device
- 3.3V vs 5V logic mismatch — device expects 5V logic levels
- SPI mode mismatch — device requires CPOL/CPHA that differs from default Mode 0
How to Fix It
-
Verify MOSI, MISO, SCK, and CS pin assignments.
ESP32 default VSPI pins are GPIO 23 (MOSI), 19 (MISO), 18 (SCK), and 5 (CS), but they can be remapped. In your SPI.begin(SCK, MISO, MOSI, CS) call, verify each pin number matches your actual wiring. A single swapped MOSI/MISO will cause no response from the device.
-
Manually control the CS pin.
The chip select pin must be driven LOW before starting a transaction and HIGH after it ends. Many libraries expect you to pass the CS pin to the constructor, but if managing manually, call digitalWrite(CS_PIN, LOW) before SPI.transfer() and HIGH after. A CS pin that stays HIGH means the device is never selected.
-
Reduce the SPI clock frequency.
Start with SPI.setFrequency(1000000) (1 MHz) regardless of what the device can theoretically support. Once communication works at 1 MHz, gradually increase to the device's maximum rated clock. Long wires and breadboard connections require lower clock speeds.
-
Check SPI mode (CPOL and CPHA).
SPI has four modes (0–3) defined by clock polarity and phase. Most common devices use Mode 0 (SPI_MODE0). If your device uses a different mode, specify it: SPI.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE1)). Check the device datasheet for the correct mode.
-
Check logic voltage compatibility.
ESP32 GPIO operates at 3.3V. If your SPI device requires 5V logic (older AVR-based modules, some displays), connect a level shifter between the ESP32 and the device. Connecting 5V SPI signals directly to an ESP32 GPIO can damage the chip.
When to Call a Professional
SPI issues are almost always wiring or configuration. Use a logic analyzer or oscilloscope to verify signals if code and wiring appear correct.