esp32-spi

How to use the SPI bus on an ESP32

  • 4 min

The SPI Bus is a synchronous communication protocol used to transfer data between a microcontroller and other devices, such as sensors, displays, memories, and peripherals.

Unlike other protocols, the SPI bus uses multiple communication lines, allowing for high transfer speeds and exceptional flexibility.

The SPI port is primarily used for its high data transmission capacity. For this reason, you will find it in devices that require a large amount of data per second, such as storage devices (memories, SD cards) and displays.

The SPI Bus on the ESP32

The ESP32, ESP32-S2, and ESP32-S3 have four SPI controllers, while the ESP32-C3 has three.

Its use in the Arduino environment is very similar to what we would find in a conventional Arduino. The functionalities are defined in the SPI.h library, under the SPIClass object.

void setup() {
  // Initialize SPI communication
  SPI.begin();
}
Copied!

The biggest confusion we’ll have is with the pins and their names. But, we have the advantage that in the case of the ESP32, we can reassign the pins.

Looking at the ESP32, ESP32-S2, and ESP32-S3, they have four controllers, called SPI0, SPI1, SPI2, and SPI3.

  • SPI0 and SPI1 are used for communication with the Flash memory. So they are not to be touched.
  • SPI2 and SPI3 are left free for the user.

This leaves two SPI controllers free for us to use (one in the ESP32-C3).

On the other hand, SPI2 is called HSPI and SPI3 is called VSPI. What do H and V mean? Absolutely nothing, they are identical in features.

In fact, search the internet, there are funny questions and answers about what H and V mean (I like the one about Henry and Victor).

In the case of the ESP32

SPIMOSIMISOSCKCS
HSPIGPIO 13GPIO 12GPIO 14GPIO 15
VSPIGPIO 23GPIO 19GPIO 18GPIO 5

In the case of the ESP32-S3

SPIMOSIMISOSCKCS
HSPIGPIO 35GPIO 37GPIO 37GPIO -
VSPIGPIO 11GPIO 13GPIO 12GPIO 10

But the same, I would make sure to define them manually

SPIClass vspi = SPIClass(VSPI);

MySPI.begin(VSPI_SCLK, VSPI_MISO, VSPI_MOSI, VSPI_SS);
Copied!

The constants VSPI and HSPI are integers defined in the Arduino Core. They are different for each ESP32 variant, so use the constant instead of a number.

ModelHSPIVSPI
ESP3213
ESP32-S223
ESP32-S323
ESP32-C313

How to Use SPI on ESP32 in the Arduino Environment

Using the I2C bus on the ESP32 in the Arduino environment is very similar to doing it on a conventional Arduino. Here are some examples.