Skip to content

Commit 36009c2

Browse files
committed
last new knobs
1 parent aa92c95 commit 36009c2

File tree

3 files changed

+81
-13
lines changed

3 files changed

+81
-13
lines changed

adafruit_scd30.py

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,12 @@
2828
"""
2929

3030
# imports
31+
from time import sleep
3132
from struct import unpack_from, unpack
3233
import adafruit_bus_device.i2c_device as i2c_device
3334
from micropython import const
3435

36+
3537
__version__ = "0.0.0-auto.0"
3638
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_SCD30.git"
3739
SCD30_DEFAULT_ADDR = 0x61
@@ -44,22 +46,38 @@
4446
_CMD_SET_FORCED_RECALIBRATION_FACTOR = const(0x5204)
4547
_CMD_SET_TEMPERATURE_OFFSET = const(0x5403)
4648
_CMD_SET_ALTITUDE_COMPENSATION = const(0x5102)
49+
_CMD_SOFT_RESET = const(0xD304)
4750

4851

4952
class SCD30:
5053
"""CircuitPython helper class for using the SCD30 e-CO2 sensor"""
5154

52-
def __init__(self, i2c_bus, address=SCD30_DEFAULT_ADDR):
55+
def __init__(self, i2c_bus, ambient_pressure=0, address=SCD30_DEFAULT_ADDR):
56+
if ambient_pressure != 0:
57+
if ambient_pressure < 700 or ambient_pressure > 1200:
58+
raise AttributeError("`ambient_pressure` must be from 700-1200 mBar")
59+
5360
self.i2c_device = i2c_device.I2CDevice(i2c_bus, address)
5461
self._buffer = bytearray(18)
5562
self._crc_buffer = bytearray(2)
56-
self._send_command(_CMD_CONTINUOUS_MEASUREMENT, 0)
63+
64+
self.reset()
65+
5766
self.measurement_interval = 2
5867
self.self_calibration_enabled = True
68+
# sets ambient pressure and starts continuous measurements
69+
self.ambient_pressure = ambient_pressure
70+
71+
# cached readings
5972
self._temperature = None
6073
self._relative_humitidy = None
6174
self._co2 = None
6275

76+
def reset(self):
77+
"""Perform a soft reset on the sensor, restoring default values"""
78+
self._send_command(_CMD_SOFT_RESET)
79+
sleep(0.030) # not mentioned by datasheet, but required to avoid IO error
80+
6381
@property
6482
def measurement_interval(self):
6583
"""Sets the interval between readings"""
@@ -72,7 +90,12 @@ def measurement_interval(self, value):
7290

7391
@property
7492
def self_calibration_enabled(self):
75-
"""Enables or disables self calibration"""
93+
"""Enables or disables automatic self calibration (ASC). To work correctly, the sensor must
94+
be on and active for 7 days after enabling ASC, and exposed to fresh air for at least 1 hour
95+
per day. Consult the manufacturer's documentation for more information.
96+
97+
´**NOTE**: Enabling self calibration will override any values set by specifying a
98+
`forced_recalibration_reference`"""
7699
return self._read_register(_CMD_AUTOMATIC_SELF_CALIBRATION) == 1
77100

78101
@self_calibration_enabled.setter
@@ -105,6 +128,30 @@ def _data_available(self):
105128

106129
return self._read_register(_CMD_GET_DATA_READY)
107130

131+
@property
132+
def ambient_pressure(self):
133+
"""Specifies the ambient air pressure at the measurement location in mBar. Setting this
134+
value adjusts the CO2 measurement calculations to account for the air pressure's effect on
135+
readings. Values must be in mBar, from 700 to 1200 mBar"""
136+
return self._read_register(_CMD_CONTINUOUS_MEASUREMENT)
137+
138+
@ambient_pressure.setter
139+
def ambient_pressure(self, pressure_mbar):
140+
if pressure_mbar != 0 and (pressure_mbar > 1200 or pressure_mbar < 700):
141+
raise AttributeError("ambient_pressure must be from 700 to 1200 mBar")
142+
self._send_command(_CMD_CONTINUOUS_MEASUREMENT, pressure_mbar)
143+
144+
@property
145+
def altitude(self):
146+
"""Specifies the altitude at the measurement location in meters above sea level. Setting
147+
this value adjusts the CO2 measurement calculations to account for the air pressure's effect
148+
on readings"""
149+
return self._read_register(_CMD_SET_ALTITUDE_COMPENSATION)
150+
151+
@altitude.setter
152+
def altitude(self, altitude):
153+
self._send_command(_CMD_SET_ALTITUDE_COMPENSATION, altitude)
154+
108155
@property
109156
def temperature_offset(self):
110157
"""Specifies the offset to be added to the reported measurements to account for a bias in
@@ -118,6 +165,19 @@ def temperature_offset(self, offset):
118165

