Adafruit CCS811 Arduino Library
Adafruit_CCS811.h
1 #ifndef LIB_ADAFRUIT_CCS811_H
2 #define LIB_ADAFRUIT_CCS811_H
3 
4 #if (ARDUINO >= 100)
5 #include "Arduino.h"
6 #else
7 #include "WProgram.h"
8 #endif
9 
10 #include <Adafruit_I2CDevice.h>
11 
12 /*=========================================================================
13  I2C ADDRESS/BITS
14  -----------------------------------------------------------------------*/
15 #define CCS811_ADDRESS (0x5A)
16 /*=========================================================================*/
17 
18 /*=========================================================================
19  REGISTERS
20  -----------------------------------------------------------------------*/
21 enum {
22  CCS811_STATUS = 0x00,
23  CCS811_MEAS_MODE = 0x01,
24  CCS811_ALG_RESULT_DATA = 0x02,
25  CCS811_RAW_DATA = 0x03,
26  CCS811_ENV_DATA = 0x05,
27  CCS811_NTC = 0x06,
28  CCS811_THRESHOLDS = 0x10,
29  CCS811_BASELINE = 0x11,
30  CCS811_HW_ID = 0x20,
31  CCS811_HW_VERSION = 0x21,
32  CCS811_FW_BOOT_VERSION = 0x23,
33  CCS811_FW_APP_VERSION = 0x24,
34  CCS811_ERROR_ID = 0xE0,
35  CCS811_SW_RESET = 0xFF,
36 };
37 
38 // bootloader registers
39 enum {
40  CCS811_BOOTLOADER_APP_ERASE = 0xF1,
41  CCS811_BOOTLOADER_APP_DATA = 0xF2,
42  CCS811_BOOTLOADER_APP_VERIFY = 0xF3,
43  CCS811_BOOTLOADER_APP_START = 0xF4
44 };
45 
46 enum {
47  CCS811_DRIVE_MODE_IDLE = 0x00,
48  CCS811_DRIVE_MODE_1SEC = 0x01,
49  CCS811_DRIVE_MODE_10SEC = 0x02,
50  CCS811_DRIVE_MODE_60SEC = 0x03,
51  CCS811_DRIVE_MODE_250MS = 0x04,
52 };
53 
54 /*=========================================================================*/
55 
56 #define CCS811_HW_ID_CODE 0x81
57 
58 #define CCS811_REF_RESISTOR 100000
59 
60 /**************************************************************************/
65 /**************************************************************************/
67 public:
68  // constructors
69  Adafruit_CCS811(void){};
70  ~Adafruit_CCS811(void);
71 
72  bool begin(uint8_t addr = CCS811_ADDRESS, TwoWire *theWire = &Wire);
73 
74  void setEnvironmentalData(float humidity, float temperature);
75 
76  uint16_t getBaseline();
77  void setBaseline(uint16_t baseline);
78 
79  // calculate temperature based on the NTC register
80  double calculateTemperature();
81 
82  void setThresholds(uint16_t low_med, uint16_t med_high,
83  uint8_t hysteresis = 50);
84 
85  void SWReset();
86 
87  void setDriveMode(uint8_t mode);
88  void enableInterrupt();
89  void disableInterrupt();
90 
91  /**************************************************************************/
97  /**************************************************************************/
98  uint16_t getTVOC() { return _TVOC; }
99 
100  /**************************************************************************/
106  /**************************************************************************/
107  uint16_t geteCO2() { return _eCO2; }
108 
109  /**************************************************************************/
115  /**************************************************************************/
116  uint16_t getCurrentSelected() { return _currentSelected; }
117 
118  /**************************************************************************/
124  /**************************************************************************/
125  uint16_t getRawADCreading() { return _rawADCreading; }
126 
127  /**************************************************************************/
133  /**************************************************************************/
134  void setTempOffset(float offset) { _tempOffset = offset; }
135 
136  // check if data is available to be read
137  bool available();
138  uint8_t readData();
139 
140  bool checkError();
141 
142 private:
143  Adafruit_I2CDevice *i2c_dev = NULL;
144  float _tempOffset;
145 
146  uint16_t _TVOC;
147  uint16_t _eCO2;
148 
149  uint16_t _currentSelected;
150  uint16_t _rawADCreading;
151 
152  void write8(byte reg, byte value);
153  void write16(byte reg, uint16_t value);
154  uint8_t read8(byte reg);
155 
156  void read(uint8_t reg, uint8_t *buf, uint8_t num);
157  void write(uint8_t reg, uint8_t *buf, uint8_t num);
158 
159  /*=========================================================================
160  REGISTER BITFIELDS
161  -----------------------------------------------------------------------*/
162  // The status register
163  struct status {
164 
165  /* 0: no error
166  * 1: error has occurred
167  */
168  uint8_t ERROR : 1;
169 
170  // reserved : 2
171 
172  /* 0: no samples are ready
173  * 1: samples are ready
174  */
175  uint8_t DATA_READY : 1;
176  uint8_t APP_VALID : 1;
177 
178  // reserved : 2
179 
180  /* 0: boot mode, new firmware can be loaded
181  * 1: application mode, can take measurements
182  */
183  uint8_t FW_MODE : 1;
184 
185  void set(uint8_t data) {
186  ERROR = data & 0x01;
187  DATA_READY = (data >> 3) & 0x01;
188  APP_VALID = (data >> 4) & 0x01;
189  FW_MODE = (data >> 7) & 0x01;
190  }
191  };
192  status _status;
193 
194  // measurement and conditions register
195  struct meas_mode {
196  // reserved : 2
197 
198  /* 0: interrupt mode operates normally
199 * 1: Interrupt mode (if enabled) only asserts the nINT signal (driven low) if
200 the new ALG_RESULT_DATA crosses one of the thresholds set in the THRESHOLDS
201 register by more than the hysteresis value (also in the THRESHOLDS register)
202 */
203  uint8_t INT_THRESH : 1;
204 
205  /* 0: int disabled
206 * 1: The nINT signal is asserted (driven low) when a new sample is ready in
207  ALG_RESULT_DATA. The nINT signal will stop being driven low
208 when ALG_RESULT_DATA is read on the I²C interface.
209 */
210  uint8_t INT_DATARDY : 1;
211 
212  uint8_t DRIVE_MODE : 3;
213 
214  uint8_t get() {
215  return (INT_THRESH << 2) | (INT_DATARDY << 3) | (DRIVE_MODE << 4);
216  }
217  };
218  meas_mode _meas_mode;
219 
220  struct error_id {
221  /* The CCS811 received an I²C write request addressed to this station but
222  with invalid register address ID */
223  uint8_t WRITE_REG_INVALID : 1;
224 
225  /* The CCS811 received an I²C read request to a mailbox ID that is invalid
226  */
227  uint8_t READ_REG_INVALID : 1;
228 
229  /* The CCS811 received an I²C request to write an unsupported mode to
230  MEAS_MODE */
231  uint8_t MEASMODE_INVALID : 1;
232 
233  /* The sensor resistance measurement has reached or exceeded the maximum
234  range */
235  uint8_t MAX_RESISTANCE : 1;
236 
237  /* The Heater current in the CCS811 is not in range */
238  uint8_t HEATER_FAULT : 1;
239 
240  /* The Heater voltage is not being applied correctly */
241  uint8_t HEATER_SUPPLY : 1;
242 
243  void set(uint8_t data) {
244  WRITE_REG_INVALID = data & 0x01;
245  READ_REG_INVALID = (data & 0x02) >> 1;
246  MEASMODE_INVALID = (data & 0x04) >> 2;
247  MAX_RESISTANCE = (data & 0x08) >> 3;
248  HEATER_FAULT = (data & 0x10) >> 4;
249  HEATER_SUPPLY = (data & 0x20) >> 5;
250  }
251  };
252  error_id _error_id;
253 
254  /*=========================================================================*/
255 };
256 
257 #endif
Class that stores state and functions for interacting with CCS811 gas sensor chips.
Definition: Adafruit_CCS811.h:66
void setTempOffset(float offset)
set the temperature compensation offset for the device. This is needed to offset errors in NTC measur...
Definition: Adafruit_CCS811.h:134
void enableInterrupt()
enable the data ready interrupt pin on the device.
Definition: Adafruit_CCS811.cpp:69
void SWReset()
trigger a software reset of the device
Definition: Adafruit_CCS811.cpp:239
void setEnvironmentalData(float humidity, float temperature)
set the humidity and temperature compensation for the sensor.
Definition: Adafruit_CCS811.cpp:134
uint16_t getRawADCreading()
returns the raw ADC reading. This does does not read the sensor. To do so, call readData() ...
Definition: Adafruit_CCS811.h:125
bool checkError()
read the status register and store any errors.
Definition: Adafruit_CCS811.cpp:251
void setBaseline(uint16_t baseline)
set the baseline for the sensor.
Definition: Adafruit_CCS811.cpp:181
void setDriveMode(uint8_t mode)
sample rate of the sensor.
Definition: Adafruit_CCS811.cpp:59
uint16_t getCurrentSelected()
returns the "Current Selected" in uA. This does does not read the sensor. To do so, call readData()
Definition: Adafruit_CCS811.h:116
void disableInterrupt()
disable the data ready interrupt pin on the device
Definition: Adafruit_CCS811.cpp:79
uint16_t getBaseline()
get the current baseline from the sensor.
Definition: Adafruit_CCS811.cpp:163
uint16_t geteCO2()
returns the stored estimated carbon dioxide measurement. This does does not read the sensor...
Definition: Adafruit_CCS811.h:107
bool begin(uint8_t addr=CCS811_ADDRESS, TwoWire *theWire=&Wire)
Setups the I2C interface and hardware and checks for communication.
Definition: Adafruit_CCS811.cpp:17
uint8_t readData()
read and store the sensor data. This data can be accessed with getTVOC(), geteCO2(), getCurrentSelected() and getRawADCreading()
Definition: Adafruit_CCS811.cpp:105
void setThresholds(uint16_t low_med, uint16_t med_high, uint8_t hysteresis=50)
set interrupt thresholds
Definition: Adafruit_CCS811.cpp:225
uint16_t getTVOC()
returns the stored total volatile organic compounds measurement. This does does not read the sensor...
Definition: Adafruit_CCS811.h:98
double calculateTemperature()
calculate the temperature using the onboard NTC resistor.
Definition: Adafruit_CCS811.cpp:198
bool available()
checks if data is available to be read.
Definition: Adafruit_CCS811.cpp:90