Go Down

Topic: nRF24 and SD card together on Mega2560 - Issue( Solved) (Read 26 times) previous topic - next topic

Mogaraghu

The code below is a simple one to use a Mega 2560 board with a nRF24 transmitter and SD card.
Pin assignments are as below :

MISO : 50
MOSI : 51
SCK   : 52

CSN of nRF24 : 53
CE of  nRF24 : 9

CS of SD Card : 10

Problem is when the SD card is connected the nRF24 stops working.

When using such multiple peripherals using the same SPI bus , is there any special precaution to be taken ??  

Code: [Select]

// Hardware kit used is Mega2560

#include <SPI.h>
#include <RF24.h>
#include <SdFat.h>
SdFat SD;                                           // Wrap SD members into SDFat
Sd2Card card;

/****************** Radio Config ***************************/
/* Set up nRF24L01 radio on SPI bus plus pins CE and CSN */
RF24 radio(9, 53);                                   // First pin (9) is CE and Next pin (53) is CSN
/**********************************************************/

byte sdCardSelect = 10;

byte radioAddress[][6] = {"1_PRX", "2_PTX"};         // Radio pipe addresses for RemoteUnit, ThisUnit.

struct tx_dataStruct {                               // Configure your transmission data into this structure
  char lcdMessage[20];
  byte cursorRow;
  boolean gfmReset;
} txDataToRemUnit  ;

struct rx_dataStruct {
  byte RFID[4];
  char keyVal;
  long gfmData;
} rxDataFromRemUnit;

bool firstLine, secondLine ;

unsigned long myMillis;

byte copy_RFID[4];
char copy_KeyVal;
long copy_GFMData;

void setup() {

  Serial.begin(9600);
  Serial.println(F("*** STARTING TRANSMITTER *** "));

  checkSDCard();                                              // Check the presence of SD card

  // Setup and configure radio
  radio.begin();
  radio.setChannel(108);                                       // Above most Wifi Channels
  radio.setDataRate( RF24_250KBPS );
  radio.enableAckPayload();                                    // Allow optional ack payloads
  radio.enableDynamicPayloads();                               // Ack payloads are dynamic payloads
  radio.setRetries(5, 10);                                     // Delay, count
}

//******************************************************
void loop(void)
{
  send_LCDInfo();
  delay(500);
}
//******************************************************

void send_LCDInfo()
{

  radio.openWritingPipe(radioAddress[1]);                                 // Writing Pipe to Remote Unit
  delay(5);
  strcpy(txDataToRemUnit.lcdMessage, " Sample Tx data..   ");             // Update the payload
  txDataToRemUnit.cursorRow  = 0;
  firstLine = (radio.write(&txDataToRemUnit, sizeof(txDataToRemUnit))); // Send First Line of LCD..
  Serial.println(firstLine);
  delay(5);
  readAckPayLoad(firstLine);

  myMillis = millis();
  sprintf (txDataToRemUnit.lcdMessage, "TX1 millis = %6ul", myMillis);
  txDataToRemUnit.cursorRow  = 1;
  secondLine = (radio.write(&txDataToRemUnit, sizeof(txDataToRemUnit))); // Send Second Line of LCD..
   Serial.println(secondLine);
  delay(5);
  readAckPayLoad(secondLine);

  if ( firstLine && secondLine)
  {
    Serial.println( "RF Link Healthy !");
  }
  else
  {
    Serial.println( "RF Link dead");
  }
}
//*****************************************************

//FUNCTION TO READ ACK PAYLOAD AND STORE DATA TO LOCAL VARIABLES..

void readAckPayLoad (boolean txResult)
{
  if (txResult)
  {
    while ( radio.available())
    {
      radio.read( &rxDataFromRemUnit, sizeof(rxDataFromRemUnit));
      copy_GFMData = rxDataFromRemUnit.gfmData;                  // Transfer all data from ackPayLoad
      copy_KeyVal = rxDataFromRemUnit.keyVal;
      copy_RFID[0] = rxDataFromRemUnit.RFID[0];
      copy_RFID[1] = rxDataFromRemUnit.RFID[1];
      copy_RFID[2] = rxDataFromRemUnit.RFID[2];
      copy_RFID[3] = rxDataFromRemUnit.RFID[3];
    }
  }
}

//*******************************************************

// FUNCTION TO CHECK PRESENCE OF SD CARD AND ITS TYPE.

void checkSDCard ()
{
  if (SD.begin(sdCardSelect))
  {
    Serial.println( "SD Card started ! ") ;
  }
  else
  {
    Serial.println ( "SD Card Failure !");
  }

  if (!card.init(SPI_HALF_SPEED, sdCardSelect))
  {
    Serial.println("Initialization failed. CHECK !!");
  }
  else
  {
    Serial.println("Wiring is correct and a card is present!!");
  }

}


Mogaraghu

So looks like peopel have suffered and recovered !!

Here we go with the solution to make the SD card adapter co-exist with nRF24 :

http://forum.arduino.cc/index.php?topic=360718.msg2942160#msg2942160

Check out #8. Thanks to the contributor for the this !!

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy