Skip to content
esp-idf-libPublic

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

2 watching

Forks

Repository files navigation

esp-idf-lib/ags10

Build examples Build docs Validation

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.

Supported Targets

  • ESP32
  • ESP32-C2
  • ESP32-C3
  • ESP32-C5
  • ESP32-C6
  • ESP32-H2
  • ESP32-P4
  • ESP32-S2
  • ESP32-S3

Hardware Connection

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; the sensor has no other pins to connect.

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

#include <stdio.h>
#include <inttypes.h>

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

ESP_ERROR_CHECK(i2cdev_init());

i2c_dev_t dev;
ESP_ERROR_CHECK(ags10_init_desc(&dev, I2C_NUM_0, AGS10_I2CADDR_DEFAULT,
                                GPIO_NUM_21, GPIO_NUM_22));

uint32_t tvoc;
esp_err_t err = ags10_read_tvoc(&dev, &tvoc);
if (err == ESP_OK)
{
    printf("TVOC: %" PRIu32 " ppb\n", tvoc);
}

ags10_free_desc(&dev);
i2cdev_done();

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.

API Reference

Initialisation:

  • ags10_init_desc() — initialise the descriptor for a port, address and pins.
  • ags10_free_desc() — delete the mutex.

Reading:

  • ags10_read_tvoc() — TVOC in ppb.
  • ags10_read_version() — firmware version of the sensor.
  • ags10_read_resistance() — raw gas resistance.

Calibration:

  • ags10_set_zero_point_with_factory_defaults() — restore the factory zero point (0xFFFF).
  • ags10_set_zero_point_with_current_resistance() — take the current resistance as the zero point (0x0000), run it in clean air.
  • ags10_set_zero_point_with() — write an explicit 16-bit zero point.

Configuration:

  • ags10_set_i2c_address() — change the address, effective immediately and after a power cycle.

Examples

  • example1 — read TVOC in a loop.
  • example2 — read TVOC and resistance, show the version, then run both calibrations on its own: 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.

TVOC Interpretation

TVOC (ppb) Air quality Description
0…65 excellent no irritation or discomfort
65…220 good no irritation or discomfort
220…660 moderate irritation and discomfort possible
660…2200 poor irritation and discomfort expected
2200…5500 very poor irritation and discomfort, more serious health issues possible
>5500 extremely poor more serious health issues possible

The table is the one from the sensor documentation; it describes typical indoor air, not a measurement uncertainty.

Important Notes

  • Let the sensor warm up for 10–60 s after power-on; the first readings drift.
  • Run the zero-point calibration in clean outdoor air, not in the room being measured.
  • The raw resistance helps to tell a dirty or aged sensing element from a genuinely high TVOC level.
  • The address change is persistent; a wrong address can only be recovered by the same command through the old address, so keep a note of it.
  • The bus runs at 20 kHz. Do not raise the clock: the sensor does not answer reliably above it.
  • 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.

Thread Safety

The driver takes the i2cdev device mutex around every transfer, so several tasks may share one descriptor. This requires locking to be enabled in i2cdev; with CONFIG_I2CDEV_NOLOCK (legacy driver only) the caller must serialise access itself.

License

MIT License, see LICENSE.

Author

Copyright (c) 2025 xyzroe, original driver.

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.

Installation

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

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

2 watching

Forks

Releases

Packages

Contributors

Languages