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)