28
28
"""
29
29
30
30
# imports
31
+ from time import sleep
31
32
from struct import unpack_from , unpack
32
33
import adafruit_bus_device .i2c_device as i2c_device
33
34
from micropython import const
34
35
36
+
35
37
__version__ = "0.0.0-auto.0"
36
38
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_SCD30.git"
37
39
SCD30_DEFAULT_ADDR = 0x61
44
46
_CMD_SET_FORCED_RECALIBRATION_FACTOR = const (0x5204 )
45
47
_CMD_SET_TEMPERATURE_OFFSET = const (0x5403 )
46
48
_CMD_SET_ALTITUDE_COMPENSATION = const (0x5102 )
49
+ _CMD_SOFT_RESET = const (0xD304 )
47
50
48
51
49
52
class SCD30 :
50
53
"""CircuitPython helper class for using the SCD30 e-CO2 sensor"""
51
54
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
+
53
60
self .i2c_device = i2c_device .I2CDevice (i2c_bus , address )
54
61
self ._buffer = bytearray (18 )
55
62
self ._crc_buffer = bytearray (2 )
56
- self ._send_command (_CMD_CONTINUOUS_MEASUREMENT , 0 )
63
+
64
+ self .reset ()
65
+
57
66
self .measurement_interval = 2
58
67
self .self_calibration_enabled = True
68
+ # sets ambient pressure and starts continuous measurements
69
+ self .ambient_pressure = ambient_pressure
70
+
71
+ # cached readings
59
72
self ._temperature = None
60
73
self ._relative_humitidy = None
61
74
self ._co2 = None
62
75
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
+
63
81
@property
64
82
def measurement_interval (self ):
65
83
"""Sets the interval between readings"""
@@ -72,7 +90,12 @@ def measurement_interval(self, value):
72
90
73
91
@property
74
92
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`"""
76
99
return self ._read_register (_CMD_AUTOMATIC_SELF_CALIBRATION ) == 1
77
100
78
101
@self_calibration_enabled .setter
@@ -105,6 +128,30 @@ def _data_available(self):
105
128
106
129
return self ._read_register (_CMD_GET_DATA_READY )
107
130
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
+
108
155
@property
109
156
def temperature_offset (self ):
110
157
"""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):
118
165
119
166
self ._send_command (_CMD_SET_TEMPERATURE_OFFSET , int (offset * 100 ))
120
167
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
+
121
181
def _read_register (self , reg_addr ):
122
182
self ._buffer [0 ] = reg_addr >> 8
123
183
self ._buffer [1 ] = reg_addr & 0xFF
0 commit comments