Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refinements for MCP2221 #9

Merged
merged 5 commits into from
Feb 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions adafruit_scd30.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down
39 changes: 39 additions & 0 deletions examples/scd30_mcp2221test.py
Original file line number Diff line number Diff line change
@@ -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)