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 <Wire.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);
73 
74  void setEnvironmentalData(uint8_t humidity, double temperature);
75 
76  // calculate temperature based on the NTC register
77  double calculateTemperature();
78 
79  void setThresholds(uint16_t low_med, uint16_t med_high,
80  uint8_t hysteresis = 50);
81 
82  void SWReset();
83 
84  void setDriveMode(uint8_t mode);
85  void enableInterrupt();
86  void disableInterrupt();
87 
88  /**************************************************************************/
94  /**************************************************************************/
95  uint16_t getTVOC() { return _TVOC; }
96 
97  /**************************************************************************/
103  /**************************************************************************/
104  uint16_t geteCO2() { return _eCO2; }
105 
106  /**************************************************************************/
112  /**************************************************************************/
113  void setTempOffset(float offset) { _tempOffset = offset; }
114 
115  // check if data is available to be read
116  bool available();
117  uint8_t readData();
118 
119  bool checkError();
120 
121 private:
122  uint8_t _i2caddr;
123  float _tempOffset;
124 
125  uint16_t _TVOC;
126  uint16_t _eCO2;
127 
128  void write8(byte reg, byte value);
129  void write16(byte reg, uint16_t value);
130  uint8_t read8(byte reg);
131 
132  void read(uint8_t reg, uint8_t *buf, uint8_t num);
133  void write(uint8_t reg, uint8_t *buf, uint8_t num);
134  void _i2c_init();
135 
136  /*=========================================================================
137  REGISTER BITFIELDS
138  -----------------------------------------------------------------------*/
139  // The status register
140  struct status {
141 
142  /* 0: no error
143  * 1: error has occurred
144  */
145  uint8_t ERROR : 1;
146 
147  // reserved : 2
148 
149  /* 0: no samples are ready
150  * 1: samples are ready
151  */
152  uint8_t DATA_READY : 1;
153  uint8_t APP_VALID : 1;
154 
155  // reserved : 2
156 
157  /* 0: boot mode, new firmware can be loaded
158  * 1: application mode, can take measurements
159  */
160  uint8_t FW_MODE : 1;
161 
162  void set(uint8_t data) {
163  ERROR = data & 0x01;
164  DATA_READY = (data >> 3) & 0x01;
165  APP_VALID = (data >> 4) & 0x01;
166  FW_MODE = (data >> 7) & 0x01;
167  }
168  };
169  status _status;
170 
171  // measurement and conditions register
172  struct meas_mode {
173  // reserved : 2
174 
175  /* 0: interrupt mode operates normally
176 * 1: Interrupt mode (if enabled) only asserts the nINT signal (driven low) if
177 the new ALG_RESULT_DATA crosses one of the thresholds set in the THRESHOLDS
178 register by more than the hysteresis value (also in the THRESHOLDS register)
179 */
180  uint8_t INT_THRESH : 1;
181 
182  /* 0: int disabled
183 * 1: The nINT signal is asserted (driven low) when a new sample is ready in
184  ALG_RESULT_DATA. The nINT signal will stop being driven low
185 when ALG_RESULT_DATA is read on the I²C interface.
186 */
187  uint8_t INT_DATARDY : 1;
188 
189  uint8_t DRIVE_MODE : 3;
190 
191  uint8_t get() {
192  return (INT_THRESH << 2) | (INT_DATARDY << 3) | (DRIVE_MODE << 4);
193  }
194  };
195  meas_mode _meas_mode;
196 
197  struct error_id {
198  /* The CCS811 received an I²C write request addressed to this station but
199  with invalid register address ID */
200  uint8_t WRITE_REG_INVALID : 1;
201 
202  /* The CCS811 received an I²C read request to a mailbox ID that is invalid
203  */
204  uint8_t READ_REG_INVALID : 1;
205 
206  /* The CCS811 received an I²C request to write an unsupported mode to
207  MEAS_MODE */
208  uint8_t MEASMODE_INVALID : 1;
209 
210  /* The sensor resistance measurement has reached or exceeded the maximum
211  range */
212  uint8_t MAX_RESISTANCE : 1;
213 
214  /* The Heater current in the CCS811 is not in range */
215  uint8_t HEATER_FAULT : 1;
216 
217  /* The Heater voltage is not being applied correctly */
218  uint8_t HEATER_SUPPLY : 1;
219 
220  void set(uint8_t data) {
221  WRITE_REG_INVALID = data & 0x01;
222  READ_REG_INVALID = (data & 0x02) >> 1;
223  MEASMODE_INVALID = (data & 0x04) >> 2;
224  MAX_RESISTANCE = (data & 0x08) >> 3;
225  HEATER_FAULT = (data & 0x10) >> 4;
226  HEATER_SUPPLY = (data & 0x20) >> 5;
227  }
228  };
229  error_id _error_id;
230 
231  /*=========================================================================*/
232 };
233 
234 #endif
bool begin(uint8_t addr=CCS811_ADDRESS)
Setups the I2C interface and hardware and checks for communication.
Definition: Adafruit_CCS811.cpp:11
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:113
void enableInterrupt()
enable the data ready interrupt pin on the device.
Definition: Adafruit_CCS811.cpp:57
void SWReset()
trigger a software reset of the device
Definition: Adafruit_CCS811.cpp:196
bool checkError()
read the status register and store any errors.
Definition: Adafruit_CCS811.cpp:208
void setDriveMode(uint8_t mode)
sample rate of the sensor.
Definition: Adafruit_CCS811.cpp:47
void disableInterrupt()
disable the data ready interrupt pin on the device
Definition: Adafruit_CCS811.cpp:67
uint16_t geteCO2()
returns the stored estimated carbon dioxide measurement. This does does not read the sensor...
Definition: Adafruit_CCS811.h:104
void setEnvironmentalData(uint8_t humidity, double temperature)
set the humidity and temperature compensation for the sensor.
Definition: Adafruit_CCS811.cpp:120
uint8_t readData()
read and store the sensor data. This data can be accessed with getTVOC() and geteCO2() ...
Definition: Adafruit_CCS811.cpp:93
void setThresholds(uint16_t low_med, uint16_t med_high, uint8_t hysteresis=50)
set interrupt thresholds
Definition: Adafruit_CCS811.cpp:182
uint16_t getTVOC()
returns the stored total volatile organic compounds measurement. This does does not read the sensor...
Definition: Adafruit_CCS811.h:95
double calculateTemperature()
calculate the temperature using the onboard NTC resistor.
Definition: Adafruit_CCS811.cpp:155
bool available()
checks if data is available to be read.
Definition: Adafruit_CCS811.cpp:78