ags10: Driver for the AGS10 TVOC sensor

ESP-IDF driver for the AGS10 total volatile organic compounds (TVOC) sensor.

The AGS10 is a small metal-oxide gas sensor with a digital interface: it measures the total concentration of volatile organic compounds and reports it in parts per billion, together with the raw resistance of the sensing element. It needs no calibration to start, only a short warm-up.

Features

  • TVOC in parts per billion, 24 bit.

  • Raw gas resistance, 32 bit, for diagnostics of the sensing element.

  • Zero-point calibration: overwrite the zero point with the current resistance or with the factory value.

  • Firmware version of the sensor.

  • Persistent I2C address change.

  • CRC8 (polynomial 0x31, init 0xFF) on the frames the driver writes: the zero-point value and the address change.

  • Thread-safe access through the i2cdev mutex.

Hardware

AGS10 pin

ESP32 pin

Description

VCC

3.3 V

power supply

GND

GND

ground

SDA

GPIO21

I2C data, configurable

SCL

GPIO22

I2C clock, configurable

The sensor runs its I2C bus at 20 kHz, well below the 100 kHz the driver would otherwise use. Only the standard two pull-up resistors are needed.

Configuration

  • I2C address: AGS10_I2CADDR_DEFAULT (0x1A), changeable with ags10_set_i2c_address().

  • I2C frequency: I2C_FREQ_HZ (20 kHz), fixed by the sensor.

  • The address change survives a power cycle, so a board that has been re-addressed needs the new address in ags10_init_desc() afterwards.

Usage

ags10_init_desc() creates the device mutex; the bus itself is created by i2cdev on the first transfer. A TVOC or resistance read takes up to about 1.5 s while the sensor integrates; the version register answers in tens of milliseconds.

ags10_read_tvoc() returns the concentration in ppb, ags10_read_resistance() the raw resistance and ags10_read_version() the firmware version.

For calibration, ags10_set_zero_point_with_current_resistance() takes the current resistance as the zero point and must be run in clean air; ags10_set_zero_point_with_factory_defaults() restores the factory value; ags10_set_zero_point_with() writes an explicit 16-bit value.

Limitations

  • The bus must stay at 20 kHz: the sensor does not answer reliably above it.

  • The address change is persistent. Recovering from a wrong address requires the same command through the old address, so keep a note of the new one.

  • The zero point is the only calibration the sensor offers; the TVOC value is derived from it and from the current resistance.

  • The frames read from the sensor are not CRC-checked: the CRC is computed for the commands only, so a corrupted reading is not detected.

Example

examples/example1 reads TVOC in a loop, examples/example2 also shows the resistance and the firmware version, then runs both calibrations by itself: the zero-point calibration after a few seconds and the factory reset after the tenth reading. Both examples take SDA and SCL from idf.py menuconfig; the port is I2C_NUM_0 and the address is AGS10_I2CADDR_DEFAULT as compiled in.

Defines

AGS10_I2CADDR_DEFAULT

AGS10 default I2C address.

AGS10_TVOCSTAT_REG

Status and TVOC reading.

AGS10_VERSION_REG

Firmware version.

AGS10_GASRES_REG

Raw gas resistance.

AGS10_SETADDR_REG

Change I2C address.

AGS10_CRC8_INIT

CRC8 init value.

AGS10_CRC8_POLYNOMIAL

CRC8 polynomial.

I2C_FREQ_HZ

Fixed I2C frequency for AGS10.

Functions

esp_err_t ags10_init_desc(i2c_dev_t *dev, i2c_port_t port, uint8_t addr, gpio_num_t sda_gpio, gpio_num_t scl_gpio)

Initialize the AGS10 sensor descriptor.

This function initializes the I2C device descriptor for the AGS10 sensor. It configures the I2C port, address, and GPIO pins for SDA and SCL.

#include <ags10.h>
#include <i2cdev.h>
#include <esp_log.h>

#define TAG "ags10_example"
#define I2C_PORT I2C_NUM_0
#define SDA_GPIO GPIO_NUM_21
#define SCL_GPIO GPIO_NUM_22

