NRF and SENSOR conflict

Hello, I need some help. I connected NRLF24L01 and Sensor Bme280(SPI) on the SPI, with different CS pins. In separate codes, both devices works fine. However when I want to make them work in the same code - reading sensor value and sending it with nrf - Sensor doesn't work. I think it can be SPI conflict? I found out that after I wrote nrf radio.begin() function, my sensor stoped working - mabye because bme.begin() function can't be initialized?

My code:

#include <SPI.h>
#include <RF24.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>

#define BME_SCK 13
#define BME_MISO 12
#define BME_MOSI 11
#define BME_CS 7

#define SEALEVELPRESSURE_HPA (1013.25)
Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO,  BME_SCK);

// ce, csn pins
RF24 radio(8, 10);


void setup(void) {
Serial.begin(9600);
 
  bme.begin();
  
 radio.begin();
 radio.setPALevel(RF24_PA_MAX);
 radio.setChannel(0x76);
 radio.openWritingPipe(0xF0F0F0F0E1LL);
 radio.enableDynamicPayloads();
 radio.powerUp();
}

void measure() {   //measure
 Serial.print("Temperature = ");
 Serial.print(bme.readTemperature());
 Serial.println(" *C");
}

void sendinfo() {   //send
 const char text[] = "Hello World is awesome";
 radio.write(&text, sizeof(text));
 delay(10);
}

void loop(void) {
 measure();
 sendinfo();
}

I add info about radio.begin() fucntion:

It could be that radio.begin() and bme.begin() are setting some of the SPI parameters differently.

Rather than calling them in setup() try calling each of them (and any other initialization code) just before you use the radio or the sensor.

If you study the code for the two libraries you may be able to identify the problem.

...R

When I put radio.begin() and bme.begin() not in setup, but in a function - both devices works, but sensor show -147 temp. When radio.begin() is in the setup, bme.begin() doesn't even start. I tried to analyze libraries, but I can't find what is wrong, can you look at these functions?

