/ adafruit_ccs811.py
adafruit_ccs811.py
  1  # The MIT License (MIT)
  2  #
  3  # Copyright (c) 2017 Dean Miller for Adafruit Industries.
  4  #
  5  # Permission is hereby granted, free of charge, to any person obtaining a copy
  6  # of this software and associated documentation files (the "Software"), to deal
  7  # in the Software without restriction, including without limitation the rights
  8  # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9  # copies of the Software, and to permit persons to whom the Software is
 10  # furnished to do so, subject to the following conditions:
 11  #
 12  # The above copyright notice and this permission notice shall be included in
 13  # all copies or substantial portions of the Software.
 14  #
 15  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16  # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17  # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18  # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19  # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 20  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 21  # THE SOFTWARE.
 22  
 23  """
 24  `CCS811` - Adafruit CCS811 Air Quality Sensor Breakout - VOC and eCO2
 25  ======================================================================
 26  This library supports the use of the CCS811 air quality sensor in CircuitPython.
 27  
 28  Author(s): Dean Miller for Adafruit Industries
 29  
 30  **Notes:**
 31  
 32  #. `Datasheet
 33  <https://cdn-learn.adafruit.com/assets/assets/000/044/636/original/CCS811_DS000459_2-00-1098798.pdf?1501602769>`_
 34  """
 35  import time
 36  import math
 37  import struct
 38  
 39  from micropython import const
 40  from adafruit_bus_device.i2c_device import I2CDevice
 41  from adafruit_register import i2c_bit
 42  from adafruit_register import i2c_bits
 43  
 44  __version__ = "0.0.0-auto.0"
 45  __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_CCS811.git"
 46  
 47  
 48  _ALG_RESULT_DATA = const(0x02)
 49  _RAW_DATA = const(0x03)
 50  _ENV_DATA = const(0x05)
 51  _NTC = const(0x06)
 52  _THRESHOLDS = const(0x10)
 53  
 54  _BASELINE = const(0x11)
 55  
 56  # _HW_ID = 0x20
 57  # _HW_VERSION = 0x21
 58  # _FW_BOOT_VERSION = 0x23
 59  # _FW_APP_VERSION = 0x24
 60  # _ERROR_ID = 0xE0
 61  
 62  _SW_RESET = const(0xFF)
 63  
 64  # _BOOTLOADER_APP_ERASE = 0xF1
 65  # _BOOTLOADER_APP_DATA = 0xF2
 66  # _BOOTLOADER_APP_VERIFY = 0xF3
 67  # _BOOTLOADER_APP_START = 0xF4
 68  
 69  DRIVE_MODE_IDLE = const(0x00)
 70  DRIVE_MODE_1SEC = const(0x01)
 71  DRIVE_MODE_10SEC = const(0x02)
 72  DRIVE_MODE_60SEC = const(0x03)
 73  DRIVE_MODE_250MS = const(0x04)
 74  
 75  _HW_ID_CODE = const(0x81)
 76  _REF_RESISTOR = const(100000)
 77  
 78  
 79  class CCS811:
 80      """CCS811 gas sensor driver.
 81  
 82      :param ~busio.I2C i2c: The I2C bus.
 83      :param int addr: The I2C address of the CCS811.
 84      """
 85  
 86      # set up the registers
 87      error = i2c_bit.ROBit(0x00, 0)
 88      """True when an error has occured."""
 89      data_ready = i2c_bit.ROBit(0x00, 3)
 90      """True when new data has been read."""
 91      app_valid = i2c_bit.ROBit(0x00, 4)
 92      fw_mode = i2c_bit.ROBit(0x00, 7)
 93  
 94      hw_id = i2c_bits.ROBits(8, 0x20, 0)
 95  
 96      int_thresh = i2c_bit.RWBit(0x01, 2)
 97      interrupt_enabled = i2c_bit.RWBit(0x01, 3)
 98      drive_mode = i2c_bits.RWBits(3, 0x01, 4)
 99  
