From 95b0fe880f814a8fbf4ef0a60e7afb431396410c Mon Sep 17 00:00:00 2001 From: Graeme Cross Date: Mon, 5 Jun 2017 20:02:18 +1000 Subject: [PATCH] Improved BME280 humidity initialisation so that it does not override existing oversampling rates that have been set up for the ctrl_meas register. The humidity oversampling rate is set to 1 (as per Section 3.5 of the BME280 data sheet). Signed-off-by: Graeme Cross --- drivers/i2c/bme280_driver.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/drivers/i2c/bme280_driver.go b/drivers/i2c/bme280_driver.go index 3ac28c70..212437d3 100644 --- a/drivers/i2c/bme280_driver.go +++ b/drivers/i2c/bme280_driver.go @@ -7,7 +7,6 @@ import ( ) const bme280RegisterControlHumidity = 0xF2 -const bme280RegisterControl = 0xf4 const bme280RegisterHumidityMSB = 0xFD const bme280RegisterCalibDigH1 = 0xa1 const bme280RegisterCalibDigH2LSB = 0xe1 @@ -114,9 +113,15 @@ func (d *BME280Driver) initHumidity() (err error) { d.hc.h5 = 0 + (int16(addrE6) << 4) | (int16(addrE5) >> 4) d.connection.WriteByteData(bme280RegisterControlHumidity, 0x3F) - d.connection.WriteByteData(bmp280RegisterControl, 0x3F) - return nil + // The 'ctrl_hum' register sets the humidity data acquisition options of + // the device. Changes to this register only become effective after a write + // operation to 'ctrl_meas'. Read the current value in, then write it back + cmr, err := d.connection.ReadByteData(bmp280RegisterControl) + if err != nil { + d.connection.WriteByteData(bmp280RegisterControl, cmr) + } + return err } func (d *BME280Driver) rawHumidity() (uint32, error) {