2

I'm trying to send out data from SPI, but can't get it to work. There appear no data on the SPI ports (D12, 13, 14; checked with an oscilloscope) and the ESP32 seems to hang. I would like to use the HSPI port.

I am also wondering whether I need a special driver for SPI to work on ESP32 and if so, how can I check if I already have that and how do I install it. When I look in the library manager, I see no special SPI driver.

I have tried using this program (copied from https://diyi0t.com/spi-tutorial-for-arduino-and-esp8266/). It's apparently intended for esp8266. Should it work out of the box also for ESP32?

#include "SPI.h"
char buff[]="Hello Slave\n";

void setup() {
 SPI.begin();
}

void loop() {
 for(int i=0; i<sizeof buff; i++)
 {
  SPI.transfer(buff[i]);
 }
 delay(1000);  
}

and also with this program:

#include "SPI.h"
char buff[]="Hello Slave\n";
SPIClass SPI1(HSPI);

void setup() {
 SPI1.begin();
 SPI1.setClockDivider(80);
}

void loop() {
 for(int i=0; i<sizeof buff; i++)
 {
  SPI1.transfer(buff[i]);
 }
 delay(1000);  
}

I am using a 30 pin ESP32 dev board, Arduino version 1.8.13. In preferences-->more board managers, it says:

http://arduino.esp8266.com/stable/package_esp8266com_index.json, https://dl.espressif.com/dl/package_esp32_index.json
3
  • ESP8266 and ESP32 are not compatible. Make absolutely sure you have the ESP32 version of Arduino libraries (it looks like you might have the ESP8266 libraries), find ESP32 samples of SPI communication. Commented Mar 21, 2021 at 10:03
  • use your pinout to see which are the MOSI/MISO/SCK lines on your board and choose your own CS, making sure to set it LOW/HIGH before/after transferring/reading data. Commented Mar 22, 2021 at 8:19
  • It looks like Tarmo is correct that I'm using the wrong libraries. I replaced the ESP32 hardware with an ESP8266 and got it working in no time. Commented Mar 22, 2021 at 15:21

4 Answers 4

3

For ESP32, you need to declare which SPI instance you want to use, like so:

#include <SPI.h>

SPIClass SPI1(HSPI);

SPI1.begin();
// Optional
// SPI1.beginTransaction(SPISettings(3000000, MSBFIRST, SPI_MODE2));

The rest is the same as ESP8266

Sign up to request clarification or add additional context in comments.

Comments

1
#include <SPI.h>

#define HSPI_MISO 12
#define HSPI_MOSI 13
#define HSPI_SCLK 14
#define HSPI_CS   15
static const int spiClk = 240000000; // 1 MHz
SPIClass * hspi = NULL;

char buff[]="Hello Slave\n";

//byte buff[] = {0xAA, 0xBB, 0xAA, 0x01,
                  0x89, 0xAB, 0xCD, 0xEF};



void setup() {
Serial.begin(9600);
hspi = new SPIClass(HSPI);
hspi->begin();
hspi->begin(HSPI_SCLK, HSPI_MISO, HSPI_MOSI, HSPI_CS); //SCLK, MISO, MOSI, SS
pinMode(HSPI_CS, OUTPUT); //HSPI SS
}

void loop() {
 for(int i=0; i<sizeof buff; i++)
 {
  SPI.transfer(buff[i]);
  Serial.println(buff[i]);

 }
 delay(1000);  
}

2 Comments

A good answer will always include an explanation why this would solve the issue, so that the OP and any future readers can learn from it.
You declare spiClk but don't use it
0

Assuming that you use the ESP32 Arduino Core, under the docs it is written that SPI is has a suppported Arduino API implementation. Thus using the Arduino SPI API, it should work, like all other devices (the ESP32 Arduino Core implementation conforms to the API defined by Arduino, of course I would check if your board's pinout corresponds to the Espressif defined ESP32 pinout).

If you want to see some examples, you can find one here. All supported APIs have examples on the ESP32 Arduino Core repo on GitHub.

I also want to point out that in the Arduino IDE (or the VSCode plugin) you can find examples for SPI, I would take a look as well for that.

Comments

0

@rebel_russia 's answer is good however is not complete. To do SPI remapping one needs to create a new SPIClass pointer:

SPIClass* hspi = new SPIClass(HSPI); 

and pass it to the constructor (or use it instead of the default SPI class). Here's @rebel_russia answer corrected:

#include <SPI.h>

#define HSPI_MISO 12
#define HSPI_MOSI 13
#define HSPI_SCLK 14
#define HSPI_CS   15
static const int spiClk = 240000000; // 1 MHz
SPIClass * hspi = NULL;

char buff[]="Hello Slave\n";

//byte buff[] = {0xAA, 0xBB, 0xAA, 0x01,
                  0x89, 0xAB, 0xCD, 0xEF};



void setup() {
Serial.begin(9600);
hspi = new SPIClass(HSPI);
hspi->begin(HSPI_SCLK, HSPI_MISO, HSPI_MOSI, HSPI_CS); //SCLK, MISO, MOSI, SS
pinMode(HSPI_CS, OUTPUT); //HSPI SS
}

void loop() {
 for(int i=0; i<sizeof buff; i++)
 {
  hspi->transfer(buff[i]);
  Serial.println(buff[i]);

 }
 delay(1000);  
}

4 Comments

Tomas Would you have any comments on how to go about using esp32 spi with arduino ethernet library2.x? I want to use custom spi pins.
Great question. However, you'll need to do your own literature review on this
@Tomas great response! my question was after days of reviews and trials and fail. But still, great response :)
well, it works for me on VS Code and Arduino Studio 2.0.1 ....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.