100      temp_offset = 0.0
101      """Temperature offset."""
102  
103      def __init__(self, i2c_bus, address=0x5A):
104          self.i2c_device = I2CDevice(i2c_bus, address)
105  
106          # check that the HW id is correct
107          if self.hw_id != _HW_ID_CODE:
108              raise RuntimeError(
109                  "Device ID returned is not correct! Please check your wiring."
110              )
111          # try to start the app
112          buf = bytearray(1)
113          buf[0] = 0xF4
114          with self.i2c_device as i2c:
115              i2c.write(buf, end=1)
116          time.sleep(0.1)
117  
118          # make sure there are no errors and we have entered application mode
119          if self.error:
120              raise RuntimeError(
121                  "Device returned a error! Try removing and reapplying power to "
122                  "the device and running the code again."
123              )
124          if not self.fw_mode:
125              raise RuntimeError(
126                  "Device did not enter application mode! If you got here, there may "
127                  "be a problem with the firmware on your sensor."
128              )
129  
130          self.interrupt_enabled = False
131  
132          # default to read every second
133          self.drive_mode = DRIVE_MODE_1SEC
134  
135          self._eco2 = None  # pylint: disable=invalid-name
136          self._tvoc = None  # pylint: disable=invalid-name
137  
138      @property
139      def error_code(self):
140          """Error code"""
141          buf = bytearray(2)
142          buf[0] = 0xE0
143          with self.i2c_device as i2c:
144              i2c.write_then_readinto(buf, buf, out_end=1, in_start=1)
145          return buf[1]
146  
147      def _update_data(self):
148          if self.data_ready:
149              buf = bytearray(9)
150              buf[0] = _ALG_RESULT_DATA
151              with self.i2c_device as i2c:
152                  i2c.write_then_readinto(buf, buf, out_end=1, in_start=1)
153  
154              self._eco2 = (buf[1] << 8) | (buf[2])
155              self._tvoc = (buf[3] << 8) | (buf[4])
156  
157              if self.error:
158                  raise RuntimeError("Error:" + str(self.error_code))
159  
160      @property
161      def baseline(self):
162          """
163          The propery reads and returns the current baseline value.
164          The returned value is packed into an integer.
165          Later the same integer can be used in order
166          to set a new baseline.
167          """
168          buf = bytearray(3)
169          buf[0] = _BASELINE
170          with self.i2c_device as i2c:
171              i2c.write_then_readinto(buf, buf, out_end=1, in_start=1)
172          return struct.unpack("<H", buf[1:])[0]
173  
174      @baseline.setter
175      def baseline(self, baseline_int):
176          """
177          The property lets you set a new baseline. As a value accepts
178          integer which represents packed baseline 2 bytes value.
179          """
180          buf = bytearray(3)
181          buf[0] = _BASELINE
182          struct.pack_into("<H", buf, 1, baseline_int)
183          with self.i2c_device as i2c:
184              i2c.write(buf)
185  
186      @property
187      def tvoc(self):  # pylint: disable=invalid-name
188          """Total Volatile Organic Compound in parts per billion."""
189          self._update_data()
190          return self._tvoc
191  
192      @property
193      def eco2(self):  # pylint: disable=invalid-name
194          """Equivalent Carbon Dioxide in parts per million. Clipped to 400 to 8192ppm."""
195          self._update_data()
196          return self._eco2
197  
198      @property
199      def temperature(self):
200          """
201          .. deprecated:: 1.1.5
202             Hardware support removed by vendor
203  
204          Temperature based on optional thermistor in Celsius."""
205          buf = bytearray(5)
206          buf[0] = _NTC
207          with self.i2c_device as i2c:
208              i2c.write_then_readinto(buf, buf, out_end=1, in_start=1)
209  
210          vref = (buf[1] << 8) | buf[2]
211          vntc = (buf[3] << 8) | buf[4]
212  
213          # From ams ccs811 app note 000925
214          # https://download.ams.com/content/download/9059/13027/version/1/file/CCS811_Doc_cAppNote-Connecting-NTC-Thermistor_AN000372_v1..pdf
215          rntc = float(vntc) * _REF_RESISTOR / float(vref)
216  
217          ntc_temp = math.log(rntc / 10000.0)
218          ntc_temp /= 3380.0
219          ntc_temp += 1.0 / (25 + 273.15)
220          ntc_temp = 1.0 / ntc_temp
221          ntc_temp -= 273.15
222          return ntc_temp - self.temp_offset
223  
224      def set_environmental_data(self, humidity, temperature):
225          """Set the temperature and humidity used when computing eCO2 and TVOC values.
226  
227          :param int humidity: The current relative humidity in percent.
228          :param float temperature: The current temperature in Celsius."""
229          # Humidity is stored as an unsigned 16 bits in 1/512%RH. The default
230          # value is 50% = 0x64, 0x00. As an example 48.5% humidity would be 0x61,
231          # 0x00.
232          humidity = int(humidity * 512)
233  
234          # Temperature is stored as an unsigned 16 bits integer in 1/512 degrees
235          # there is an offset: 0 maps to -25C. The default value is 25C = 0x64,
236          # 0x00. As an example 23.5% temperature would be 0x61, 0x00.
237          temperature = int((temperature + 25) * 512)
238  
239          buf = bytearray(5)
240          buf[0] = _ENV_DATA
241          struct.pack_into(">HH", buf, 1, humidity, temperature)
242  
243          with self.i2c_device as i2c:
244              i2c.write(buf)
245  
246      def set_interrupt_thresholds(self, low_med, med_high, hysteresis):
247          """Set the thresholds used for triggering the interrupt based on eCO2.
248          The interrupt is triggered when the value crossed a boundary value by the
249          minimum hysteresis value.
250  
251          :param int low_med: Boundary between low and medium ranges
252          :param int med_high: Boundary between medium and high ranges
253          :param int hysteresis: Minimum difference between reads"""
254          buf = bytearray(
255              [
256                  _THRESHOLDS,
257                  ((low_med >> 8) & 0xF),
258                  (low_med & 0xF),
259                  ((med_high >> 8) & 0xF),
260                  (med_high & 0xF),
261                  hysteresis,
262              ]
263          )
264          with self.i2c_device as i2c:
265              i2c.write(buf)
266  
267      def reset(self):
268          """Initiate a software reset."""
269          # reset sequence from the datasheet
270          seq = bytearray([_SW_RESET, 0x11, 0xE5, 0x72, 0x8A])
271          with self.i2c_device as i2c:
272              i2c.write(seq)