<?xml version="1.0" encoding="UTF-8"?>        <rss version="2.0"
             xmlns:atom="http://www.w3.org/2005/Atom"
             xmlns:dc="http://purl.org/dc/elements/1.1/"
             xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
             xmlns:admin="http://webns.net/mvcb/"
             xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
             xmlns:content="http://purl.org/rss/1.0/modules/content/">
        <channel>
            <title>
									SPI problem with 2 ESP32 - ESP32 &amp; ESP8266				            </title>
            <link>https://forum.dronebotworkshop.com/esp32-esp8266/spi-problem-with-2-esp32/</link>
            <description>Discussion board for Robotics, Arduino, Raspberry Pi and other DIY electronics and modules. Join us today!</description>
            <language>en-US</language>
            <lastBuildDate>Sun, 12 Apr 2026 19:18:36 +0000</lastBuildDate>
            <generator>wpForo</generator>
            <ttl>60</ttl>
							                    <item>
                        <title>RE: SPI problem with 2 ESP32</title>
                        <link>https://forum.dronebotworkshop.com/esp32-esp8266/spi-problem-with-2-esp32/#post-34130</link>
                        <pubDate>Sat, 24 Sep 2022 13:31:21 +0000</pubDate>
                        <description><![CDATA[@fedimakni  Start with one of Bill&#039;s sketches as in
Once that is working, make the few changes needed to get it running on a pair of ESP32&#039;s. Once that is running, now carefully and slowly m...]]></description>
                        <content:encoded><![CDATA[<p>@<span>fedimakni</span>  Start with one of Bill's sketches as in <a href="https://dronebotworkshop.com/i2c-arduino-arduino/" target="_blank" rel="noopener">https://dronebotworkshop.com/i2c-arduino-arduino/</a></p>
<p>Once that is working, make the few changes needed to get it running on a pair of ESP32's. Once that is running, now carefully and slowly modify (Leave old code in, just // comment out) the code to do what you want.</p>]]></content:encoded>
						                            <category domain="https://forum.dronebotworkshop.com/esp32-esp8266/">ESP32 &amp; ESP8266</category>                        <dc:creator>Ron</dc:creator>
                        <guid isPermaLink="true">https://forum.dronebotworkshop.com/esp32-esp8266/spi-problem-with-2-esp32/#post-34130</guid>
                    </item>
				                    <item>
                        <title>SPI problem with 2 ESP32</title>
                        <link>https://forum.dronebotworkshop.com/esp32-esp8266/spi-problem-with-2-esp32/#post-34124</link>
                        <pubDate>Sat, 24 Sep 2022 03:38:17 +0000</pubDate>
                        <description><![CDATA[Hello,
I am currently trying to implement an SPI communication between 2 ESP32 using arduino IDE but i am facing a problem.
I set 1 as a master using the normal SPI library and the second ...]]></description>
                        <content:encoded><![CDATA[<p>Hello,</p>
<p>I am currently trying to implement an SPI communication between 2 ESP32 using arduino IDE but i am facing a problem.</p>
<p>I set 1 as a master using the normal SPI library and the second one as slave using ESP32SPISlave.</p>
<p>Master SW is basically writing 0 or 1 to slave device and wait for a response</p>
<p>if the master send a 0 the slave will send back 2 in the next communication, </p>
<p>if the master send 1 the slave will send back 1 in the next communication.</p>
<p>the problem that sometimes even if the master send 1 i receive the number 2 instead of 1 so it seems that it doesn't override the previous value.</p>
<p>I am not sure exactly what's the problem. could you please support me?</p>
<p>I will paste both SW</p>
<p>You can find the picture of the issue in the link below</p>
<p>https://drive.google.com/file/d/1K08ntvOcR7fH9SyWb-RZu7XXCOKP2msH/view?usp=sharing</p>
<p>Master SW</p>
<pre contenteditable="false">#include &lt;SPI.h&gt;

// Define ALTERNATE_PINS to use non-standard GPIO pins for SPI bus

#ifdef ALTERNATE_PINS
  #define VSPI_MISO   26
  #define VSPI_MOSI   27
  #define VSPI_SCLK   25
  #define VSPI_SS     32
#else
  #define VSPI_MISO   MISO
  #define VSPI_MOSI   MOSI
  #define VSPI_SCLK   SCK
  #define VSPI_SS     SS
#endif

static const int spiClk = 1000000; // 1 MHz

int data1;

//uninitalised pointers to SPI objects
SPIClass * vspi = NULL;

void setup() {
  Serial.begin(115200);
    delay(2000);
  //initialise instance of the SPIClass attached to HSPI
  vspi = new SPIClass(VSPI);
  
  //clock miso mosi ss
#ifndef ALTERNATE_PINS
  //initialise hspi with default pins
  //SCLK = 14, MISO = 12, MOSI = 13, SS = 15
  vspi-&gt;begin();
#else
  //alternatively route through GPIO pins
  vspi-&gt;begin(VSPI_SCLK, VSPI_MISO, VSPI_MOSI, VSPI_SS); //SCLK, MISO, MOSI, SS
#endif

  //set up slave select pins as outputs as the Arduino API
  //doesn't handle automatically pulling SS low
  pinMode(VSPI_SS, OUTPUT); //HSPI SS
}

// the loop function runs over and over again until power down or reset
void loop() {
  vspi_send_command();
  delay(1000);
}

void vspi_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
  
  vspi-&gt;beginTransaction(SPISettings(spiClk, MSBFIRST, SPI_MODE0));
  digitalWrite(VSPI_SS, LOW);
  data1 = vspi-&gt;transfer(data_on);
  digitalWrite(VSPI_SS, HIGH);
  vspi-&gt;endTransaction();
  Serial.print("data sent is 1 and data received is ");
  Serial.println(data1);
  delay(1000);
  
  vspi-&gt;beginTransaction(SPISettings(spiClk, MSBFIRST, SPI_MODE0));
  digitalWrite(VSPI_SS, LOW);
  data1 = vspi-&gt;transfer(data_off);
  digitalWrite(VSPI_SS, HIGH);
  vspi-&gt;endTransaction();
  Serial.print("data sent is 0 and data received is ");
  Serial.println(data1);
  delay(1000);

}</pre>
<p> </p>
<p>Slave SW</p>
<pre contenteditable="false">#include &lt;ESP32SPISlave.h&gt;

ESP32SPISlave slave;

static constexpr uint32_t BUFFER_SIZE {32};
uint8_t spi_slave_tx_buf;
uint8_t spi_slave_rx_buf;

#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 -&gt; default
    // VSPI = CS:  5, CLK: 18, MOSI: 23, MISO: 19
    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
    slave.wait(spi_slave_rx_buf, spi_slave_tx_buf, BUFFER_SIZE);

    // 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);
         data = spi_slave_rx_buf;
         slave.pop();
    }
    if(data == 1 )
    {
        Serial.println("Setting LED active HIGH ");
        digitalWrite(LED, HIGH);
        memset(spi_slave_tx_buf, 1, BUFFER_SIZE);
    }
    else if(data == 0 )
    {
        Serial.println("Setting LED active LOW ");
        digitalWrite(LED, LOW);
        memset(spi_slave_tx_buf, 2, BUFFER_SIZE);
    }
     Serial.println("");
}</pre>
<p>Thank you again for your cooperation.</p>
<p> </p>]]></content:encoded>
						                            <category domain="https://forum.dronebotworkshop.com/esp32-esp8266/">ESP32 &amp; ESP8266</category>                        <dc:creator>fedimakni</dc:creator>
                        <guid isPermaLink="true">https://forum.dronebotworkshop.com/esp32-esp8266/spi-problem-with-2-esp32/#post-34124</guid>
                    </item>
							        </channel>
        </rss>
		