diff --git a/adafruit_scd30.py b/adafruit_scd30.py index 68ba9ef..187596d 100644 --- a/adafruit_scd30.py +++ b/adafruit_scd30.py @@ -61,11 +61,11 @@ 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 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 +76,7 @@ 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 @property def measurement_interval(self): @@ -108,6 +108,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): @@ -196,7 +198,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 diff --git a/examples/scd30_mcp2221test.py b/examples/scd30_mcp2221test.py new file mode 100644 index 0000000..22639d3 --- /dev/null +++ b/examples/scd30_mcp2221test.py @@ -0,0 +1,39 @@ +# 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. +# pylint:disable=protected-access +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)