nrf24l01 radio and vS1053 mp3 both use SPI at same time? not working ...

I successfully got two Nrf24l01 radio transmitters working. The transmitter is on an Arduino Mega and the receiver is on and Arduino Uno.

Then I got Adafruit's Music Maker Shield working on the Mega.

Now I want both to work at the same time.

In the sketch below, if I comment out the MP3 code then the radio works fine. If I leave the MP3 code in then nothing work.

I know that SPI can be used by multiple devices and I have been trying but with no luck. Adafruit says to make sure each device has a different CS pin. They do, the NRF24l01 has the CSN pin on 53 of the Mega and the other CS pins are 7, 6 and 4.

I do not know what to try next.

Thank you in advance.

#include <SPI.h>
#include "RF24.h"
#include <Adafruit_VS1053.h>
#include <SD.h>

// These are the pins used for the music maker shield
#define SHIELD_RESET  -1      // VS1053 reset pin (unused!)
#define SHIELD_CS     7      // VS1053 chip select pin (output)
#define SHIELD_DCS    6      // VS1053 Data/command select pin (output)

// These are common pins between breakout and shield
#define CARDCS 4     // Card chip select pin
// DREQ should be an Int pin, see http://arduino.cc/en/Reference/attachInterrupt
#define DREQ 3       // VS1053 Data request, ideally an Interrupt pin

Adafruit_VS1053_FilePlayer musicPlayer =
  // create shield-example object!
  Adafruit_VS1053_FilePlayer(SHIELD_RESET, SHIELD_CS, SHIELD_DCS, DREQ, CARDCS);

/***      Set this radio as radio number 0 or 1         ***/
bool radioNumber = 1;

/* Hardware configuration: Set up nRF24L01 radio (CE, CSN)on SPI bus plus pins */
RF24 radio(9, 53);

// Used to control whether this node is sending or receiving
bool role = 1;

byte addresses[][6] = {"1Node", "2Node"};

byte ledState[] = {0, 0, 0, 0};

unsigned long currentMillis = 1;


void setup() {
  delay(2000); //wait for radio 3.3v regulator to power up before sending radio commands
  Serial.begin(9600);
  if (role == 0) {
    pinMode(2, OUTPUT); //pinModes for recieving board
    pinMode(3, OUTPUT);
    pinMode(4, OUTPUT);
    pinMode(5, OUTPUT);
  }

  if (role == 1) {
    pinMode(8, OUTPUT);
    pinMode(14, OUTPUT);
    pinMode(15, OUTPUT);
    pinMode(16, OUTPUT);

    // initialise the music player
    if (! musicPlayer.begin()) { // initialise the music player
      Serial.println(F("Couldn't find VS1053, do you have the right pins defined?"));
      while (1);
    }
    Serial.println(F("VS1053 found"));

    if (!SD.begin(CARDCS)) {
      Serial.println(F("SD failed, or not present"));
      while (1);  // don't do anything more
    }
    Serial.println("SD OK!");
    // Set volume for left, right channels. lower numbers == louder volume!
    musicPlayer.setVolume(20, 20);

  } // end role 0 setup
  //radio.begin();

  // Set the PA Level low to prevent power supply related issues since this is a
  // getting_started sketch, and the likelihood of close proximity of the devices. RF24_PA_MAX is default.
  radio.setPALevel(RF24_PA_LOW);

  // Open a writing and reading pipe on each radio, with opposite addresses
  if (radioNumber) {
    radio.openWritingPipe(addresses[1]);
    radio.openReadingPipe(1, addresses[0]);
  } else {
    radio.openWritingPipe(addresses[0]);
    radio.openReadingPipe(1, addresses[1]);
  }

  // Start the radio listening for data
  radio.startListening();
}

