Pb with bmp280 on I2C

hello,
I want to build a weather station using an esp 32 Devkitc v4. the part with the wind meter wind vane and rain meter work ok (mostly ) .I want to add the rtc ds3231 via I2C, work OK, and I want add temperature and pressure (humidity too but obviously aliexpress don't know the difference between bmp and bme ;-( ), the ds3231 and the bmp280 are on the same I2C pins (gpio 21 and 22 as per the pinout.
the pb is I can get the bmp working with the bmx280MI library and the simple example ino file:

// BMx280_I2C.ino
//
// shows how to use the BMP280 / BMx280 library with the sensor connected using I2C.
//
// Copyright (c) 2018 Gregor Christandl
//
// connect the AS3935 to the Arduino like this:
//
// Arduino - BMP280 / BME280
// 3.3V ---- VCC
// GND ----- GND
// SDA ----- SDA
// SCL ----- SCL
// some BMP280/BME280 modules break out the CSB and SDO pins as well: 
// 5V ------ CSB (enables the I2C interface)
// GND ----- SDO (I2C Address 0x76)
// 5V ------ SDO (I2C Address 0x77)
// other pins can be left unconnected.

#include <Arduino.h>
#include <Wire.h>

#include <BMx280I2C.h>

#define I2C_ADDRESS 0x76

//create a BMx280I2C object using the I2C interface with I2C Address 0x76
BMx280I2C bmx280(I2C_ADDRESS);

void setup() {
  // put your setup code here, to run once:
	Serial.begin(9600);

	//wait for serial connection to open (only necessary on some boards)
	while (!Serial);

	Wire.begin();

	//begin() checks the Interface, reads the sensor ID (to differentiate between BMP280 and BME280)
	//and reads compensation parameters.
	if (!bmx280.begin())
	{
		Serial.println("begin() failed. check your BMx280 Interface and I2C Address.");
		while (1);
	}

	if (bmx280.isBME280())
  {
		Serial.println("sensor is a BME280");
  }
	else
  {
		Serial.println("sensor is a BMP280");
  }
	//reset sensor to default parameters.
	bmx280.resetToDefaults();

	//by default sensing is disabled and must be enabled by setting a non-zero
	//oversampling setting.
	//set an oversampling setting for pressure and temperature measurements. 
	bmx280.writeOversamplingPressure(BMx280MI::OSRS_P_x16);
	bmx280.writeOversamplingTemperature(BMx280MI::OSRS_T_x16);

	//if sensor is a BME280, set an oversampling setting for humidity measurements.
	if (bmx280.isBME280())
		bmx280.writeOversamplingHumidity(BMx280MI::OSRS_H_x16);
}

void loop() {
  // put your main code here, to run repeatedly:

	delay(1000);

	//start a measurement
	if (!bmx280.measure())
	{
		Serial.println("could not start measurement, is a measurement already running?");
		return;
	}

	//wait for the measurement to finish
	do
	{
		delay(100);
	} while (!bmx280.hasValue());

	Serial.print("Pressure: "); Serial.println(bmx280.getPressure());
	Serial.print("Pressure (64 bit): "); Serial.println(bmx280.getPressure64());
	Serial.print("Temperature: "); Serial.println(bmx280.getTemperature());

	//important: measurement data is read from the sensor in function hasValue() only. 
	//make sure to call get*() functions only after hasValue() has returned true. 
	if (bmx280.isBME280())
	{
		Serial.print("Humidity: "); 
		Serial.println(bmx280.getHumidity());
	}
}

work ok , I get the normal value....

Pressure: 102373.00
Pressure (64 bit): 102370.33
Temperature: 23.88

but when I use the code in my code

#include <Wire.h>

#include <DS3231.h>
#include <Arduino.h>
#include <BMx280I2C.h>

DS3231 myRTC;


#define I2C_ADDRESS 0x76
BMx280I2C bmx280(I2C_ADDRESS);
bool century = false;
bool h12Flag;
bool pmFlag;
float tempe=0;
float press=0;


// pour le Macch +19.7HPa pour compensation altitude
const int gir = 34;
const int pluiv = 39;
const int anemo = 36;

unsigned long time1=0;
unsigned long time2=0;
unsigned long time3=0;
float rafale=0;
unsigned long vent=0;
float vent1=0;
float vent2=0;
unsigned long nbmesure=0;
float ventmoyen=0;
float pluvio=0.2794;  //mm eau par impulsion
long pluie=0;
float pluie2=0;
float pluieh24=0;
unsigned long girouette=0;
char dirvent[]=" n ";
int heure ;
int sec;
int year;
int moid;
int jour;









void setup() {
  Serial.begin(9600);
 Wire.begin();
 Wire.setClock(100000);
 bmx280.resetToDefaults();

attachInterrupt(digitalPinToInterrupt(anemo), interruptVent, RISING);
attachInterrupt(digitalPinToInterrupt(pluiv), interruptPluie, RISING);





time2=millis();
time3= millis();

}

// interruptVent
void interruptVent(){
  vent=vent+1;
}

//interruptPluie
void interruptPluie(){
  pluie=pluie+1;
}








void loop() {
  // put your main code here, to run repeatedly:
time1= millis();
if (time1-time2>3000){
  time2=millis();
  vent1=(3.14*0.07*2*vent)/3*3.6; //
  if (vent1>rafale) rafale=vent1;
  nbmesure++;
  vent2=vent2+vent1;
  Serial.print ("vent :");
  Serial.println(vent1);
  Serial.print ("pluie  :");
  Serial.println (pluie);
  vent=0;
}
// toutes les minutes
if (time1-time3>20000){
  
  //calcul vent moyen et rafale sur 1 minute
  ventmoyen=vent2/nbmesure;
  Serial.print("vent moyen :");
  Serial.println(ventmoyen);
  Serial.print ("rafale :");
  Serial.println(rafale);
  vent2=0;
  rafale=0;
  nbmesure=0;
  time3=millis();


//pluviometre
  pluie2=(pluie*pluvio);
  pluieh24=pluieh24+pluie2;
  Serial.print("mm de pluie :");
  Serial.println(pluie2);
  Serial.print("24h :");
  Serial.println(pluieh24);
  pluie=0;
  pluie2=0;

  // inserer remise à zero pluieh24 si heure >0h00 et <0h02



  //girouette
  girouette=analogRead(gir);
   //if (girouette>  && girouette <   ) dirvent=" N ";
   //if (girouette>  && girouette <   ) dirvent=" N ";
  // if (girouette>  && girouette <   ) dirvent=" N ";
  // if (girouette>  && girouette <   ) dirvent=" N ";
  // if (girouette>  && girouette <   ) dirvent=" N ";
   //if (girouette>  && girouette <   ) dirvent=" N ";
  // if (girouette>  && girouette <   ) dirvent=" N ";
  // if (girouette>  && girouette <   ) dirvent=" N ";
   //if (girouette>  && girouette <   ) dirvent=" N ";
  // if (girouette>  && girouette <   ) dirvent=" N ";
   //if (girouette>  && girouette <   ) dirvent=" N ";
   //if (girouette>  && girouette <   ) dirvent=" N ";
  Serial.print ("Vent de :");
  Serial.println (girouette);

  	if (!bmx280.measure())
	{
		Serial.println("could not start measurement, is a measurement already running?");
		return;
	}
 
  while (!bmx280.hasValue());

    Serial.print("Pressure: "); 
    Serial.println(bmx280.getPressure());

	Serial.print("Temperature: "); 
  Serial.println(bmx280.getTemperature());

        Serial.print(myRTC.getYear(), DEC);
      Serial.print("-");
      Serial.print(myRTC.getMonth(century), DEC);
      Serial.print("-");
      Serial.print(myRTC.getDate(), DEC);
      Serial.print(" ");
      Serial.print(myRTC.getHour(h12Flag, pmFlag), DEC); //24-hr
      Serial.print(":");
      Serial.println(myRTC.getMinute(), DEC);

}
}



 

I get :

Pressure: nan
Temperature: nan

0-1-1 0:23

So the ds3231 give the date /hour (the I2C is working )
but I can't get the temperature and pressure from the bmp.

I tried different library with the same result.

as it work with the same wiring and the test sketch, and not in mine, obviously there is something in my code.
I tried with no interrupt , but no chance

I'm a rather beginner in arduino, my last programming was in turbo pascal and Dbase4 (less than 60 can't know :wink:
sorry for the bad english, french school 40 years ago

thank you for any tip.

Run the I2C scanner and let us know what you get. They have to be at different addresses. If you are not getting consistently the two addresses post an annotated schematic showing exactly how you have wired it with links to technical information on each of the hardware devices. Be sure to show the pull up resistors.

Scanning...
I2C device found at address 0x57 !
I2C device found at address 0x5F !
I2C device found at address 0x68 !
I2C device found at address 0x76 !

0x57 and 0x5F seems linked to the board esp 32 WROOM 32d, and are here with the bord alone
0x68 is the ds3231 address 0x76 is the bmp280.

10K pull up resistor

with the same wiring one the breadboard, the testing sketch work, mine not ....

thank you

Try switching your pull up resistors to something in the 2.5K to 3.5K range. What Arduino did you use on the breadboard. 10K resistors are border line but many use them on the modules because you can connect several modules and it will still work.

as I said it's an esp32 Wroom 32 devkitc-V4. the ds3231 works fine and the bmp too with the example sketch. without changing anything on the wiring on the breadboard with my sketch the ds3231 works fine but not the bmp280. look like a software pb. I'm puzzled with the "nan" value can't say where it comes from.
thank you for your help

Welcome to the forum.

I think there are more than one issues.

Can you feel the rhythm of setup() ? It is not the same:
First sketch: bmx280.begin() - bmx280.isBME280() - bmx280.resetToDefaults()
Second sketch: bmx280.resetToDefaults()

I don't think that 0x57 and 0x5F are on the ESP32 Dev Kit C V4 board :thinking:
Can you remove everything and run a I2C Scanner for just the board ?

How long are the wires of the I2C bus ?

thank you for the tip !
the pb is the library is not really beginner friendly, with example sketches quite complex and no basic explanation.
if you want to get the results from the bmx280, you need the code like that:

  bmx280.begin();
 	bmx280.writeOversamplingPressure(BMx280MI::OSRS_P_x16);
	bmx280.writeOversamplingTemperature(BMx280MI::OSRS_T_x16);
  bmx280.measure();
  while (!bmx280.hasValue());  
    press=bmx280.getPressure();
    tempe=bmx280.getTemperature();

  Serial.print ("pression");
  Serial.println (press);
  Serial.print ("temp");
  Serial.println (tempe);

and it works !

about the wire's length is around 20cm, doesn't seem to be the pb.
and you were right about the address 0x76 is the bmp280 address,
Scanning...
I2C device found at address 0x57 !
I2C device found at address 0x5F !
I2C device found at address 0x68 !
are the addresses from the ds3231 after a bit of search I found the 0x68 is the address for the rtc chip, the 0x57 the address of the small eeprom AT24C32 I didn't know is on the module. look like the 0x5F address is also for the eeprom which get 2 for one chip.

thank you a lot

It seems that 0x5F is some kind of "ghost" address :ghost: for a clone/counterfeit EEPROM.
See also: I2C response to "Ghost Address" 0x5F - Electrical Engineering Stack Exchange

the bugs are well known, if there is a ghost in the machine.. gardening may be safer :wink:

anyway thanks for the help

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.