Device Driver for AMS CCS811 digital gas sensor. More...
Device Driver for AMS CCS811 digital gas sensor.
Definition in file ccs811.h.
Go to the source code of this file.
Data Structures | |
struct | ccs811_params_t |
CCS811 device initialization parameters. More... | |
struct | ccs811_t |
CCS811 sensor device data structure. More... | |
Enumerations | |
enum | ccs811_error_codes_t { CCS811_OK, CCS811_ERROR_I2C, CCS811_ERROR_NO_DEV, CCS811_ERROR_NO_APP, CCS811_ERROR_NO_NEW_DATA, CCS811_ERROR_NO_IAQ_DATA, CCS811_ERROR_WRITE_REG_INV, CCS811_ERROR_READ_REG_INV, CCS811_ERROR_MEASMODE_INV, CCS811_ERROR_THRESH_INV, CCS811_ERROR_MAX_RESISTANCE, CCS811_ERROR_HEATER_FAULT, CCS811_ERROR_HEATER_SUPPLY, CCS811_ERROR_NO_INT_PIN, CCS811_ERROR_NO_WAKE_PIN, CCS811_ERROR_NO_RESET_PIN, CCS811_ERROR_NOT_SUPPORTED } |
Driver error codes (returned as negative values) More... | |
enum | ccs811_mode_t { CCS811_MODE_IDLE = 0, CCS811_MODE_1S = 1, CCS811_MODE_10S = 2, CCS811_MODE_60S = 3, CCS811_MODE_250MS = 4 } |
CCS811 operation modes. More... | |
enum | ccs811_int_mode_t { CCS811_INT_NONE = 0, CCS811_INT_DATA_READY, CCS811_INT_THRESHOLD } |
CCS811 interrupt mode. More... | |
Functions | |
int | ccs811_init (ccs811_t *dev, const ccs811_params_t *params) |
Initialize a CCS811 sensor device. More... | |
int | ccs811_read_iaq (const ccs811_t *dev, uint16_t *iaq_tvoc, uint16_t *iaq_eco2, uint16_t *raw_i, uint16_t *raw_v) |
Read IAQ sensor values and/or RAW sensor data. More... | |
int | ccs811_read_ntc (const ccs811_t *dev, uint32_t r_ref, uint32_t *r_ntc) |
Read the resistance of connected NTC thermistor. More... | |
int | ccs811_data_ready (const ccs811_t *dev) |
Data-ready status function. More... | |
int | ccs811_power_down (ccs811_t *dev) |
Power down the sensor. More... | |
int | ccs811_power_up (ccs811_t *dev) |
Power up the sensor. More... | |
int | ccs811_set_mode (ccs811_t *dev, ccs811_mode_t mode) |
Set the operation mode of the sensor. More... | |
int | ccs811_set_int_mode (ccs811_t *dev, ccs811_int_mode_t mode) |
Enable/disable data ready or threshold interrupt signal nINT More... | |
int | ccs811_set_environmental_data (const ccs811_t *dev, int16_t temp, int16_t hum) |
Set environmental data. More... | |
int | ccs811_set_eco2_thresholds (const ccs811_t *dev, uint16_t low, uint16_t high, uint8_t hyst) |
Set eCO2 thresholds for threshold interrupts. More... | |
int | ccs811_get_baseline (const ccs811_t *dev, uint16_t *baseline) |
Get the current baseline value from sensor. More... | |
int | ccs811_set_baseline (const ccs811_t *dev, uint16_t baseline) |
Write a previously stored baseline value to the sensor. More... | |
CCS811 I2C addresses | |
#define | CCS811_I2C_ADDRESS_1 (0x5A) |
default | |
#define | CCS811_I2C_ADDRESS_2 (0x5B) |
CCS811 IAQ value ranges | |
#define | CCS811_ECO2_RANGE_MIN (400) |
eCO2 min in ppm | |
#define | CCS811_ECO2_RANGE_MAX (8192) |
eCO2 max in ppm | |
#define | CCS811_TVOC_RANGE_MIN (0) |
TVOC min in ppb. | |
#define | CCS811_TVOC_RANGE_MAX (1187) |
TVOC min in ppb. | |
enum ccs811_error_codes_t |
Driver error codes (returned as negative values)
enum ccs811_int_mode_t |
enum ccs811_mode_t |
CCS811 operation modes.
int ccs811_data_ready | ( | const ccs811_t * | dev | ) |
Data-ready status function.
The function reads the status register and returns CSS811_OK when new data are available. The function is useful for polling the sensor.
[in] | dev | Device descriptor of CCS811 device to read from |
CCS811_OK | when new data are available |
CCS811_ERROR_NO_NEW_DATA | when no new data are available |
CCS811_ERROR_* | otherwise, see ccs811_error_codes_t. |
int ccs811_get_baseline | ( | const ccs811_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 using the ccs811_set_baseline function after sensor is powered up again to continue the automatic baseline process.
[in] | dev | Device descriptor of CCS811 device |
[in] | baseline | Current baseline value |
CCS811_OK | on success |
CCS811_ERROR_* | on error, see ccs811_error_codes_t |
int ccs811_init | ( | ccs811_t * | dev, |
const ccs811_params_t * | params | ||
) |
Initialize a CCS811 sensor device.
The function resets the CCS811 sensor, checks its availability and initializes it according to the given configuration parameters.
If ccs811_params_t::reset_pin is configured
If ccs811_params_t::wake_pin is configured, it use to switch the sensor into the sleep mode while the I2C interface is not used.
[in] | dev | Device descriptor of CCS811 device to be initialized |
[in] | params | Configuration parameters used by initialization |
CCS811_OK | on success |
CCS811_ERROR_* | on error, see ccs811_error_codes_t |
int ccs811_power_down | ( | ccs811_t * | dev | ) |
Power down the sensor.
The feature disables sensor measurements by entering idle mode (CCS811_MODE_IDLE). In addition, the function sets the low active signal ** nWAKE ** to high to completely deactivate the sensor. The sensor is then no longer accessible via I2C. The last sensor measurement mode is saved.
The low active nWAKE signal pin has to be configured (ccs811_params_t::wake_pin) accordingly. Otherwise, the function fails and returns with CCS811_ERROR_NO_WAKE_PIN.
[in] | dev | Device descriptor of CCS811 device to read from |
CCS811_OK | on success |
CCS811_ERROR_NO_WAKE_PIN | ccs811_params_t::wake_pin not configured |
CCS811_ERROR_* | on error, see ccs811_error_codes_t |
int ccs811_power_up | ( | ccs811_t * | dev | ) |
Power up the sensor.
The function sets the low active signal ** nWAKE ** to low to activate the sensor and switches back from the idle mode (CCS811_MODE_IDLE) to the last measurement mode.
The low active nWAKE signal pin has to be configured (ccs811_params_t::wake_pin) accordingly. Otherwise, the function fails and returns with CCS811_ERROR_NO_WAKE_PIN.
[in] | dev | Device descriptor of CCS811 device to read from |
CCS811_OK | on success |
CCS811_ERROR_NO_WAKE_PIN | ccs811_params_t::wake_pin not configured |
CCS811_ERROR_* | on error, see ccs811_error_codes_t |
int ccs811_read_iaq | ( | const ccs811_t * | dev, |
uint16_t * | iaq_tvoc, | ||
uint16_t * | iaq_eco2, | ||
uint16_t * | raw_i, | ||
uint16_t * | raw_v | ||
) |
Read IAQ sensor values and/or RAW sensor data.
The function reads the IAQ sensor values (TVOC and eCO2) and/or the raw sensor data. For either iaq_tvoc2
, iaq_eco2
, raw_i
, or raw_v
also NULL
can be passed, if their value are not of interest.
iaq_tvoc
and iaq_eco2
parameters are not NULL
.[in] | dev | Device descriptor of CCS811 device to read from |
[out] | iaq_tvoc | TVOC total volatile organic compound (0..1187 ppb) |
[out] | iaq_eco2 | eCO2 equivalent CO2 (400 - 8192 ppm) |
[out] | raw_i | Current through the sensor used for measuring (0..63 uA) |
[out] | raw_v | Voltage across the sensor measured (0..1023 = 1.65 V) |
CCS811_OK | on success and new data are returned |
CCS811_ERROR_NO_NEW_DATA | when no new data are available and last measurement results are returned. |
CCS811_ERROR_* | otherwise, see ccs811_error_codes_t. |
int ccs811_read_ntc | ( | const ccs811_t * | dev, |
uint32_t | r_ref, | ||
uint32_t * | r_ntc | ||
) |
Read 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.
[in] | dev | Device descriptor of CCS811 device to read from |
[in] | r_ref | Resistance of R_REF in Ohm |
[out] | r_ntc | Resistance of R_NTC in Ohm |
CCS811_OK | on success |
CCS811_ERROR_* | on error, see ccs811_error_codes_t |
int ccs811_set_baseline | ( | const ccs811_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 (ccs811_get_baseline) value can be restored after the sensor is powered up to continue the automatic baseline process.
[in] | dev | Device descriptor of CCS811 device |
[in] | baseline | Stored baseline value |
CCS811_OK | on success |
CCS811_ERROR_* | on error, see ccs811_error_codes_t |
int ccs811_set_eco2_thresholds | ( | const ccs811_t * | dev, |
uint16_t | low, | ||
uint16_t | high, | ||
uint8_t | hyst | ||
) |
Set eCO2 thresholds for threshold interrupts.
Threshold interrupts, if enabled (ccs811_int_mode_t), are generated when new eCO2 value moves from the current range (LOW, MEDIUM or HIGH) to another range by more than one hysteresis value. The hysteresis is used to prevent multiple interrupts near a threshold.
Ranges are defined as following:
Name | Range | Value | Default |
---|---|---|---|
LOW | below the low parameter | > 400 | 1500 |
MEDIUM | between the low and high parameters | ||
HIGH | above the value of the high parameter | < 8192 | 2500 |
[in] | dev | Device descriptor of CCS811 device |
[in] | low | Threshold LOW to MEDIUM |
[in] | high | Threshold MEDIUM to HIGH |
[in] | hyst | Hysteresis value (default 50) |
CCS811_OK | on success |
CCS811_ERROR_* | on error, see ccs811_error_codes_t |
int ccs811_set_environmental_data | ( | const ccs811_t * | dev, |
int16_t | temp, | ||
int16_t | hum | ||
) |
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.
[in] | dev | Device descriptor of CCS811 device |
[in] | temp | Temperature [hundredths of a degree Celsius] |
[in] | hum | Relative Humidity [hundredths of a percent] |
CCS811_OK | on success |
CCS811_ERROR_* | on error, see ccs811_error_codes_t |
int ccs811_set_int_mode | ( | ccs811_t * | dev, |
ccs811_int_mode_t | mode | ||
) |
Enable/disable data ready or threshold interrupt signal nINT
CCS811 can trigger either
As soon as an interrupt condition occurs, the signal nINT is driven low. It stops being driven low when the sensor data is read with the ccs811_read_iaq function. ccs811_params_t::int_pin parameter has to be configured.
With CCS811_INT_NONE (the default), interrupt generation is disabled. The interrupt generation is disabled by default.
[in] | dev | Device descriptor of CCS811 device |
[in] | mode | Enable the interrupt if true, otherwise disable it |
CCS811_OK | on success |
CCS811_ERROR_NO_INT_PIN | ccs811_params_t::int_pin not configured |
CCS811_ERROR_* | on error, see ccs811_error_codes_t |
int ccs811_set_mode | ( | ccs811_t * | dev, |
ccs811_mode_t | mode | ||
) |
Set the operation mode of the sensor.
The function sets the operating mode of the sensor.
The sensor starts periodic measurements with the specified period if the mode
parameter is either
The ccs811_read_iaq function can then be used to get sensor data at the same rate to get the results.
In case, the mode
parameter is CCS811_MODE_IDLE, the sensor does not perform any measurements.
[in] | dev | Device descriptor of CCS811 device |
[in] | mode | CCS811 operation mode |
CCS811_OK | on success |
CCS811_ERROR_* | on error, see ccs811_error_codes_t |