void app_main(void)
{
    i2c_dev_t dev;
    ESP_ERROR_CHECK(i2cdev_init());

    esp_err_t err = ags10_init_desc(&dev, I2C_PORT, AGS10_I2CADDR_DEFAULT, SDA_GPIO, SCL_GPIO);
if (err != ESP_OK) {
        ESP_LOGE(TAG, "Failed to init AGS10: %s", esp_err_to_name(err));
return;
    }

    uint32_t tvoc;
    err = ags10_read_tvoc(&dev, &tvoc);
if (err == ESP_OK) {
        ESP_LOGI(TAG, "TVOC: %lu ppb", tvoc);
    }

ags10_free_desc(&dev);
}
Parameters:
  • dev – [out] Pointer to the I2C device descriptor

  • port – [in] I2C port number

  • addr – [in] I2C address of the sensor

  • sda_gpio – [in] GPIO number for SDA

  • scl_gpio – [in] GPIO number for SCL

Returns:

ESP_OK on success

esp_err_t ags10_free_desc(i2c_dev_t *dev)

Free the AGS10 sensor descriptor.

This function cleans up the I2C device descriptor resources.

Parameters:

dev – [in] Pointer to the I2C device descriptor

Returns:

ESP_OK on success

esp_err_t ags10_read_tvoc(i2c_dev_t *dev, uint32_t *tvoc)

Read TVOC value from the sensor.

This function reads the Total Volatile Organic Compounds (TVOC) value from the AGS10 sensor. The value is returned in parts per billion (ppb).

Note

The sensor requires some warm-up time after power-on for accurate readings. Typical warm-up time is around 10-60 seconds.

Parameters:
  • dev – [in] Pointer to the I2C device descriptor

  • tvoc – [out] Pointer to store the TVOC value in ppb

Returns:

  • ESP_OK: Success

  • ESP_ERR_INVALID_ARG: Invalid argument

  • ESP_FAIL: Communication failure

esp_err_t ags10_read_version(i2c_dev_t *dev, uint8_t *version)

Read firmware version from the sensor.

This function reads the firmware version of the AGS10 sensor.

Parameters:
  • dev – [in] Pointer to the I2C device descriptor

  • version – [out] Pointer to store the firmware version

Returns:

  • ESP_OK: Success

  • ESP_ERR_INVALID_ARG: Invalid argument

  • ESP_FAIL: Communication failure

esp_err_t ags10_read_resistance(i2c_dev_t *dev, uint32_t *resistance)

Read resistance value from the sensor.

This function reads the raw gas resistance value from the AGS10 sensor. This value can be used for advanced calibration or diagnostics.

Parameters:
  • dev – [in] Pointer to the I2C device descriptor

  • resistance – [out] Pointer to store the resistance value

Returns:

  • ESP_OK: Success

  • ESP_ERR_INVALID_ARG: Invalid argument

  • ESP_FAIL: Communication failure

esp_err_t ags10_set_i2c_address(i2c_dev_t *dev, uint8_t new_address)

Set new I2C address for the sensor.

This function changes the I2C address of the AGS10 sensor. The new address will be persistent after power cycling.

Warning

Use this function with caution. Make sure no other I2C devices use the new address to avoid conflicts.

Parameters:
  • dev – [inout] Pointer to the I2C device descriptor (address will be updated)

  • new_address – [in] New I2C address (7-bit, range: 0x08-0x77)

Returns:

  • ESP_OK: Success

  • ESP_ERR_INVALID_ARG: Invalid argument

  • ESP_FAIL: Communication failure

esp_err_t ags10_set_zero_point_with_factory_defaults(i2c_dev_t *dev)

Set zero-point calibration with factory defaults.

This function resets the zero-point calibration to factory default values. This is useful when the sensor readings drift over time.

Parameters:

dev – [in] Pointer to the I2C device descriptor

Returns:

  • ESP_OK: Success

  • ESP_ERR_INVALID_ARG: Invalid argument

  • ESP_FAIL: Communication failure

esp_err_t ags10_set_zero_point_with_current_resistance(i2c_dev_t *dev)

Set zero-point calibration with current resistance.

This function sets the current resistance reading as the zero point. Use this function in clean air conditions for calibration.

Parameters:

dev – [in] Pointer to the I2C device descriptor

Returns:

  • ESP_OK: Success

  • ESP_ERR_INVALID_ARG: Invalid argument

  • ESP_FAIL: Communication failure

esp_err_t ags10_set_zero_point_with(i2c_dev_t *dev, uint16_t value)

Set zero-point calibration with a specific value.

This function sets the zero-point calibration to a specific value. This is for advanced users who know the exact calibration value.

Parameters:
  • dev – [in] Pointer to the I2C device descriptor

  • value – [in] Calibration value (0x0000 = current resistance, 0xFFFF = factory defaults)

Returns:

  • ESP_OK: Success

  • ESP_ERR_INVALID_ARG: Invalid argument

  • ESP_FAIL: Communication failure