Skip to content

Commit 746e77f

Browse files
authored
Merge pull request #9 from caternuson/iss2_mcp2221
Refinements for MCP2221
2 parents 66b756d + 081f2ee commit 746e77f

File tree

2 files changed

+46
-5
lines changed

2 files changed

+46
-5
lines changed

adafruit_scd30.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,11 @@ def __init__(self, i2c_bus, ambient_pressure=0, address=SCD30_DEFAULT_ADDR):
6161
self._buffer = bytearray(18)
6262
self._crc_buffer = bytearray(2)
6363

64-
self.reset()
65-
64+
# set continuous measurement interval in seconds
6665
self.measurement_interval = 2
66+
# activate automatic self-calibration
6767
self.self_calibration_enabled = True
68-
# sets ambient pressure and starts continuous measurements
68+
# trigger continuous measurements with optional ambient pressure compensation
6969
self.ambient_pressure = ambient_pressure
7070

7171
# cached readings
@@ -76,7 +76,7 @@ def __init__(self, i2c_bus, ambient_pressure=0, address=SCD30_DEFAULT_ADDR):
7676
def reset(self):
7777
"""Perform a soft reset on the sensor, restoring default values"""
7878
self._send_command(_CMD_SOFT_RESET)
79-
sleep(0.030) # not mentioned by datasheet, but required to avoid IO error
79+
sleep(0.1) # not mentioned by datasheet, but required to avoid IO error
8080

8181
@property
8282
def measurement_interval(self):
@@ -108,6 +108,8 @@ def self_calibration_enabled(self):
108108
@self_calibration_enabled.setter
109109
def self_calibration_enabled(self, enabled):
110110
self._send_command(_CMD_AUTOMATIC_SELF_CALIBRATION, enabled)
111+
if enabled:
112+
sleep(0.01)
111113

112114
@property
113115
def data_available(self):
@@ -196,7 +198,7 @@ def temperature(self):
196198
def relative_humidity(self):
197199
"""Returns the current relative humidity in %rH.
198200
199-
**NOTE** Between measurements, the most recent reading will be cached and returned. """
201+
**NOTE** Between measurements, the most recent reading will be cached and returned."""
200202
if self.data_available:
201203
self._read_data()
202204
return self._relative_humidity

examples/scd30_mcp2221test.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# SPDX-FileCopyrightText: 2021 by Carter Nelson, written for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: Unlicense
4+
import time
5+
import board
6+
import busio
7+
import adafruit_scd30
8+
9+
i2c = busio.I2C(board.SCL, board.SDA)
10+
scd = adafruit_scd30.SCD30(i2c)
11+
12+
# The SCD30 reset generates a hiccup on the SCL and SDA lines
13+
# which can end up not being handled well by different hosts.
14+
scd.reset()
15+
16+
# The MCP2221 is known to not like the SCD30 reset hiccup.
17+
# See below for more information:
18+
# https://github.com/adafruit/Adafruit_CircuitPython_SCD30/issues/2
19+
# Can get around it by resetting via this hack.
20+
# pylint:disable=protected-access
21+
if hasattr(i2c, "_i2c"):
22+
# we're using Blinka, check for MCP2221
23+
if hasattr(i2c._i2c, "_mcp2221"):
24+
# reset it
25+
i2c._i2c._mcp2221._reset()
26+
27+
while True:
28+
# since the measurement interval is long (2+ seconds) we check for new data before reading
29+
# the values, to ensure current readings.
30+
if scd.data_available:
31+
print("Data Available!")
32+
print("CO2:", scd.CO2, "PPM")
33+
print("Temperature:", scd.temperature, "degrees C")
34+
print("Humidity:", scd.relative_humidity, "%%rH")
35+
print("")
36+
print("Waiting for new data...")
37+
print("")
38+
39+
time.sleep(0.5)

0 commit comments

Comments
 (0)