119166
self._send_command(_CMD_SET_TEMPERATURE_OFFSET, int(offset * 100))
120167

168+
@property
169+
def forced_recalibration_reference(self):
170+
"""Specifies the concentration of a reference source of CO2 placed in close proximity to the
171+
sensor. The value must be from 400 to 2000 ppm.
172+
173+
´**NOTE**: Specifying a forced recalibration reference will override any calibration values
174+
set by Automatic Self Calibration"""
175+
return self._read_register(_CMD_SET_FORCED_RECALIBRATION_FACTOR)
176+
177+
@forced_recalibration_reference.setter
178+
def forced_recalibration_reference(self, reference_value):
179+
self._send_command(_CMD_SET_FORCED_RECALIBRATION_FACTOR, reference_value)
180+
121181
def _read_register(self, reg_addr):
122182
self._buffer[0] = reg_addr >> 8
123183
self._buffer[1] = reg_addr & 0xFF

examples/scd30_simpletest.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
print("CO2:", scd.co2, "PPM")
1818
print("Temperature:", scd._temperature, "degrees C")
1919
print("Humidity::", scd._relative_humitidy, "%%rH")
20-
else:
21-
print("no data")
20+
print("")
21+
print("Waiting for new data...")
22+
print("")
23+
2224
time.sleep(0.5)

examples/scd30_tuning_knobs.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,33 @@
99

1010
i2c = busio.I2C(board.SCL, board.SDA)
1111
scd = adafruit_scd30.SCD30(i2c)
12-
scd.temperature_offset = 5.5
12+
# scd.temperature_offset = 5.5
1313
print("Temperature offset:", scd.temperature_offset)
1414

15-
scd.measurement_interval = 4
15+
# scd.measurement_interval = 4
1616
print("Measurement interval:", scd.measurement_interval)
1717

18-
scd.self_calibration_enabled = True
18+
# scd.self_calibration_enabled = True
1919
print("Self-calibration enabled:", scd.self_calibration_enabled)
2020

21-
# getTemperatureOffset(void)
22-
# setAmbientPressure(uint16_t pressure_mbar)
23-
# setAltitudeCompensation(uint16_t altitude)
24-
# setAutoSelfCalibration(enable)
25-
# setForcedRecalibrationFactor(uint16_t concentration)
21+
# scd.ambient_pressure = 1100
22+
print("Ambient Pressure:", scd.ambient_pressure)
2623

24+
# scd.altitude = 1600
25+
print("Altitude:", scd.altitude, "meters above sea level")
26+
27+
# scd.forced_recalibration_reference = 409
28+
print("Forced recalibration reference:", scd.forced_recalibration_reference)
29+
print("")
2730
while True:
2831
data = scd._data_available
2932
if data:
3033
print("Data Available!")
3134
print("CO2:", scd.co2, "PPM")
3235
print("Temperature:", scd._temperature, "degrees C")
3336
print("Humidity::", scd._relative_humitidy, "%%rH")
37+
print("")
38+
print("Waiting for new data...")
39+
print("")
3440

3541
time.sleep(0.5)

0 commit comments

Comments
 (0)