ccs811: driver for the AMS CCS811 digital gas sensor

The chip measures the total VOC level of the air and derives an equivalent CO2 value from it. Both numbers come from a calibration algorithm inside the chip, so they are estimates, not a measurement of a specific gas.

Wiring

The chip runs from 1.8 to 3.3 V and uses the same voltage for the I2C pins. The address is 0x5A when ADDR is tied to ground and 0x5B when it is tied to VDD. The bus supports up to 400 kHz.

A module usually adds a 3.3 V regulator and level shifters, and ties WAKE and nRESET low; the driver does not control them. The INT pin is open drain and needs a pull-up if the threshold interrupt is used.

Usage

ccs811_dev_t dev;
ESP_ERROR_CHECK(ccs811_init_desc(&dev, CCS811_I2C_ADDRESS_1, 0,
                                 CONFIG_EXAMPLE_I2C_MASTER_SDA,
                                 CONFIG_EXAMPLE_I2C_MASTER_SCL));
ESP_ERROR_CHECK(ccs811_init(&dev));

uint16_t eco2, tvoc;
while (1)
{
    esp_err_t err = ccs811_get_results(&dev, &tvoc, &eco2, NULL, NULL);
    if (err == ESP_OK)
        printf("eCO2: %d ppm, TVOC: %d ppb\n", eco2, tvoc);
    vTaskDelay(pdMS_TO_TICKS(1000));
}

ccs811_init() checks the hardware ID, performs a software reset, starts the application firmware when the chip boots into the bootloader and leaves the sensor in CCS811_MODE_1S. ccs811_free_desc() only frees the mutex; the I2C bus is shared and belongs to i2cdev.

Measurement modes

Mode

Interval

What is available

CCS811_MODE_IDLE

—

Nothing, the sensor and the heater are off

CCS811_MODE_1S

1 s

eCO2, TVOC, raw data

CCS811_MODE_10S

10 s

eCO2, TVOC, raw data

CCS811_MODE_60S

60 s

eCO2, TVOC, raw data

CCS811_MODE_250MS

250 ms

Raw data only

The 1 s mode draws the most current; the pulse modes trade accuracy for power. In the 250 ms mode the algorithm does not run, so ccs811_get_results() returns CCS811_ERR_NO_IAQ_DATA when eCO2 or TVOC is requested.

When no new sample has arrived since the previous read, the function returns CCS811_ERR_NO_NEW_DATA and leaves the output variables untouched.

Values and calibration

ccs811_get_results() fills any of four outputs, all of which may be NULL: eCO2 in ppm, TVOC in ppb, raw sensor current in microamps (raw_i, 6 bits, 0 to 63) and raw sensor voltage as a 10-bit ADC code (raw_v, 0 to 1023) referenced to the supply voltage.

The first hours of a new sensor are not representative: the datasheet asks for a 48 hour run-in, and the internal baseline settles over the first 20 minutes after every power-up. ccs811_get_baseline() and ccs811_set_baseline() let the application save the baseline, for example once a day, and write it back after a reset instead of waiting again. The baseline is only valid for the same sensor in the same enclosure.

Environmental compensation

ccs811_set_environmental_data() passes the ambient temperature (-25 to 50 °C) and relative humidity (0 to 100 %) to the chip; values outside these ranges are rejected with ESP_ERR_INVALID_ARG. The sensor uses the last value it received, so update it when the room conditions change and send it again after a reset. Without the call the chip assumes 25 °C and 50 %RH.

Threshold interrupt

ccs811_set_eco2_thresholds() takes the low and high limits in ppm (400 to 8192, low strictly below high) and a hysteresis in ppm, programs them into the chip and enables the threshold interrupt. Calling it with all three values zero disables the interrupt. The INT pin is not touched by the driver; reading it is up to the application.

NTC thermistor

ccs811_get_ntc_resistance() reads the NTC conversion result and the reference voltage, and returns the thermistor resistance for the given reference resistor:

R_ntc = V_ntc / V_ref * R_ref

The values are 16-bit ADC codes. A zero reference voltage means that no thermistor is connected, and the call returns ESP_ERR_INVALID_RESPONSE.

Errors

Errors from the chip are reported with the component’s own codes in the 0xA000 range (CCS811_ERR_*): wrong hardware ID, invalid register address, invalid measurement mode, maximum sensor resistance, heater fault and heater supply. esp_err_to_name() does not know them and prints UNKNOWN ERROR; compare the result with the constants instead. Bus errors are standard ESP_ERR_* values.

Example

examples/default initialises the sensor, selects the 1 s mode and prints eCO2 and TVOC every second.

Reference

Defines

CCS811_I2C_ADDRESS_1

< CCS811 I2C addresses

default

CCS811_I2C_ADDRESS_2
CCS811_ERR_BASE

