diff --git a/README.md b/README.md index 59e289b..5c12de0 100644 --- a/README.md +++ b/README.md @@ -19,10 +19,11 @@ for details. ## Class -bme = BME280(i2c=i2c, mode=BME280_OSAMPLE_8, address=BME280_I2CADDR) +bme = BME280(mode=BME280_OSAMPLE_8, address=BME280_I2CADDR, i2c=i2c) -mode is the setting for oversampling of the humidity value, address the i2c -address used. +`mode` is the setting for oversampling of the humidity value. It must be either a single +int or a tuple of 3 ints, specifying (mode_hum, mode_temp, mode_pressure). `address` is the i2c +address used, and i2c must be a I2C object. ## Properties @@ -88,13 +89,14 @@ Copy `bme280_float.py` onto the board. Then: ``` python # -# this script assumes the default connection of the I2C bus -# On pycom devuces that is P9 = SDA, P10 = scl +# this script for the rp2040 port assumes the I2C connections at +# GPIO8 and 9. At the RPi Pico, these are the board pins 11 and 12 +# Please check that pull-up resistors are in place at sda and scl. # -import machine +import machine, Pin import bme280_float as bme280 -i2c = machine.I2C() +i2c = machine.I2C(0, sda=machine.Pin(8), scl=machine.Pin(9)) bme = bme280.BME280(i2c=i2c) print(bme.values) diff --git a/bme280_float.py b/bme280_float.py index 06f1b6d..ab3f613 100644 --- a/bme280_float.py +++ b/bme280_float.py @@ -72,13 +72,21 @@ class BME280: i2c=None, **kwargs): # Check that mode is valid. - if mode not in [BME280_OSAMPLE_1, BME280_OSAMPLE_2, BME280_OSAMPLE_4, - BME280_OSAMPLE_8, BME280_OSAMPLE_16]: - raise ValueError( - 'Unexpected mode value {0}. Set mode to one of ' - 'BME280_OSAMPLE_1, BME280_OSAMPLE_2, BME280_OSAMPLE_4,' - 'BME280_OSAMPLE_8, BME280_OSAMPLE_16'.format(mode)) - self._mode = mode + if type(mode) is tuple and len(mode) == 3: + self._mode_hum, self._mode_temp, self._mode_press = mode + elif type(mode) == int: + self._mode_hum, self._mode_temp, self._mode_press = mode, mode, mode + else: + raise ValueError("Wrong type for the mode parameter, must be int or a 3 element tuple") + + for mode in (self._mode_hum, self._mode_temp, self._mode_press): + if mode not in [BME280_OSAMPLE_1, BME280_OSAMPLE_2, BME280_OSAMPLE_4, + BME280_OSAMPLE_8, BME280_OSAMPLE_16]: + raise ValueError( + 'Unexpected mode value {0}. Set mode to one of ' + 'BME280_ULTRALOWPOWER, BME280_STANDARD, BME280_HIGHRES, or ' + 'BME280_ULTRAHIGHRES'.format(mode)) + self.address = address if i2c is None: raise ValueError('An I2C object is required.') @@ -105,7 +113,7 @@ class BME280: self._l8_barray = bytearray(8) self._l3_resultarray = array("i", [0, 0, 0]) - self._l1_barray[0] = self._mode << 5 | self._mode << 2 | MODE_SLEEP + self._l1_barray[0] = self._mode_temp << 5 | self._mode_press << 2 | MODE_SLEEP self.i2c.writeto_mem(self.address, BME280_REGISTER_CONTROL, self._l1_barray) self.t_fine = 0 @@ -120,10 +128,10 @@ class BME280: None """ - self._l1_barray[0] = self._mode + self._l1_barray[0] = self._mode_hum self.i2c.writeto_mem(self.address, BME280_REGISTER_CONTROL_HUM, self._l1_barray) - self._l1_barray[0] = self._mode << 5 | self._mode << 2 | MODE_FORCED + self._l1_barray[0] = self._mode_temp << 5 | self._mode_press << 2 | MODE_FORCED self.i2c.writeto_mem(self.address, BME280_REGISTER_CONTROL, self._l1_barray) diff --git a/bme280_int.py b/bme280_int.py index af118f6..f98dbbd 100644 --- a/bme280_int.py +++ b/bme280_int.py @@ -73,13 +73,21 @@ class BME280: i2c=None, **kwargs): # Check that mode is valid. - if mode not in [BME280_OSAMPLE_1, BME280_OSAMPLE_2, BME280_OSAMPLE_4, - BME280_OSAMPLE_8, BME280_OSAMPLE_16]: - raise ValueError( - 'Unexpected mode value {0}. Set mode to one of ' - 'BME280_OSAMPLE_1, BME280_OSAMPLE_2, BME280_OSAMPLE_4,' - 'BME280_OSAMPLE_8, BME280_OSAMPLE_16'.format(mode)) - self._mode = mode + if type(mode) is tuple and len(mode) == 3: + self._mode_hum, self._mode_temp, self._mode_press = mode + elif type(mode) == int: + self._mode_hum, self._mode_temp, self._mode_press = mode, mode, mode + else: + raise ValueError("Wrong type for the mode parameter, must be int or a 3 element tuple") + + for mode in (self._mode_hum, self._mode_temp, self._mode_press): + if mode not in [BME280_OSAMPLE_1, BME280_OSAMPLE_2, BME280_OSAMPLE_4, + BME280_OSAMPLE_8, BME280_OSAMPLE_16]: + raise ValueError( + 'Unexpected mode value {0}. Set mode to one of ' + 'BME280_ULTRALOWPOWER, BME280_STANDARD, BME280_HIGHRES, or ' + 'BME280_ULTRAHIGHRES'.format(mode)) + self.address = address if i2c is None: raise ValueError('An I2C object is required.') @@ -107,7 +115,7 @@ class BME280: self._l8_barray = bytearray(8) self._l3_resultarray = array("i", [0, 0, 0]) - self._l1_barray[0] = self._mode << 5 | self._mode << 2 | MODE_SLEEP + self._l1_barray[0] = self._mode_temp << 5 | self._mode_press << 2 | MODE_SLEEP self.i2c.writeto_mem(self.address, BME280_REGISTER_CONTROL, bytearray([0x3c | MODE_SLEEP])) @@ -121,10 +129,10 @@ class BME280: None """ - self._l1_barray[0] = self._mode + self._l1_barray[0] = self._mode_hum self.i2c.writeto_mem(self.address, BME280_REGISTER_CONTROL_HUM, self._l1_barray) - self._l1_barray[0] = self._mode << 5 | self._mode << 2 | MODE_FORCED + self._l1_barray[0] = self._mode_temp << 5 | self._mode_press << 2 | MODE_FORCED self.i2c.writeto_mem(self.address, BME280_REGISTER_CONTROL, self._l1_barray) diff --git a/bmetest.py b/bmetest.py index bb74040..a5f6fda 100644 --- a/bmetest.py +++ b/bmetest.py @@ -4,7 +4,7 @@ from machine import I2C from bme280 import * from utime import sleep -i2c=I2C() +i2c=I2C(0) bme280 = BME280(i2c=i2c) while True: print(bme280.values)