Interfacing with BME280 problem
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,
