I have two ESP32 cards. I want to set one of these cards as Master and the other as Slave and communicate via SPI. I have done this before on Arduino Uno, but I cannot do this on ESP32.
Below are my Master and Slave codes that I have prepared simply. I can't get what I want in any way. Can anyone help?
**Master Code:**
```
#include <SPI.h>
#define CS_PIN 5 // Choose the appropriate pin for Chip Select (CS)
void setup() {
Serial.begin(115200);
SPI.begin();
}
void loop() {
char dataToSend = 'A';
digitalWrite(CS_PIN, LOW); // Start communication with the slave
SPI.transfer(dataToSend); // Send data to the slave
digitalWrite(CS_PIN, HIGH); // End communication with the slave
delay(2000); // Delay for demonstration purposes
}
```
**Slave Code:**
```
#include <SPI.h>
#define CS_PIN 5 // Choose the appropriate pin for Chip Select (CS)
void setup() {
Serial.begin(115200);
SPI.begin();
}
void loop() {
if (digitalRead(CS_PIN) == LOW) { // Check if CS (Chip Select) is LOW, meaning the device is selected
char receivedData = SPI.transfer(0); // Read received data from the master
// Display received data on the serial monitor
Serial.println("Received Data: " + String(receivedData));
delay(1000); // Optional delay for demonstration purposes
}
}
```
***New Code - New Problem:***
I tried to adapt the codes at https://microcontrollerslab.com/esp32-spi-communication-tutorial-arduino/. From what I understand it uses the following pins for SPI: MISO-12; MOSI-13; SCLK-14; SS-15.
I set my connections the same way, but I cannot get the output I expected from the Slave. It remains waiting at the slave.wait line.
**Master Code:**
```
#include <SPI.h>
// Define ALTERNATE_PINS to use non-standard GPIO pins for SPI bus
#define HSPI_MISO MISO
#define HSPI_MOSI MOSI
#define HSPI_SCLK SCK
#define HSPI_SS SS
static const int spiClk = 1000000; // 1 MHz
//uninitalised pointers to SPI objects
SPIClass * hspi = NULL;
void setup() {
//initialise instance of the SPIClass attached to HSPI
hspi = new SPIClass(HSPI);
//initialise hspi with default pins
//SCLK = 14, MISO = 12, MOSI = 13, SS = 15
hspi->begin();
pinMode(HSPI_SS, OUTPUT); //HSPI SS
}
// the loop function runs over and over again until power down or reset
void loop() {
hspi_send_command();
delay(100);
}
void hspi_send_command() {
byte data_on = 0b00000001; // data 1 to turn on LED of slave
byte data_off = 0b0000000; // data 0 to turn off LED of slave
hspi->beginTransaction(SPISettings(spiClk, MSBFIRST, SPI_MODE0));
digitalWrite(HSPI_SS, LOW);
hspi->transfer(data_on);
digitalWrite(HSPI_SS, HIGH);
hspi->endTransaction();
delay(1000);
hspi->beginTransaction(SPISettings(spiClk, MSBFIRST, SPI_MODE0));
digitalWrite(HSPI_SS, LOW);
hspi->transfer(data_off);
digitalWrite(HSPI_SS, HIGH);
hspi->endTransaction();
delay(1000);
}
*/
```
**Slave Code:**
```
#include <ESP32SPISlave.h>
ESP32SPISlave slave;
static constexpr uint32_t BUFFER_SIZE {32};
uint8_t spi_slave_tx_buf[BUFFER_SIZE];
uint8_t spi_slave_rx_buf[BUFFER_SIZE];
#define LED 2
void setup() {
Serial.begin(115200);
delay(2000);
pinMode(LED, OUTPUT);
// begin() after setting
// HSPI = CS: 15, CLK: 14, MOSI: 13, MISO: 12 -> default
// VSPI = CS: 5, CLK: 18, MOSI: 23, MISO: 190
slave.setDataMode(SPI_MODE0);
slave.begin();
// slave.begin(VSPI); // you can use VSPI like this
// clear buffers
memset(spi_slave_tx_buf, 0, BUFFER_SIZE);
memset(spi_slave_rx_buf, 0, BUFFER_SIZE);
}
void loop() {
// block until the transaction comes from master
Serial.println("Initialize...");
slave.wait(spi_slave_rx_buf, spi_slave_tx_buf, BUFFER_SIZE);
Serial.println("*********************");
// if transaction has completed from master,
// available() returns size of results of transaction,
// and buffer is automatically updated
char data;
while (slave.available()) {
// show received data
Serial.print("Command Received: ");
Serial.println(spi_slave_rx_buf[0]);
data = spi_slave_rx_buf[0];
slave.pop();
}
if(data == 1 )
{
Serial.println("Setting LED active HIGH ");
digitalWrite(LED, HIGH);
}
else if(data == 0 )
{
Serial.println("Setting LED active LOW ");
digitalWrite(LED, LOW);
}
}
```