Spi communication not working

I would like to send data via SPI communication from nano to Teensy 4.1 but i receive only zeros. Thanks for any suggestions.
Code for analysion.
Teensy 4.1:

#include <SPI.h>

const int slaveSelectPin = 10; // Set your SS pin

void setup() {
  Serial.begin(9600); // Initialize serial communication
  pinMode(slaveSelectPin, INPUT_PULLUP); // Set SS pin as input with pull-up resistor
  // Set up SPI communication
  SPI.begin();
}

void loop() {
  // Check if SS pin is low (indicating data is available)
  if (digitalRead(slaveSelectPin) == HIGH) {
    // Read data from SPI
    byte data = SPI.transfer(2);
    // Print received data to serial monitor
    Serial.println(data);
    
    // Wait for SS pin to go high again (indicating end of transmission)
    
  }
}
Arduino Nano:
#include <SPI.h>

void setup() {
  // Initialize SPI communication
  SPI.begin();
  // Set Slave Select pin as OUTPUT
  pinMode(SS, OUTPUT);
  // Select the Slave
  digitalWrite(SS, LOW);
}

void loop() {
  // Send 1 via SPI
  digitalWrite(SS, HIGH);
  SPI.transfer(5);
  delay(1000);
  
}

One byte is ordered to receive 2 charaters? Try using word or unsigned inte.

Transferring 5 byte havin what value?

The code snippets looks like not complete.

well me and my mate we were doing it in a rush any suggestion for a code I have no idea how it should look like, also i read i guess here: SPI Arduino Library, connecting SPI devices to Teensy that teensy cant be a slave so you need some library but even after that it didnt work

also i thought that the 5 in the code is the number i would send via SPI

Okey.
Explore the IDE examples and see what You find! Sending one byte, value 5, looks strange to me.

well i found code to do it with arduino to arduino but when the teensy is the slave and i use this code :

 #include <SPI.h>
char buff [50];
volatile byte indx;
volatile boolean process;

void setup (void) {
   Serial.begin (115200);
   pinMode(MISO, OUTPUT); // have to send on master in so it set as output
   SPCR |= _BV(SPE); // turn on SPI in slave mode
   indx = 0; // buffer empty
   process = false;
   SPI.attachInterrupt(); // turn on interrupt
}

ISR (SPI_STC_vect) // SPI interrupt routine 
{ 
   byte c = SPDR; // read byte from SPI Data Register
   if (indx < sizeof buff) {
      buff [indx++] = c; // save data in the next index in the array buff
      if (c == '\r') //check for the end of the word
      process = true;
   }
}

void loop (void) {
   if (process) {
      process = false; //reset the process
      Serial.println (buff); //print the array on serial monitor
      indx= 0; //reset button to zero
   }
}

i get this error: error: expected constructor, destructor, or type conversion before '(' token
15 | ISR (SPI_STC_vect) // SPI interrupt routine

Here's the error line. Check arduino/reference for how to activate interrupts.

Your Nano code is incorrect. The slave SPI device is selected when it's SlaveSelect goes low. You need to exit setup with SS high.

In your loop, you should set SS low, transfer your data and set SS high again.

well but the problem was with the teensy code because of this line: ISR (SPI_STC_vect)

i guess i tried that as well but with no change

i tried but as well no result

I've not used a Teensy but I know they are fast micros. I would try sending just 1 byte and see what happens. With slower micros like an 328P based UNO, it's easy to overwhelm the receiving SPI device with bytes. Even with a teensy, you may have to put short delays in between each byte you send.

However, the Teensy my handle SPI differently and this may not be an issue. Hopefully a Teensy user will clarify this.

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