I was trying to make NRF24 and ADXL345 work on the same SPI bus. Each device worked ok individually, but not together, when used by the same program. After some investigation, using a logic analyzer (listening to the SPI pins and to the CS pins of both devices), I managed to solve the problem. What I found was:
NRF24 (TMRH20 library) sets SPI settings before each SPI transaction it makes. It uses SPI_MODE0, MSBFIRST, SPI speed up to 10 MHz (not 100% sure about the max speed).
ADXL (SparkFun library) does not change existing SPI settings before its transactions. It seems to do it only once when creating an instance of the device ADXL345 adxl = ADXL345(ADXL_ChipSelect_PIN); Therefore, the first NRF24 transaction modifies SPI settings, disabling the ADXL345. ADXL345 uses SPI_MODE3. MSBFIRST. SPI speed up to 5 MHz, according to the specs.
So, here is how I made them work together: In the code, before each ADXL transaction I insert: SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE3)); and SPI.endTransaction(); at the end of each transaction. The detailed description can be found here. I didn’t have to use SPI settings before NRF24 SPI transactions because, as already mentioned above, the RF24 library does it.
Here is an exemplary code, which seems to work fine.
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <SparkFun_ADXL345.h>
ADXL345 adxl = ADXL345(10);
RF24 radio(8, 9);
const byte address[6] = "00001";
void setup() {
SPI.begin();
radio.begin();
radio.openWritingPipe(address);
radio.setPALevel(RF24_PA_MIN);
radio.stopListening();
SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE3)); //SPI seetings for ADXL
adxl.powerOn();
adxl.setRangeSetting(2);
adxl.setRate(3200);
adxl.setSpiBit(0);
SPI.endTransaction(); //SPI seetings for ADXL
}
void loop() {
int x,y,z;
SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE3));//SPI seetings for ADXL
adxl.readAccel(&x, &y, &z);
SPI.endTransaction();//SPI seetings for ADXL
int val = x;
radio.write(&val, sizeof(val));
delay(500);
}
Here is the logic analyzer view of the working SPI communication of both devices. One can see SPI modes changing before each transaction.
And I didn’t delete the 3 lines, mentioned by Oitzu in the beginning of this post. If I did, NRF24 would not be able to set SPI the way it needs (namely MODE0) before each transaction. I guess, the devices of Oitzu were using the same mode, therefore deleting the SPI settings didn’t harm in that particular case. Please correct me if I’m wrong.
I hope it helps.