bool RF24::begin(void)
  572 {
  573 
  574   uint8_t setup=0;
  575 
  576   #if defined (RF24_LINUX)
  577 
  578     #if defined (MRAA)
  579       GPIO();   
  580       gpio.begin(ce_pin,csn_pin);   
  581     #endif
  582     
  583     #ifdef RF24_RPi
  584       switch(csn_pin){     //Ensure valid hardware CS pin
  585         case 0: break;
  586         case 1: break;
  587         // Allow BCM2835 enums for RPi
  588         case 8: csn_pin = 0; break;
  589         case 7: csn_pin = 1; break;
  590         default: csn_pin = 0; break;
  591       }
  592     #endif
  593     
  594     _SPI.begin(csn_pin);
  595 
  596     pinMode(ce_pin,OUTPUT);
  597     ce(LOW);    
  598 
  599     delay(100);
  600   
  601   #elif defined(LITTLEWIRE)
  602     pinMode(csn_pin,OUTPUT);
  603     _SPI.begin();
  604     csn(HIGH);
  605   #elif defined(XMEGA_D3)
  606     if (ce_pin != csn_pin) pinMode(ce_pin,OUTPUT);
  607     _SPI.begin(csn_pin);
  608     ce(LOW);
  609     csn(HIGH);
  610     delay(200);
  611   #else
  612     // Initialize pins
  613     if (ce_pin != csn_pin) pinMode(ce_pin,OUTPUT);  
  614   
  615     #if ! defined(LITTLEWIRE)
  616       if (ce_pin != csn_pin)
  617     #endif
  618         pinMode(csn_pin,OUTPUT);
  619     
  620     _SPI.begin();
  621     ce(LOW);
  622     csn(HIGH);
  623     #if defined (__ARDUINO_X86__)
  624         delay(100);
  625     #endif
  626   #endif //Linux
  627 
  628   // Must allow the radio time to settle else configuration bits will not necessarily stick.
  629   // This is actually only required following power up but some settling time also appears to
  630   // be required after resets too. For full coverage, we'll always assume the worst.
  631   // Enabling 16b CRC is by far the most obvious case if the wrong timing is used - or skipped.
  632   // Technically we require 4.5ms + 14us as a worst case. We'll just call it 5ms for good measure.
  633   // WARNING: Delay is based on P-variant whereby non-P *may* require different timing.
  634   delay( 5 ) ;
  635 
  636   // Reset NRF_CONFIG and enable 16-bit CRC.
  637   write_register( NRF_CONFIG, 0b00001100 ) ;
  638 
  639   // Set 1500uS (minimum for 32B payload in ESB@250KBPS) timeouts, to make testing a little easier
  640   // WARNING: If this is ever lowered, either 250KBS mode with AA is broken or maximum packet
  641   // sizes must never be used. See documentation for a more complete explanation.
  642   setRetries(5,15);
  643 
  644   // Reset value is MAX
  645   //setPALevel( RF24_PA_MAX ) ;
  646 
  647   // check for connected module and if this is a p nRF24l01 variant
  648   //
  649   if( setDataRate( RF24_250KBPS ) )
  650   {
  651     p_variant = true ;
  652   }
  653   setup = read_register(RF_SETUP);
  654   /*if( setup == 0b00001110 )     // register default for nRF24L01P
  655   {
  656     p_variant = true ;
  657   }*/
  658   
  659   // Then set the data rate to the slowest (and most reliable) speed supported by all
  660   // hardware.
  661   setDataRate( RF24_1MBPS ) ;
  662 
  663   // Initialize CRC and request 2-byte (16bit) CRC
  664   //setCRCLength( RF24_CRC_16 ) ;
  665 
  666   // Disable dynamic payloads, to match dynamic_payloads_enabled setting - Reset value is 0
  667   toggle_features();
  668   write_register(FEATURE,0 );
  669   write_register(DYNPD,0);
  670 
  671   // Reset current status
  672   // Notice reset and flush is the last thing we do
  673   write_register(NRF_STATUS,_BV(RX_DR) | _BV(TX_DS) | _BV(MAX_RT) );
  674 
  675   // Set up default configuration.  Callers can always change it later.
  676   // This channel should be universally safe and not bleed over into adjacent
  677   // spectrum.
  678   setChannel(76);
  679 
  680   // Flush buffers
  681   flush_rx();
  682   flush_tx();
  683 
  684   powerUp(); //Power up by default when begin() is called
  685 
  686   // Enable PTX, do not write CE high so radio will remain in standby I mode ( 130us max to transition to RX or TX instead of 1500us from powerUp )
  687   // PTX should use only 22uA of power
  688   write_register(NRF_CONFIG, ( read_register(NRF_CONFIG) ) & ~_BV(PRIM_RX) );
  689 
  690   // if setup is 0 or ff then there was no response from module
  691   return ( setup != 0 && setup != 0xff );
  692 }

Sensor Begin function

bool Adafruit_BME280::begin(uint8_t a) {

  _i2caddr = a;



  if (_cs == -1) {

    // i2c

    Wire.begin();

  } else {

    digitalWrite(_cs, HIGH);

    pinMode(_cs, OUTPUT);



    if (_sck == -1) {

      // hardware SPI

      SPI.begin();

    } else {

      // software SPI

      pinMode(_sck, OUTPUT);

      pinMode(_mosi, OUTPUT);

      pinMode(_miso, INPUT);

    }

  }



  if (read8(BME280_REGISTER_CHIPID) != 0x60)

    return false;



  readCoefficients();



  //Set before CONTROL_meas (DS 5.4.3)

  write8(BME280_REGISTER_CONTROLHUMID, 0x05); //16x oversampling 



  write8(BME280_REGISTER_CONTROL, 0xB7); // 16x ovesampling, normal mode

  return true;

}

Please post your complete latest program without line numbers.

...R

My latest code where both devices work, but sensor send bad information: temperature always -145:

#include <SPI.h>
#include <RF24.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>

#define BME_SCK 13
#define BME_MISO 12
#define BME_MOSI 11
#define BME_CS 7

#define SEALEVELPRESSURE_HPA (1013.25)
Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO,  BME_SCK);

// ce, csn pins
RF24 radio(8, 10);