void loop() {
  currentMillis = millis();

  /****************** Ping Out Role ***************************/
  if (role == 1)  {

    if (! musicPlayer.startPlayingFile("/track001.mp3")) {
      Serial.println("Could not open file track001.mp3");
      while (1);
    }
    Serial.println(F("Started playing"));
    radio.stopListening();                                    // First, stop listening so we can talk.
    allOff();
    delay(100);
    allOn();
    delay(100);


  }

  /****************** Pong Back Role ***************************/

  if ( role == 0 )
  {
    if ( radio.available()) {
      // Variable for the received timestamp
      while (radio.available()) {                                   // While there is data ready
        radio.read( &ledState, sizeof(ledState) );

      }// Get the payload
    }

    if (ledState[0] == 1) {
      digitalWrite(2, HIGH);
    }
    if (ledState[0] == 0) {
      digitalWrite(2, LOW);
    }
    if (ledState[1] == 1) {
      digitalWrite(3, HIGH);
    }
    if (ledState[1] == 0) {
      digitalWrite(3, LOW);
    }
    if (ledState[2] == 1) {
      digitalWrite(4, HIGH);
    }
    if (ledState[2] == 0) {
      digitalWrite(4, LOW);
    }
    if (ledState[3] == 1) {
      digitalWrite(5, HIGH);
    }
    if (ledState[3] == 0) {
      digitalWrite(5, LOW);
    }
  }

  /****************** Change Roles via Serial Commands ***************************/

  if ( Serial.available() )
  {
    char c = toupper(Serial.read());
    if ( c == 'T' && role == 0 ) {
      Serial.println(F("*** CHANGING TO TRANSMIT ROLE -- PRESS 'R' TO SWITCH BACK"));
      role = 1;                  // Become the primary transmitter (ping out)

    } else if ( c == 'R' && role == 1 ) {
      Serial.println(F("*** CHANGING TO RECEIVE ROLE -- PRESS 'T' TO SWITCH BACK"));
      role = 0;                // Become the primary receiver (pong back)
      radio.startListening();

    }
  }
}

void allOff() {
  for (int i = 0; i < 13; i++) {
    toggleLights(i, 0);
  }
}

void allOn() {
  for (int i = 0; i < 13; i++) {
    toggleLights(i, 1);
  }
}

//////////// light switch Cases ////////
void toggleLights(byte lightNo, byte pinState)
{
  switch (lightNo)
  {
    case 1:  ledState[0] = pinState;
      radio.write( &ledState, sizeof(ledState) );
      break;//
    case 2: ledState[1] = pinState;
      radio.write( &ledState, sizeof(ledState) );
      break;//
    case 3: ledState[2] = pinState;
      radio.write( &ledState, sizeof(ledState) );
      break;//
    case 4: ledState[3] = pinState;
      radio.write( &ledState, sizeof(ledState) );
      break;//
    case 5: digitalWrite(4, pinState);    break;// 19-T+
    case 6: digitalWrite(5, pinState); break;// 16-T +
    case 7: digitalWrite(6, pinState); break;// 47-T +
    case 8: digitalWrite(7, pinState); break;// 43-T +
    case 9: digitalWrite(8, pinState); break;// 43-T +
    case 10: digitalWrite(14, pinState); break;// 43-T +
    case 11: digitalWrite(15, pinState); break;// 43-T +
    case 12: digitalWrite(16, pinState); break;// 43-T +
  }
}

Maybe it's a problem with how the library makes use of the CS pin. Note that to use a module you will have to activate it via CS. So to switch between them alternately, you will need to use some management, perhaps editing the library, perhaps using RTOS.

you can have as many SPI devices as you have select pins to run, but only one can be used at a time. many libraries will toggle the select pin on only when it's actually needed, some don't and you either have to do that manually or modify the library.

Source: nRF24L01, UNO, and music maker shield won't talk to each other...

rtek1000:
Source: nRF24L01, UNO, and music maker shield won't talk to each other...

Thank you for your reply.

I read that Reddit thread before posting and tried to separate the CS pins. All the CS pins of the Adafruit MP3 player are different and the CSN pin on the radio transmitter is also different.

I don't now how to make the libraries talk to each other so that they do not conflict.