2021-08-22 06:32 AM
I recently have purchased a BME280 I2C sensor, which measures pressure, temperature, and humidity.
I wanted to create a library for it to measure those variables. the problem is that the result (at least, the temperature measurement) is wrong.
it throws the following:
temperature -> 106.2 °C (sometimes, -143, it should show 28 °C or 30 °C)
humidity -> 76% or 53% (this seems the most accurate because it shows 76% when I'm touching it and 53% when not, but sometimes, it just shows 0%)
pressure -> 1137.27 Hpa (according to google, where I live, the average pressure in Hpa is 1009)
I was following the datasheet for the compensation formula and reading data recommendations. I don't know if it's problem with the sensor, or something wrong with my software.
std::uint32_t raw_press = (data[0] << 12) | (data[1] << 4) | (data[2] >> 4);
std::uint32_t raw_temp = (data[3] << 12) | (data[4] << 4) | (data[5] >> 4);
std::uint32_t raw_hum = (data[6] << 8) | data[7];
temp = CalculateTemp(raw_temp, t_fine);
press = CalculatePress(raw_press, t_fine);
hum = CalculateHum(raw_hum, t_fine);
The previous code is arranging the raw data (I'm using oversampling x1 and no filters)
float BME280::CalculateTemp(std::int32_t raw, std::int32_t& t_fine){
std::int32_t var1, var2, final;
std::uint16_t dig_T1 = (m_dig[1] << 8) | m_dig[0];
std::int16_t dig_T2 = (m_dig[3] << 8) | m_dig[2];
std::int16_t dig_T3 = (m_dig[5] << 8) | m_dig[4];
var1 = ((((raw >> 3) - ((int32_t)dig_T1 << 1))) * ((int32_t)dig_T2)) >> 11;
var2 = (((((raw >> 4) - ((int32_t)dig_T1)) * ((raw >> 4) - ((int32_t)dig_T1))) >> 12) * ((int32_t)dig_T3)) >> 14;
t_fine = var1 + var2;
final = (t_fine * 5 + 128) >> 8;
return final/100.0; // in °C
}
And these are the temperature calculations.
If someone has experience with this sensor, it would be a greater help.
Thanks in advance,
2021-08-22 11:10 PM
There are a LOT of BMP280 libraries out there. It's a popular sensor. Might want to cross check what you're doing with one of those.
https://github.com/ciastkolog/BMP280_STM32/blob/master/BMP280/bmp280.c
Humidity is not a fast-changing variable. It shouldn't be changing much by just touching the sensor.
2021-08-23 12:07 AM
Hi,
The simplest way to know if it is a problem with the sensor or with your code is to use the debugger and:
Mind the data types of the calibration parameters, it is easy to have an overflow / underflow.
Also, even if it removes the fun of writing your own library, you can use the one from Adafruit : https://github.com/adafruit/Adafruit_BME280_Library
@TDK simply blowing your breath on such humidity sensor should change considerably its value in the course of 1 or 2 seconds. That is my experience at least.
Best regards.
2021-08-23 12:09 AM
Blowing breath, yes, touching, no.
2021-08-23 12:11 AM
Yes, you are correct.
2021-08-23 05:43 AM
Hi, thanks, yes, i was following one bme library actually, one from adafruit, and another user, but i will check this out. thanks
2021-08-23 05:44 AM
I didn't think that I will try it. Thanks
2024-06-20 09:09 AM
Hi Friends,
I I have to interface STM32F103C8T6 with BME280 I2C sensor to Get the real time Temperature,Humidity and Pressure data . I have tried to get the data so you have to give the library files and code for getting data.