void setup(void) {

Serial.begin(9600);
 

}

void measure() {   //measure
 bme.begin();
 Serial.print("Temperature = ");
 Serial.print(bme.readTemperature());
 Serial.println(" *C");
}

void sendinfo() {   //send

  
 radio.begin();
 radio.setPALevel(RF24_PA_MAX);
 radio.setChannel(0x76);
 radio.openWritingPipe(0xF0F0F0F0E1LL);
 radio.enableDynamicPayloads();
 radio.powerUp();


 const char text[] = "Hello World is awesome";
 radio.write(&text, sizeof(text));
 delay(10);
}

void loop(void) {
 measure();
 sendinfo();
}

Adafruit_BME280.cpp library

RF24.cpp library where RF24.begin() written from 572 line

I don't have a BME sensor so it is difficult to know what the problem might be.

What is the minimum you need to disable within the program in Reply #4 in order to get the BME sensor to work?

...R

Deleting radio.begin() solves BME problem, but NRF doesn't work then;/

I bet this library part from rf24.begin() causes error...

 uint8_t setup=0;

  #if defined (RF24_LINUX)

	SPI();
  	

	switch(csn_pin){     //Ensure valid hardware CS pin
	  case 0: break;
	  case 1: break;
	  // Allow BCM2835 enums for RPi
	  case 8: csn_pin = 0; break;
	  case 7: csn_pin = 1; break;
	  default: csn_pin = 0; break;
	}	
	
    _SPI.begin(csn_pin);

	pinMode(ce_pin,OUTPUT);
	ce(LOW);    

	delay(100);
  
 
     #else
    // Initialize pins
    if (ce_pin != csn_pin) pinMode(ce_pin,OUTPUT);  
      pinMode(csn_pin,OUTPUT);
   
    _SPI.begin();
    ce(LOW);
  	csn(HIGH); 
	
  	#if defined (__ARDUINO_X86__)
		delay(100); 
  	#endif 
  #endif //Linux

My best guess (and it's not one I would bet much on) is that in RF24_config.h the line

  const uint32_t RF24_SPI_SPEED = 10000000;

defines the SPI speed which is used everywhere in the function beginTransaction()

But in the BME code the speed is defined as

SPI.beginTransaction(SPISettings(500000, MSBFIRST, SPI_MODE0));

I wonder what would happen if you edit RF24_config.h and change the value to match the BME code?

...R

Nothing changes :confused: This error is so hard to find out...

Fact that might help: When I measure temp, first measurement is good and it looks like that:

Temp: 25
Temp:-147
Temp:-147
Temp:-147
Temp:-147

This is because before 1st measurement function rf24.begin() isn't called

I had been assuming the first value would be OK.

Are you expecting 25s where it is actually showing -147?

Can you ask the Adafruit people about it?

Another thought is to study the SPI library reference and maybe try putting some SPI settings commands between the RF24 call and the BME call.

I note you are using pin 10 with the RF24. Maybe re-wire things so the BME can use that - though I can't think why it would matter. I do know that the RF24 won't care what pins are used for CE and CSN.

...R
PS make sure to set the RF24 code back to what it should be.

Yes I expect 25, because it's my house temperature :smiley:
Pin swap didn't change anything :confused:

When i delete nrf24.begin() I got right temperature for example:

temp:25.01
temp:25.02
temp:25.04
temp:25.03
temp:25.02
temp:25.03

with nrf.begin() :

temp:25.01
temp:-172
temp:-172
temp:-172
temp:-172
temp:-172

I will try to do what you said after work :slight_smile:

Just find out that BME280 CS pin never go LOW - as I know it only communicate when cs is low?

I solved the problem:

Instead of:
#define BME_SCK 13
#define BME_MISO 12
#define BME_MOSI 11
#define BME_CS 10

//Adafruit_BME280 bme; // I2C
//Adafruit_BME280 bme(BME_CS); // hardware SPI
Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK);

I used hardware SPI:

#define BME_CS 10

Adafruit_BME280 bme(BME_CS); // hardware SPI

Robin2 thank you very much for the help :slight_smile: