Skip to content
esp-idf-libPublic

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Repository files navigation

esp-idf-lib/ccs811

Build examples Build docs Validation

ESP-IDF 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.

Features

  • eCO2 from 400 to 8192 ppm and TVOC from 0 to 1187 ppb.
  • Raw sensor current and voltage for the calibration algorithm.
  • Four measurement modes, from one sample per second to one per minute.
  • Environmental compensation with external temperature and humidity.
  • eCO2 threshold interrupt with hysteresis.
  • Baseline save and restore across power cycles.
  • Reading of the external NTC thermistor.

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

#include <ccs811.h>

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),
  • 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

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

Installation

idf.py add-dependency esp-idf-lib/ccs811

Support

For questions and discussions about the component, please use Discussions at esp-idf-lib/core.

Contributing

Please read CONTRIBUTING.md at esp-idf-lib/core.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages