adafruit / Adafruit_BME280_Library

Arduino Library for BME280 sensors
Other
333 stars 304 forks source link

config register writes in "Normal" mode may be ignored #30

Closed rai42 closed 4 years ago

rai42 commented 6 years ago

I had the issue that changing the standby duration with setSampling apparently had no effect. Looking at the data sheet, section 7.4.6 says: "Writes to the “config” register in normal mode may be ignored. In sleep mode writes are not ignored.". The current version of setSampling does not honor this. Since after begin() we are in normal mode, this will be an issue for basically every use of setSampling that changes the filter or standby duration settings - i.e. it may work but it it will be unreliable.

So we need to insert another write that resets the mode to "Sleep" before the actual values are written:

void Adafruit_BME280::setSampling(sensor_mode       mode,
         sensor_sampling   tempSampling,
         sensor_sampling   pressSampling,
         sensor_sampling   humSampling,
         sensor_filter     filter,
         standby_duration  duration) {
    _measReg.mode     = mode;
    _measReg.osrs_t   = tempSampling;
    _measReg.osrs_p   = pressSampling;

    _humReg.osrs_h    = humSampling;
    _configReg.filter = filter;
    _configReg.t_sb   = duration;

    // "Writes to the “config” register in normal mode may be ignored.
    // In sleep mode writes are not ignored." (DS 7.4.6)
    write8(BME280_REGISTER_CONTROL, 0);  //temporarily change to sleep mode

    // you must make sure to also set REGISTER_CONTROL after setting the
    // CONTROLHUMID register, otherwise the values won't be applied (see DS 5.4.3)
    write8(BME280_REGISTER_CONTROLHUMID, _humReg.get());
    write8(BME280_REGISTER_CONFIG, _configReg.get());
    write8(BME280_REGISTER_CONTROL, _measReg.get());
}
jrussek commented 5 years ago

Thanks @ladyada! Fixed in master now :)