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  {
23  CCS811_STATUS = 0x00,
24  CCS811_MEAS_MODE = 0x01,
25  CCS811_ALG_RESULT_DATA = 0x02,
26  CCS811_RAW_DATA = 0x03,
27  CCS811_ENV_DATA = 0x05,
28  CCS811_NTC = 0x06,
29  CCS811_THRESHOLDS = 0x10,
30  CCS811_BASELINE = 0x11,
31  CCS811_HW_ID = 0x20,
32  CCS811_HW_VERSION = 0x21,
33  CCS811_FW_BOOT_VERSION = 0x23,
34  CCS811_FW_APP_VERSION = 0x24,
35  CCS811_ERROR_ID = 0xE0,
36  CCS811_SW_RESET = 0xFF,
37  };
38 
39  //bootloader registers
40  enum
41  {
42  CCS811_BOOTLOADER_APP_ERASE = 0xF1,
43  CCS811_BOOTLOADER_APP_DATA = 0xF2,
44  CCS811_BOOTLOADER_APP_VERIFY = 0xF3,
45  CCS811_BOOTLOADER_APP_START = 0xF4
46  };
47 
48  enum
49  {
50  CCS811_DRIVE_MODE_IDLE = 0x00,
51  CCS811_DRIVE_MODE_1SEC = 0x01,
52  CCS811_DRIVE_MODE_10SEC = 0x02,
53  CCS811_DRIVE_MODE_60SEC = 0x03,
54  CCS811_DRIVE_MODE_250MS = 0x04,
55  };
56 
57 /*=========================================================================*/
58 
59 #define CCS811_HW_ID_CODE 0x81
60 
61 #define CCS811_REF_RESISTOR 100000
62 
63 /**************************************************************************/
67 /**************************************************************************/
69  public:
70  //constructors
71  Adafruit_CCS811(void) {};
72  ~Adafruit_CCS811(void) {};
73 
74  bool begin(uint8_t addr = CCS811_ADDRESS);
75 
76  void setEnvironmentalData(uint8_t humidity, double temperature);
77 
78  //calculate temperature based on the NTC register
79  double calculateTemperature();
80 
81  void setThresholds(uint16_t low_med, uint16_t med_high, uint8_t hysteresis = 50);
82 
83  void SWReset();
84 
85  void setDriveMode(uint8_t mode);
86  void enableInterrupt();
87  void disableInterrupt();
88 
89  /**************************************************************************/
94  /**************************************************************************/
95  uint16_t getTVOC() { return _TVOC; }
96 
97  /**************************************************************************/
102  /**************************************************************************/
103  uint16_t geteCO2() { return _eCO2; }
104 
105  /**************************************************************************/
110  /**************************************************************************/
111  void setTempOffset(float offset) { _tempOffset = offset; }
112 
113  //check if data is available to be read
114  bool available();
115  uint8_t readData();
116 
117  bool checkError();
118 
119  private:
120  uint8_t _i2caddr;
121  float _tempOffset;
122 
123  uint16_t _TVOC;
124  uint16_t _eCO2;
125 
126  void write8(byte reg, byte value);
127  void write16(byte reg, uint16_t value);
128  uint8_t read8(byte reg);
129 
130  void read(uint8_t reg, uint8_t *buf, uint8_t num);
131  void write(uint8_t reg, uint8_t *buf, uint8_t num);
132  void _i2c_init();
133 
134 /*=========================================================================
135  REGISTER BITFIELDS
136  -----------------------------------------------------------------------*/
137  // The status register
138  struct status {
139 
140  /* 0: no error
141  * 1: error has occurred
142  */
143  uint8_t ERROR: 1;
144 
145  // reserved : 2
146 
147  /* 0: no samples are ready
148  * 1: samples are ready
149  */
150  uint8_t DATA_READY: 1;
151  uint8_t APP_VALID: 1;
152 
153  // reserved : 2
154 
155  /* 0: boot mode, new firmware can be loaded
156  * 1: application mode, can take measurements
157  */
158  uint8_t FW_MODE: 1;
159 
160  void set(uint8_t data){
161  ERROR = data & 0x01;
162  DATA_READY = (data >> 3) & 0x01;
163  APP_VALID = (data >> 4) & 0x01;
164  FW_MODE = (data >> 7) & 0x01;
165  }
166  };
167  status _status;
168 
169  //measurement and conditions register
170  struct meas_mode {
171  // reserved : 2
172 
173  /* 0: interrupt mode operates normally
174  * 1: Interrupt mode (if enabled) only asserts the nINT signal (driven low) if the new
175  ALG_RESULT_DATA crosses one of the thresholds set in the THRESHOLDS register
176  by more than the hysteresis value (also in the THRESHOLDS register)
177  */
178  uint8_t INT_THRESH: 1;
179 
180  /* 0: int disabled
181  * 1: The nINT signal is asserted (driven low) when a new sample is ready in
182  ALG_RESULT_DATA. The nINT signal will stop being driven low when
183  ALG_RESULT_DATA is read on the I²C interface.
184  */
185  uint8_t INT_DATARDY: 1;
186 
187  uint8_t DRIVE_MODE: 3;
188 
189  uint8_t get(){
190  return (INT_THRESH << 2) | (INT_DATARDY << 3) | (DRIVE_MODE << 4);
191  }
192  };
193  meas_mode _meas_mode;
194 
195  struct error_id {
196  /* The CCS811 received an I²C write request addressed to this station but with
197  invalid register address ID */
198  uint8_t WRITE_REG_INVALID: 1;
199 
200  /* The CCS811 received an I²C read request to a mailbox ID that is invalid */
201  uint8_t READ_REG_INVALID: 1;
202 
203  /* The CCS811 received an I²C request to write an unsupported mode to
204  MEAS_MODE */
205  uint8_t MEASMODE_INVALID: 1;
206 
207  /* The sensor resistance measurement has reached or exceeded the maximum
208  range */
209  uint8_t MAX_RESISTANCE: 1;
210 
211  /* The Heater current in the CCS811 is not in range */
212  uint8_t HEATER_FAULT: 1;
213 
214  /* The Heater voltage is not being applied correctly */
215  uint8_t HEATER_SUPPLY: 1;
216 
217  void set(uint8_t data){
218  WRITE_REG_INVALID = data & 0x01;
219  READ_REG_INVALID = (data & 0x02) >> 1;
220  MEASMODE_INVALID = (data & 0x04) >> 2;
221  MAX_RESISTANCE = (data & 0x08) >> 3;
222  HEATER_FAULT = (data & 0x10) >> 4;
223  HEATER_SUPPLY = (data & 0x20) >> 5;
224  }
225  };
226  error_id _error_id;
227 
228 /*=========================================================================*/
229 };
230 
231 #endif
bool begin(uint8_t addr=CCS811_ADDRESS)
Setups the I2C interface and hardware and checks for communication.
Definition: Adafruit_CCS811.cpp:10
Class that stores state and functions for interacting with CCS811 gas sensor chips.
Definition: Adafruit_CCS811.h:68
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:111
void enableInterrupt()
enable the data ready interrupt pin on the device.
Definition: Adafruit_CCS811.cpp:55
void SWReset()
trigger a software reset of the device
Definition: Adafruit_CCS811.cpp:194
bool checkError()
read the status register and store any errors.
Definition: Adafruit_CCS811.cpp:207
void setDriveMode(uint8_t mode)
sample rate of the sensor.
Definition: Adafruit_CCS811.cpp:44
void disableInterrupt()
disable the data ready interrupt pin on the device
Definition: Adafruit_CCS811.cpp:66
uint16_t geteCO2()
returns the stored estimated carbon dioxide measurement. This does does not read the sensor...
Definition: Adafruit_CCS811.h:103
void setEnvironmentalData(uint8_t humidity, double temperature)
set the humidity and temperature compensation for the sensor.
Definition: Adafruit_CCS811.cpp:117
uint8_t readData()
read and store the sensor data. This data can be accessed with getTVOC() and geteCO2() ...
Definition: Adafruit_CCS811.cpp:92
void setThresholds(uint16_t low_med, uint16_t med_high, uint8_t hysteresis=50)
set interrupt thresholds
Definition: Adafruit_CCS811.cpp:181
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:152
bool available()
checks if data is available to be read.
Definition: Adafruit_CCS811.cpp:78