Problem with multiple SPI devices on Arduino NANO and slave select not working properly

For a project, I'm trying to use two Arduino Nano to comunicate data from one to the other with two nRF24L01 modules. The Transmitter Arduino Nano also has a microSD adapter to save the data as it sends it as a failsafe. I managed to get the two Arduino to send and receive data but when I tried connecting the SD adapter module the Receiver Nano received corrupted data(0.00, nan...) and they sometimes didn't communicate at all. I also got the Sd card working by itself but even when I try using SS pins(CSN & CS) it still doesn't work if both modules are attached to SPI pins. The problem seems to be SPI pins(MOSI, MISO...) which are connected in parallel to the two devices interfere and I'm unable to select SS pins to manage which module should be active. These are the TX and RX sketches respectively, is there a way to solve this?

Transmitter sketch:

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <SD.h>

RF24 radio(8, 9); // pin di attivazione e pin di ricezione
const uint64_t pipe = 0xE8E8F0F0E1LL; // indirizzo del pipe

File file;    //declare file

void seleziona (int a){    //function that lets us enable or disable ss pins
  if(a==1){            //SD adapter CS pin=5 
    digitalWrite(5, LOW); //activate SD
    digitalWrite(9, HIGH);   //deactivate NRF24L01
    Serial.println("1");
  }
  else if(a==2){     //NRF24L01 CSN pin=9 
    digitalWrite(9, LOW); //activate NRF24L01
    digitalWrite(5, HIGH);   //deactivate SD
    Serial.println("2");
  }
}

void setup() {
  
  seleziona(2);  //activate NRF24L01 ss pin
  Serial.begin(9600);
  radio.begin();
  radio.openWritingPipe(pipe);

  seleziona(1); //activate SD ss pin
  if (!SD.begin(5)) {
    Serial.println("Error");   //print error message 
    while (1);
  }
  Serial.println("SD initialized");   
  if(SD.exists("data.txt")){   //reset SD data
    SD.remove("data.txt");
  }

  delay(100);
}

void loop() {
  seleziona(2);  //activate NRF24L01 ss pin  
  double numero = 3.1415; //placeholder data
  radio.write(&numero, sizeof(numero)); // transmit data
  Serial.println(numero); // print transmitted data

  seleziona(1);   //activate SD ss pin
  file = SD.open("dati.txt", FILE_WRITE);   //open SD file
  file.println(numero);    //write data on SD
  file.close();
  delay(100); //wait 100 ms
}

Receiver sketch:

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
 
RF24 radio(9, 10); //initialize NRF24L01
const uint64_t pipe = 0xE8E8F0F0E1LL; //set pipe
 
void setup() {
  Serial.begin(9600);
  radio.begin(); //start the receiver
  radio.openReadingPipe(1, pipe);
  radio.startListening();
}
 
void loop() {
  double numero; //declare data variable
  if (radio.available()) {
    radio.read(&numero, sizeof(numero)); //Read TX signal
    Serial.print("Altezza rilevata: ");
    Serial.print(numero); //Print data on Serial
    Serial.println(" metri");
  }
}

The "Seleziona" function uses digitalWrite() to enable and disable slave select pins as describe before.
TX.ino (1.4 KB)
RX.ino (557 Bytes)

No that is the way that SPI works.

However you have a file open on the SD card. This will prevent any other use of the SPI interface until the file is closed.

I have updated the code to close the file with "file.close()" after the writing process but it still doesn't work.

Hi,
Can we please have a circuit diagrams?
An image of a hand drawn schematic will be fine, include ALL power supplies, component names and pin labels.

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

Can you post a link to the SD card adapter? There is a fairly common design that has an error where the MISO line is not properly released. See this discussion for a bit more detail https://forum.arduino.cc/t/spi-sd-card-reader-other-spi-device-not-working-together/447006 there is a different modification that leaves the buffer in place but connects the enable pin for MISO to the CS line, unfortunately I can't locate the details for that at the moment.

1 Like

SD card adapter link
The third item is the one I am using.

The microSD card adapter with the buffer chip most likely has the design error, and will need to be modified to work correctly with other SPI devices.

So is my adapter faulty? If so is there any way to fix it or would I have to buy another one?

The discussion I linked shows one way to correct the problem, and discusses the other, which is to pull the enable pin for the buffer section that is used for MISO off of the solder pad, and then connect it to the CS line. That will disable the buffer when the SD card is not selected.

Your SD card is powered by 5V. That implies that the board has pull up resistors to 5V.

Where as your wireless module is powered by 3V3, meaning the SPI signals should only ever be 3V3 signals. This voltage mix is stoping the SPI from working properly.

You need all the same voltage signals for SPI to work correctly. So that would imply you need level shifters on the radio module.

1 Like

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