CCS811 driver error codes ORed with error codes for I2C the interface.

CCS811_ERR_BOOT_MODE

firmware is in boot mode

CCS811_ERR_NO_APP

no application firmware loaded

CCS811_ERR_NO_NEW_DATA

no new data samples are ready

CCS811_ERR_NO_IAQ_DATA

no new data samples are ready

CCS811_ERR_HW_ID

wrong hardware ID

CCS811_ERR_INV_SENS

invalid sensor ID

CCS811_ERR_WR_REG_INV

invalid register addr on write

CCS811_ERR_RD_REG_INV

invalid register addr on read

CCS811_ERR_MM_INV

invalid measurement mode

CCS811_ERR_MAX_RESIST

max sensor resistance reached

CCS811_ERR_HEAT_FAULT

heater current not in range

CCS811_ERR_HEAT_SUPPLY

heater voltage not correct

CCS811_ERR_WRONG_MODE

wrong measurement mode

CCS811_ERR_RD_STAT_FAILED

read status register failed

CCS811_ERR_RD_DATA_FAILED

read sensor data failed

CCS811_ERR_APP_START_FAIL

sensor app start failure

CCS811_ERR_WRONG_PARAMS

wrong parameters used

CCS_ECO2_RANGE_MIN
CCS_ECO2_RANGE_MAX
CCS_TVOC_RANGE_MIN
CCS_TVOC_RANGE_MAX

Enums

enum ccs811_mode_t

CCS811 operation modes.

Values:

enumerator CCS811_MODE_IDLE

Idle, low current mode.

enumerator CCS811_MODE_1S

Constant Power mode, IAQ values every 1 s.

enumerator CCS811_MODE_10S

Pulse Heating mode, IAQ values every 10 s.

enumerator CCS811_MODE_60S

Low Power Pulse Heating, IAQ values every 60 s.

enumerator CCS811_MODE_250MS

Constant Power mode, RAW data every 250 ms.

Functions

esp_err_t ccs811_init_desc(ccs811_dev_t *dev, uint8_t addr, i2c_port_t port, gpio_num_t sda_gpio, gpio_num_t scl_gpio)

Initialize device descriptor.

Parameters:
  • dev – Pointer to the sensor device data structure

  • addr – Sensor address

  • port – I2C port number

  • sda_gpio – GPIO pin number for SDA

  • scl_gpio – GPIO pin number for SCL

Returns:

ESP_OK on success

esp_err_t ccs811_free_desc(ccs811_dev_t *dev)

Free device descriptor.

Parameters:

dev – Pointer to the sensor device data structure

Returns:

ESP_OK on success

esp_err_t ccs811_init(ccs811_dev_t *dev)

Initialize a CCS811 sensor.

The function initializes the CCS811 sensor and checks its availability.

Parameters:

dev – Pointer to the sensor device data structure

Returns:

ESP_OK on success

esp_err_t ccs811_set_mode(ccs811_dev_t *dev, ccs811_mode_t mode)

Set the operation mode of the sensor.

The function sets the operating mode of the sensor. If the parameter mode is either CCS811_MODE_1S, CCS811_MODE_10S, CCS811_MODE_60S or CCS811_MODE_250MS, the sensor starts a periodic measurement with the specified period. Function ccs811_get_results() can then be used at the same rate to get the results.

In CCS811_MODE_1S, CCS811_MODE_10S and CCS811_MODE_60S, raw sensor data as well as IAQ values calculated by the sensor values are available. In CCS811_MODE_250MS, only raw data are available.

In case, parameter mode is CCS811_MODE_IDLE, the sensor does not perform any measurements.

Please note: Mode timings are subject to typical 2% tolerance due to accuracy of internal sensor clock.

Please note: After setting the sensor mode, the sensor needs up to 20 minutes, before accurate readings are generated.

Please note: When a sensor operating mode is changed to a new mode with a lower sample rate, e.g., from CCS811_MODE_60S to CCS811_MODE_1S, it should be placed in mode_idle for at least 10 minutes before enabling the new mode.

Parameters:
  • dev – Pointer to the sensor device data structure

  • mode – Operation mode of the sensor

Returns:

ESP_OK on success

esp_err_t ccs811_get_results(ccs811_dev_t *dev, uint16_t *iaq_tvoc, uint16_t *iaq_eco2, uint8_t *raw_i, uint16_t *raw_v)

Get latest IAQ sensor values and/or RAW sensor data.

The function reads the IAQ sensor values (TVOC and eCO2) and/or the raw sensor data. If some of the results are not needed, the corresponding pointer parameters can be set to NULL.

Please note: If the function is called and no new data are available, e.g., due to the sensor mode time tolerance of 2%, the function still returns successfully. In this case, the results of the last measurement are returned and the error code CCS811_ERR_NO_NEW_DATA is set.

