Not recieving data via SPI on ESP-wroom-32 from Teensy 4.1

At the very least I am sending the letter "C" from the Teensy to the ESP-wroom-32.
ESP says its receiving a null. I'm stumped. The reason for the 2 sec delay on the esp32, I was just trying to see if it recognized the pin being flipped from low to high on each side (it does).

Not sure where I'm going wrong with SPI comms.

Teensy code:

#include <Arduino.h>
#include <SPI.h>

const int slaveSelectPin = 40; // CS pin
const int readyPin = 9; // Ready pin

void setup() {
  Serial.begin(115200);
  SPI.begin(); // Initialize SPI
  pinMode(slaveSelectPin, OUTPUT);
  pinMode(readyPin, INPUT); // Set ready pin as input
  digitalWrite(slaveSelectPin, HIGH);

  Serial.println("Teensy 4.1: SPI communication test started");
}

void loop() {
  // Wait until the ready pin is HIGH
  if (digitalRead(readyPin) == HIGH) {
    Serial.println("ESP32 is ready to receive data.");
    
    char data = 'C'; // Data to send to ESP32

    Serial.println("Sending: " + String(data));
    SPI.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE0));
    digitalWrite(slaveSelectPin, LOW); // Select the slave
    SPI.transfer(data); // Send data
    digitalWrite(slaveSelectPin, HIGH); // Deselect the slave
    SPI.endTransaction();
  } else {
    Serial.println("ESP32 is not ready.");
  }

  delay(1000); // Wait for a second before checking again
}

ESP32 code:

#include <Arduino.h>
#include <SPI.h>

const int slaveSelectPin = 5; // CS pin
const int readyPin = 33; // Ready pin
volatile bool dataReceived = false;
volatile char receivedData = 0;

void IRAM_ATTR onSPI() {
  dataReceived = true;
}

void setup() {
  Serial.begin(115200);
  pinMode(slaveSelectPin, INPUT_PULLUP);
  pinMode(readyPin, OUTPUT); // Set ready pin as output
  digitalWrite(readyPin, LOW); // Initially set ready pin to LOW

  SPI.begin(18, 23, 19, slaveSelectPin); // Initialize SPI with SCK, MISO, MOSI, and SS
  attachInterrupt(digitalPinToInterrupt(slaveSelectPin), onSPI, FALLING);

  Serial.println("ESP32: SPI communication test started");
}

void loop() {
  if (dataReceived) {
    SPI.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE0));
    receivedData = SPI.transfer(0x00); // Read data from master
    SPI.endTransaction();

    Serial.print("Received from Teensy: ");
    Serial.println(receivedData, DEC); // Print the received data as a number
    Serial.print("Character: ");
    Serial.println(receivedData); // Print the received data as a character

    dataReceived = false;
    digitalWrite(readyPin, LOW); // Set ready pin to LOW after receiving data
    delay(2000);
  } else {
    digitalWrite(readyPin, HIGH); // Set ready pin to HIGH when ready to receive data
  }
}

With SPI, one device has to be 'M' and the other/s 'S'.
I believe the Teensy and ESP are 'M' (by default).

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.