Language: EN

esp32-spi

How to use the SPI bus on an ESP32

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 mainly 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();
}

The biggest challenge will be with the pins and their designations. However, we have the advantage that in the case of the ESP32, we can reassign the pins.

If we look 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 available for the user.

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

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

In fact, search on 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 take care of defining them manually

SPIClass vspi = SPIClass(VSPI);

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

The constants VSPI and HSPI are integers defined in the Arduino Core. They are different for each variant of the ESP32, 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 SPI bus on the ESP32 in the Arduino environment is very similar to doing it on a conventional Arduino. Here are some examples.

Sending Data

byte dataToSend = 0xAA;
byte receivedData;

digitalWrite(SS, LOW);  // Lower CS/SS line to select the device
receivedData = SPI.transfer(dataToSend);  // Send and receive data
digitalWrite(SS, HIGH); // Raise CS/SS line to end communication

Using multiple SPI on the ESP32

As we mentioned, the ESP32, ESP32-S2, and ESP32-S3 have two I2C controllers, so we can create two simultaneous device networks.

#include <SPI.h>

#define HSPI 2  // 2 for S2 and S3, 1 for S1
#define VSPI 3

// Replace with the pins you want to use
const int HSPI_MISO = 0;
const int HSPI_MOSI = 0;
const int HSPI_SCLK = 0;
const int HSPI_SS = 0;

const int VSPI_MISO = 0;
const int VSPI_MOSI = 0;
const int VSPI_SCLK = 0;
const int VSPI_SS = 0;

SPIClass vspi = SPIClass(VSPI);
SPIClass hspi = SPIClass(HSPI);

void setup()
{ 
  vspi.begin(VSPI_SCLK, VSPI_MISO, VSPI_MOSI, VSPI_SS);
  hspi.begin(HSPI_SCLK, HSPI_MISO, HSPI_MOSI, HSPI_SS);
}

void loop()
{
  delay(1000);  // do nothing
}