Please note: In CCS811_MODE_250MS, only RAW data are available. In that case, the function fails with error_code CCS811_ERR_NO_IAQ_DATA if parameters iaq_tvoc and iaq_eco2 are not NULL.

Parameters:
  • dev – pointer to the sensor device data structure

  • iaq_tvoc – TVOC total volatile organic compound (0 - 1187 ppb)

  • iaq_eco2 – eCO2 equivalent CO2 (400 - 8192 ppm)

  • raw_i – current through the sensor used for measuring (0 - 63 uA)

  • raw_v – voltage across the sensor measured (0 - 1023 = 1.65 V)

Returns:

ESP_OK on success

esp_err_t ccs811_get_ntc_resistance(ccs811_dev_t *dev, uint32_t r_ref, uint32_t *res)

Get the resistance of connected NTC thermistor.

CCS811 supports an external interface for connecting a negative thermal coefficient thermistor (R_NTC) to provide a cost effective and power efficient means of calculating the local ambient temperature. The sensor measures the voltage V_NTC across the R_NTC as well as the voltage V_REF across a connected reference resistor (R_REF). The function returns the current resistance of R_NTC using the equation

     R_NTC = R_REF / V_REF * V_NTC
Using the data sheet of the NTC, the ambient temperature can be calculated.

Parameters:
  • dev – pointer to the sensor device data structure

  • r_ref – resistance of R_REF in Ohm

  • res – [out] resistance of R_NTC in Ohm

Returns:

ESP_OK on success

esp_err_t ccs811_set_environmental_data(ccs811_dev_t *dev, float temperature, float humidity)

Set environmental data.

If information about the environment are available from another sensor, they can be used by CCS811 to compensate gas readings due to temperature and humidity changes.

Parameters:
  • dev – pointer to the sensor device data structure

  • temperature – measured temperature in degree Celsius

  • humidity – measured relative humidity in percent

Returns:

ESP_OK on success

esp_err_t ccs811_enable_interrupt(ccs811_dev_t *dev, bool enabled)

Enable or disable data ready interrupt signal nINT

At the end of each measurement cycle (250ms, 1s, 10s, 60s), CCS811 can optionally trigger an interrupt. The signal nINT is driven low as soon as new sensor values are ready to read. It will stop being driven low when sensor data are read with function ccs811_get_results.

The interrupt is disabled by default.

Parameters:
  • dev – pointer to the sensor device data structure

  • enabled – if true, the interrupt is enabled, or disabled otherwise

Returns:

ESP_OK on success

esp_err_t ccs811_set_eco2_thresholds(ccs811_dev_t *dev, uint16_t low, uint16_t high, uint8_t hysteresis)

Set eCO2 threshold mode for data ready interrupts.

The user task can choose that the data ready interrupt is not generated every time when new sensor values become ready but only if the eCO2 value moves from the current range (LOW, MEDIUM, or HIGH) into another range by more than a hysteresis value. Hysteresis is used to prevent multiple interrupts close to a threshold.

  • LOW below parameter value low

  • MEDIUM between parameter values low and high

  • HIGH above parameter value high is range HIGH.

If all parameters have valid values, the function sets the thresholds and enables the data ready interrupt. Using 0 for all parameters disables the interrupt.

The interrupt is disabled by default.

Parameters:
  • dev – pointer to the sensor device data structure

  • low – threshold LOW to MEDIUM (>= 400, default 1500)

  • high – threshold MEDIUM to HIGH (<= 8192, default 2500)

  • hysteresis – hysteresis value (default 50)

Returns:

ESP_OK on success

esp_err_t ccs811_get_baseline(ccs811_dev_t *dev, uint16_t *baseline)

Get the current baseline value from sensor.

The sensor supports automatic baseline correction over a minimum time of 24 hours. Using this function, the current baseline value can be saved before the sensor is powered down. This baseline can then be restored after sensor is powered up again to continue the automatic baseline process.

Parameters:
  • dev – pointer to the sensor device data structure

  • baseline – [out] current baseline value on success, or 0 on error

Returns:

ESP_OK on success

esp_err_t ccs811_set_baseline(ccs811_dev_t *dev, uint16_t baseline)

Write a previously stored baseline value to the sensor.

The sensor supports automatic baseline correction over a minimum time of 24 hours. Using this function, a previously saved baseline value be restored after the sensor is powered up to continue the automatic baseline process.

Please note: The baseline must be written after the conditioning period of 20 min after power up.

Parameters:
  • dev – pointer to the sensor device data structure

  • baseline – baseline to be set

Returns:

ESP_OK on success

struct ccs811_dev_t
#include <ccs811.h>

CCS811 sensor device data structure.

Public Members

i2c_dev_t i2c_dev

I2C device handle.

ccs811_mode_t mode

operation mode