Blog Datasheets Home About me Clients My work Services Contact

G2Labs Grzegorz Grzęda

Introductory Guide to Working with SPI and I2C on ESP32

May 11, 2024

Introductory Guide to Working with SPI and I2C on ESP32

Image Source: Unsplash

If you have been working with ESP32, you must have come across Serial Peripheral Interface (SPI) and Inter-Integrated Circuit (I2C) communication protocols. These two protocols are widely used to interface microcontrollers with various external devices such as sensors, displays, and memory modules. In this introductory guide, we’ll explore how to work with SPI and I2C on ESP32, along with examples and detailed explanations.

SPI Communication Protocol

SPI is a full-duplex, synchronous communication protocol that allows multiple master and slave devices to exchange data over short distances. ESP32 has dedicated hardware SPI controllers, making it ideal for high-speed data transfer.

To begin, you need to include the SPI library:

1
#include <SPI.h>

Initializing SPI

To initialize the SPI interface, use the SPI.begin() function, which sets the default pins and configurations for SPI communication. You can also specify the slave select (SS) pin if you want to define a specific pin for the slave device.

1
2
3
4
void setup() {
  // ...
  SPI.begin();
}

Configuring SPI

The SPI.beginTransaction() function allows you to configure the SPI settings according to your requirements. You can specify parameters such as the clock frequency, data order, and data mode.

1
2
3
4
void setup() {
  // ...
  SPI.beginTransaction(SPISettings(clockSpeed, dataOrder, dataMode));
}

Sending and Receiving Data

To send and receive data over SPI, use the SPI.transfer() function. This function sends a byte of data to the master and returns the received byte from the slave.

1
2
byte sendData = 0xAB;
byte receivedData = SPI.transfer(sendData);

You can also send and receive multiple bytes in one function call:

1
2
3
4
byte sendBuffer[] = {0x01, 0x02, 0x03};
byte receiveBuffer[sizeof(sendBuffer)];

SPI.transfer(sendBuffer, receiveBuffer, sizeof(sendBuffer));

Ending SPI

To release the SPI interface after use, call the SPI.end() function.

1
2
3
4
void loop() {
  // ...
  SPI.end();
}

I2C Communication Protocol

I2C is a popular serial communication protocol used in embedded systems. It allows multiple devices to communicate with each other using only two wires: SDA (Serial Data) and SCL (Serial Clock). ESP32 provides dedicated hardware I2C controllers, making it easy to interface with I2C devices.

To begin, you’ll need to include the Wire library:

1
#include <Wire.h>

Initializing I2C

To initialize the I2C interface, use the Wire.begin() function. By default, ESP32 SDA and SCL pins are GPIO 21 and GPIO 22, respectively.

1
2
3
4
void setup() {
  // ...
  Wire.begin();
}

Sending Data

To send data over I2C, use the Wire.write() function. It takes the data as an argument and sends it to the I2C bus.

1
2
3
4
byte sendData = 0xAB;
Wire.beginTransmission(address);
Wire.write(sendData);
Wire.endTransmission();

Receiving Data

To receive data from an I2C device, use the Wire.requestFrom() function. It takes the slave address and the number of bytes to request as arguments.

1
byte receiveData = Wire.requestFrom(address, numOfBytes);

You can then read the received bytes using the Wire.read() function.

1
byte receivedData = Wire.read();

Ending I2C

To release the I2C interface after use, call the Wire.end() function.

1
2
3
4
void loop() {
  // ...
  Wire.end();
}

Conclusion

In this introductory guide, we covered the basics of working with SPI and I2C communication protocols on ESP32. We learned how to initialize, configure, send, and receive data using these protocols, along with detailed explanations and code examples. With this knowledge, you can now start interfacing your ESP32 with a wide range of SPI and I2C devices. Happy hacking!

Remember to consult the official documentation and device datasheets for further details regarding specific SPI and I2C devices you are working with.

Happy coding!


➡️ Linux 'ls' command


⬅️ Linux 'pwd' command


Go back to Posts.