### What is SPI?
SPI (Serial Peripheral Interface) is a fast way for microcontrollers like the ESP32-S3 to talk to sensors, displays, or other chips. It's like a quick chat: one "master" (usually the ESP32) controls the conversation with "slaves" (devices).
### Key Pins on ESP32-S3
- **SCK**: Clock signal (syncs data).
- **MOSI**: Data from master to slave.
- **MISO**: Data from slave to master.
- **CS/SS**: Chip Select (picks which slave to talk to).
ESP32-S3 can use almost any GPIO pins for SPI (flexible!).
# Default SPI Pins
We can easily printout the default SPI pins using in Arduino IDE by using the following codes:
``` cpp
void setup() {
Serial.begin(115200);
}
void loop() {
Serial.printf(" SCK: %d\n", SCK);
Serial.printf(" MOSI: %d\n", MOSI);
Serial.printf(" MISO: %d\n", MISO);
Serial.printf(" SS: %d\n", SS);
delay(3000);
}
```

# Reroute SPI Pins
The ESP32-S3 supports using any available GPIO pins for SPI communication through its flexible GPIO matrix. The ESP32 has two general-purpose SPI buses: VSPI (default) and HSPI. You can initialize either (or both) with your chosen GPIO numbers for SCK (clock), MISO, MOSI, and SS/CS (chip select).
Here's how:
nclude the SPI library:
``` cpp
#include <SPI.h>
```
Define your custom pins (replace with your desired GPIOs; ensure they're not conflicting with other uses like UART or flash):
``` cpp
#define SCK_PIN 18 // Clock
#define MISO_PIN 19 // Master In Slave Out
#define MOSI_PIN 23 // Master Out Slave In
#define SS_PIN 5 // Chip Select (optional for single device)
```
Initialize SPI in setup():
``` cpp
void setup() {
SPI.begin(SCK_PIN, MISO_PIN, MOSI_PIN, SS_PIN);
// Now use SPI.transfer() or library functions for communication
}
```
- If SS_PIN is not needed (e.g., for a single device), you can omit it or set it to -1.
- For multiple buses or devices, create a separate SPIClass instance:
``` cpp
SPIClass mySPI(VSPI); // Or HSPI for the second bus
mySPI.begin(SCK_PIN, MISO_PIN, MOSI_PIN, SS_PIN);
```
Example full sketch for testing:
``` cpp
#include <SPI.h>
#define SCK_PIN 18
#define MISO_PIN 19
#define MOSI_PIN 23
#define SS_PIN 5
void setup() {
Serial.begin(115200);
SPI.begin(SCK_PIN, MISO_PIN, MOSI_PIN, SS_PIN);
Serial.println("SPI initialized with custom pins!");
}
void loop() {
// Your SPI communication code here
}
```
## Adding Multiple Sensors to One SPI Bus

You can connect multiple sensors to the **same** SCK, MOSI, and MISO pins (shared bus). This saves pins on your ESP32-S3!
- **Why each sensor needs its own CS/SS pin?** The CS pin acts like a "switch" to select which sensor the ESP32 is talking to. Without separate CS pins, all sensors would "hear" the same commands and data, causing confusion or errors (like multiple devices responding at once).
- **How it works:**
1. Wire SCK, MOSI, MISO to **all** sensors in parallel.
2. Give each sensor a unique CS pin from the ESP32 (e.g., GPIO5 for Sensor1, GPIO4 for Sensor2).
3. In code (e.g., Arduino): The ESP32 (master) sets a sensor's CS pin **LOW** (active) to "wake it up" and communicate. Other CS pins stay **HIGH** (inactive), so those sensors ignore the bus. Example in Arduino:
``` cpp
digitalWrite(CS1, LOW); // Select Sensor1
SPI.transfer(data); // Send/receive data
digitalWrite(CS1, HIGH); // Deselect
```
This way, you talk to one at a time without conflicts.
- Choosing a selection results in a full page refresh.