From 6a2063aa67b8923313e285b09e000e387c4b008a Mon Sep 17 00:00:00 2001 From: caternuson Date: Tue, 9 Feb 2021 15:46:16 -0800 Subject: [PATCH 1/5] refinements for mcp2221 --- adafruit_scd30.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/adafruit_scd30.py b/adafruit_scd30.py index 68ba9ef..37ce10b 100644 --- a/adafruit_scd30.py +++ b/adafruit_scd30.py @@ -63,9 +63,11 @@ def __init__(self, i2c_bus, ambient_pressure=0, address=SCD30_DEFAULT_ADDR): self.reset() + # set continuous measurement interval in seconds self.measurement_interval = 2 + # activate automatic self-calibration self.self_calibration_enabled = True - # sets ambient pressure and starts continuous measurements + # trigger continuous measurements with optional ambient pressure compensation self.ambient_pressure = ambient_pressure # cached readings @@ -76,7 +78,13 @@ def __init__(self, i2c_bus, ambient_pressure=0, address=SCD30_DEFAULT_ADDR): def reset(self): """Perform a soft reset on the sensor, restoring default values""" self._send_command(_CMD_SOFT_RESET) - sleep(0.030) # not mentioned by datasheet, but required to avoid IO error + sleep(0.1) # not mentioned by datasheet, but required to avoid IO error + # are we using Blinka? + if hasattr(self.i2c_device.i2c, "_i2c"): + # with an MCP2221? + if hasattr(self.i2c_device.i2c._i2c, "_mcp2221"): + # then lets reset that too + self.i2c_device.i2c._i2c._mcp2221._reset() @property def measurement_interval(self): @@ -108,6 +116,8 @@ def self_calibration_enabled(self): @self_calibration_enabled.setter def self_calibration_enabled(self, enabled): self._send_command(_CMD_AUTOMATIC_SELF_CALIBRATION, enabled) + if enabled: + sleep(0.01) @property def data_available(self): From b507ba046e38b6bc6042311dd0a646ac9eaffc50 Mon Sep 17 00:00:00 2001 From: caternuson Date: Tue, 9 Feb 2021 15:59:43 -0800 Subject: [PATCH 2/5] blinted --- adafruit_scd30.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/adafruit_scd30.py b/adafruit_scd30.py index 37ce10b..5751bb5 100644 --- a/adafruit_scd30.py +++ b/adafruit_scd30.py @@ -79,6 +79,7 @@ def reset(self): """Perform a soft reset on the sensor, restoring default values""" self._send_command(_CMD_SOFT_RESET) sleep(0.1) # not mentioned by datasheet, but required to avoid IO error + # pylint:disable=protected-access # are we using Blinka? if hasattr(self.i2c_device.i2c, "_i2c"): # with an MCP2221? @@ -206,7 +207,7 @@ def temperature(self): def relative_humidity(self): """Returns the current relative humidity in %rH. - **NOTE** Between measurements, the most recent reading will be cached and returned. """ + **NOTE** Between measurements, the most recent reading will be cached and returned.""" if self.data_available: self._read_data() return self._relative_humidity From 5af0f25ebd513e419387eff686858af3c0485b64 Mon Sep 17 00:00:00 2001 From: caternuson Date: Tue, 9 Feb 2021 16:29:24 -0800 Subject: [PATCH 3/5] remove reset from init --- adafruit_scd30.py | 9 --------- examples/scd30_reset.py | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 9 deletions(-) create mode 100644 examples/scd30_reset.py diff --git a/adafruit_scd30.py b/adafruit_scd30.py index 5751bb5..187596d 100644 --- a/adafruit_scd30.py +++ b/adafruit_scd30.py @@ -61,8 +61,6 @@ def __init__(self, i2c_bus, ambient_pressure=0, address=SCD30_DEFAULT_ADDR): self._buffer = bytearray(18) self._crc_buffer = bytearray(2) - self.reset() - # set continuous measurement interval in seconds self.measurement_interval = 2 # activate automatic self-calibration @@ -79,13 +77,6 @@ def reset(self): """Perform a soft reset on the sensor, restoring default values""" self._send_command(_CMD_SOFT_RESET) sleep(0.1) # not mentioned by datasheet, but required to avoid IO error - # pylint:disable=protected-access - # are we using Blinka? - if hasattr(self.i2c_device.i2c, "_i2c"): - # with an MCP2221? - if hasattr(self.i2c_device.i2c._i2c, "_mcp2221"): - # then lets reset that too - self.i2c_device.i2c._i2c._mcp2221._reset() @property def measurement_interval(self): diff --git a/examples/scd30_reset.py b/examples/scd30_reset.py new file mode 100644 index 0000000..5e8e6e3 --- /dev/null +++ b/examples/scd30_reset.py @@ -0,0 +1,38 @@ +# SPDX-FileCopyrightText: 2021 by Carter Nelson, written for Adafruit Industries +# +# SPDX-License-Identifier: Unlicense +import time +import board +import busio +import adafruit_scd30 + +i2c = busio.I2C(board.SCL, board.SDA) +scd = adafruit_scd30.SCD30(i2c) + +# The SCD30 reset generates a hiccup on the SCL and SDA lines +# which can end up not being handled well by different hosts. +scd.reset() + +# The MCP2221 is known to not like the SCD30 reset hiccup. +# See below for more information: +# https://github.com/adafruit/Adafruit_CircuitPython_SCD30/issues/2 +# Can get around it by resetting via this hack. +if hasattr(i2c, "_i2c"): + # we're using Blinka, check for MCP2221 + if hasattr(i2c._i2c, "_mcp2221"): + # reset it + i2c._i2c._mcp2221._reset() + +while True: + # since the measurement interval is long (2+ seconds) we check for new data before reading + # the values, to ensure current readings. + if scd.data_available: + print("Data Available!") + print("CO2:", scd.CO2, "PPM") + print("Temperature:", scd.temperature, "degrees C") + print("Humidity:", scd.relative_humidity, "%%rH") + print("") + print("Waiting for new data...") + print("") + + time.sleep(0.5) From 1a63b9eda60f70d649e73d0a183f1847c32057d1 Mon Sep 17 00:00:00 2001 From: caternuson Date: Tue, 9 Feb 2021 16:41:45 -0800 Subject: [PATCH 4/5] shushing example --- examples/scd30_reset.py | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/scd30_reset.py b/examples/scd30_reset.py index 5e8e6e3..22639d3 100644 --- a/examples/scd30_reset.py +++ b/examples/scd30_reset.py @@ -17,6 +17,7 @@ # See below for more information: # https://github.com/adafruit/Adafruit_CircuitPython_SCD30/issues/2 # Can get around it by resetting via this hack. +# pylint:disable=protected-access if hasattr(i2c, "_i2c"): # we're using Blinka, check for MCP2221 if hasattr(i2c._i2c, "_mcp2221"): From 081f2eed65f8f7bf75e9118b4acaaf83c02cb06a Mon Sep 17 00:00:00 2001 From: caternuson Date: Tue, 9 Feb 2021 18:24:44 -0800 Subject: [PATCH 5/5] example rename --- examples/{scd30_reset.py => scd30_mcp2221test.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename examples/{scd30_reset.py => scd30_mcp2221test.py} (100%) diff --git a/examples/scd30_reset.py b/examples/scd30_mcp2221test.py similarity index 100% rename from examples/scd30_reset.py rename to examples/scd30_mcp2221test.py