GY-63_MS5611/libraries/AGS02MA/README.md

364 lines
14 KiB
Markdown
Raw Normal View History

2021-12-11 12:45:41 -05:00
2021-08-13 07:08:52 -04:00
[![Arduino CI](https://github.com/RobTillaart/AGS02MA/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)
2021-09-24 06:31:48 -04:00
[![Arduino-lint](https://github.com/RobTillaart/AGS02MA/actions/workflows/arduino-lint.yml/badge.svg)](https://github.com/RobTillaart/AGS02MA/actions/workflows/arduino-lint.yml)
[![JSON check](https://github.com/RobTillaart/AGS02MA/actions/workflows/jsoncheck.yml/badge.svg)](https://github.com/RobTillaart/AGS02MA/actions/workflows/jsoncheck.yml)
2023-09-25 11:36:00 -04:00
[![GitHub issues](https://img.shields.io/github/issues/RobTillaart/AGS02MA.svg)](https://github.com/RobTillaart/AGS02MA/issues)
2021-08-13 07:08:52 -04:00
[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/AGS02MA/blob/master/LICENSE)
[![GitHub release](https://img.shields.io/github/release/RobTillaart/AGS02MA.svg?maxAge=3600)](https://github.com/RobTillaart/AGS02MA/releases)
2023-09-25 11:36:00 -04:00
[![PlatformIO Registry](https://badges.registry.platformio.org/packages/robtillaart/library/AGS02MA.svg)](https://registry.platformio.org/libraries/robtillaart/AGS02MA)
2021-08-13 07:08:52 -04:00
2021-10-19 05:51:55 -04:00
2021-08-13 07:08:52 -04:00
# AGS02MA
2021-08-15 13:35:40 -04:00
Arduino library for AGS02MA TVOC sensor.
2021-08-13 07:08:52 -04:00
2023-12-06 13:16:29 -05:00
#### Description
2022-08-12 06:15:08 -04:00
This library is still experimental, so please use with care.
2024-02-03 14:45:51 -05:00
The AGS02MA is a sensor that measures the TVOC = Total Volatile Organic Compounds
in the air. It does not measure a specific gas, but several.
2022-08-12 06:15:08 -04:00
Note the warning about the I2C low speed, the device works at max 30 KHz.
Since 0.3.1 this library uses 25 KHz.
2021-08-13 07:08:52 -04:00
2024-02-03 14:45:51 -05:00
Note this library is not meant to replace professional monitoring systems.
2021-08-13 07:08:52 -04:00
2023-12-06 13:16:29 -05:00
#### 0.4.0 Breaking change
Version 0.4.0 introduced a breaking change.
You cannot set the pins in **begin()** any more.
This reduces the dependency of processor dependent Wire implementations.
The user has to call **Wire.begin()** and can optionally set the Wire pins
before calling **begin()**.
2024-02-03 14:45:51 -05:00
#### Related
- https://www.renesas.com/us/en/document/whp/overview-tvoc-and-indoor-air-quality
2022-08-12 06:15:08 -04:00
## I2C
### PIN layout from left to right
2023-01-21 08:28:43 -05:00
| Front L->R | Description |
|:------------:|:--------------|
| pin 1 | VDD + |
| pin 2 | SDA data |
| pin 3 | GND |
| pin 4 | SCL clock |
2022-08-12 06:15:08 -04:00
2023-01-21 08:28:43 -05:00
#### WARNING - LOW SPEED
2021-08-13 07:08:52 -04:00
2021-08-15 13:35:40 -04:00
The sensor uses I2C at very low speed <= 30 KHz.
2022-08-12 06:15:08 -04:00
For an Arduino UNO the lowest speed supported is about 30.4KHz (TWBR = 255) which works.
First runs with Arduino UNO indicate 2 failed reads in > 500 Reads, so less than 1%
2023-01-21 08:28:43 -05:00
Tests with ESP32 / ESP8266 at 30 KHz look good,
2022-08-12 06:15:08 -04:00
tests with ESP32 at lower clock speeds are to be done but expected to work.
2021-08-15 13:35:40 -04:00
2023-01-21 08:28:43 -05:00
The library sets the clock speed to 30 KHz (for non AVR) during operation
2022-08-12 06:15:08 -04:00
and resets it default to 100 KHz after operation.
2023-01-21 08:28:43 -05:00
This is done to minimize interference with the communication of other devices.
2022-08-12 06:15:08 -04:00
The reset clock speed can be changed with **setI2CResetSpeed(speed)** e.g. to 200 or 400 KHz.
#### 0.3.1 fix.
2023-01-21 08:28:43 -05:00
Version 0.3.1 sets the **I2C prescaler TWSR** register of the Arduino UNO to 4 so the lowest
speed possible is reduced to about 8 KHz.
2022-08-12 06:15:08 -04:00
A test run 4 hours with 6000++ reads on an UNO at 25 KHz gave 0 errors.
So the communication speed will be set to 25 KHz, also for other boards, for stability.
After communication the clock (+ prescaler) is reset again as before.
2021-08-13 07:08:52 -04:00
2024-02-03 14:45:51 -05:00
#### I2C multiplexing
Sometimes you need to control more devices than possible with the default
address range the device provides.
This is possible with an I2C multiplexer e.g. TCA9548 which creates up
to eight channels (think of it as I2C subnets) which can use the complete
address range of the device.
Drawback of using a multiplexer is that it takes more administration in
your code e.g. which device is on which channel.
This will slow down the access, which must be taken into account when
deciding which devices are on which channel.
Also note that switching between channels will slow down other devices
too if they are behind the multiplexer.
- https://github.com/RobTillaart/TCA9548
2022-06-12 11:18:20 -04:00
## Version 118 problems
2022-04-25 08:17:43 -04:00
The library can request the version with **getSensorVersion()**.
My devices all report version 117 and this version is used to develop / test this library.
2022-06-12 11:18:20 -04:00
There are devices reported with version 118 which behave differently.
2023-01-21 08:28:43 -05:00
#### ugM3 not supported
2022-06-12 11:18:20 -04:00
See - https://github.com/RobTillaart/AGS02MA/issues/11
The version 118 seems only to support the **PPB** and not the **ugM3** mode.
2022-04-25 08:17:43 -04:00
It is unclear if this is an incident, bug or a structural change in the firmware.
2022-06-12 11:18:20 -04:00
If you encounter similar problems with setting the mode (any version), please let me know.
That will help indicating if this is a "structural change" or incident.
2023-01-21 08:28:43 -05:00
#### Calibrate problem!
2022-06-12 11:18:20 -04:00
See - https://github.com/RobTillaart/AGS02MA/issues/13
2023-01-21 08:28:43 -05:00
In this issue a problem is reported with a version 118 sensor.
2022-06-12 11:18:20 -04:00
The problem exposed itself after running the calibration sketch (command).
The problem has been confirmed by a 2nd version 118 sensor.
Additional calibration runs did not fix the problem.
Version 117 seem to have no problems with calibration.
**Advice**: do **NOT** calibrate a version 118.
Note: the version 0.2.0 determines the version in the calibration function so
2023-01-21 08:28:43 -05:00
it won't calibrate any non 117 version.
2022-06-12 11:18:20 -04:00
2023-01-21 08:28:43 -05:00
#### Please report your experiences.
2022-04-25 08:17:43 -04:00
2023-01-21 08:28:43 -05:00
If you have a AGS20MA device, version 117 or 118 or other,
please let me know your experiences
2022-06-12 11:18:20 -04:00
with the sensor and this (or other) library.
2022-04-25 08:17:43 -04:00
2021-08-13 07:08:52 -04:00
## Interface
2023-09-25 11:36:00 -04:00
```cpp
#include "AGS02MA.h"
```
2023-01-21 08:28:43 -05:00
#### Constructor
2021-08-15 13:35:40 -04:00
2023-12-06 13:16:29 -05:00
- **AGS02MA(uint8_t deviceAddress = 26, TwoWire \*wire = &Wire)** constructor,
with default address and default I2C interface.
- **bool begin()** initialize the library.
2021-08-13 07:08:52 -04:00
- **bool isConnected()** returns true if device address can be seen on I2C.
2021-09-24 06:31:48 -04:00
- **void reset()** reset internal variables.
2021-08-13 07:08:52 -04:00
2023-01-21 08:28:43 -05:00
#### Timing
2021-08-13 07:08:52 -04:00
2021-08-15 13:35:40 -04:00
- **bool isHeated()** returns true if 2 minutes have passed after startup (call of **begin()** ).
Otherwise the device is not optimal ready.
2021-12-11 12:45:41 -05:00
According to the datasheet the preheating will improve the quality of the measurements.
2021-08-15 13:35:40 -04:00
- **uint32_t lastRead()** last time the device is read, timestamp is in milliseconds since start.
2023-01-21 08:28:43 -05:00
Returns 0 if **readPPB()** or **readUGM3()** is not called yet.
This function allows to implement sort of asynchronous wait.
2021-09-24 06:31:48 -04:00
One must keep reads at least 1.5 seconds but preferred 3 seconds apart according to the datasheet.
2021-08-13 07:08:52 -04:00
2023-01-21 08:28:43 -05:00
#### Administration
2021-08-13 07:08:52 -04:00
2023-01-21 08:28:43 -05:00
- **bool setAddress(const uint8_t deviceAddress)** sets a new address for the sensor.
2021-09-24 06:31:48 -04:00
If function succeeds the address changes immediately and will be persistent over a reboot.
2021-08-15 13:35:40 -04:00
- **uint8_t getAddress()** returns the set address. Default the function will return 26 or 0x1A.
2023-01-21 08:28:43 -05:00
- **uint8_t getSensorVersion()** reads sensor version from device.
If the version cannot be read the function will return 255.
2022-01-22 04:17:50 -05:00
(My test sensors all return version 117, version 118 is reported)
2023-01-21 08:28:43 -05:00
- **uint32_t getSensorDate()** (experimental) reads bytes from the sensor that seem to indicate the production date(?). This date is encoded in an uint32_t to minimize footprint as it is a debug function.
2021-08-13 07:08:52 -04:00
2022-01-22 04:17:50 -05:00
```cpp
uint32_t dd = sensor.getSensorDate();
2023-01-21 08:28:43 -05:00
Serial.println(dd, HEX); // prints YYYYMMDD e.g. 20210203
2022-01-22 04:17:50 -05:00
```
2023-01-21 08:28:43 -05:00
#### I2C clock speed
2022-01-22 04:17:50 -05:00
2023-01-21 08:28:43 -05:00
The library sets the clock speed to 25 KHz during operation
2021-09-24 06:31:48 -04:00
and resets it to 100 KHz after operation.
2021-08-15 13:35:40 -04:00
This is done to minimize interference with the communication of other devices.
The following function can change the I2C reset speed to e.g. 200 or 400 KHz.
2021-08-13 07:08:52 -04:00
2021-12-11 12:45:41 -05:00
- **void setI2CResetSpeed(uint32_t speed)** sets the I2C speed the library need to reset the I2C speed to.
2021-09-24 06:31:48 -04:00
- **uint32_t getI2CResetSpeed()** returns the value set. Default is 100 KHz.
2021-08-13 07:08:52 -04:00
2023-01-21 08:28:43 -05:00
#### setMode
2021-08-13 07:08:52 -04:00
2021-08-15 13:35:40 -04:00
The default mode at startup of the sensor is PPB = parts per billion.
2021-08-13 07:08:52 -04:00
- **bool setPPBMode()** sets device in PartPerBillion mode. Returns true on success.
- **bool setUGM3Mode()** sets device in micro gram per cubic meter mode. Returns true on success.
- **uint8_t getMode()** returns mode set. 0 = PPB, 1 = UGm3, 255 = not set.
2023-12-06 13:16:29 -05:00
2023-09-25 11:36:00 -04:00
#### Air quality classification
2024-02-03 14:45:51 -05:00
Indicative
| TVOC(ppb) | Scale | Description | Colour |
|:---------:|:-------:|:---------------------:|:-------------|
| <= 220 | 1 | Good | Green |
| <= 660 | 3 | Moderate | Yellow |
| <= 1430 | 7 | Bad | Orange |
| <= 2200 | 10 | Unhealthy | Red |
| <= 3300 | 15 | Very unhealthy | Purple |
| <= 5500 | 25 | Hazardous | Deep Purple |
| > 5500 | 50 | Extremely Hazardous | Deep Purple |
2023-09-25 11:36:00 -04:00
[Source](https://learn.kaiterra.com/en/resources/understanding-tvoc-volatile-organic-compounds)
2021-08-13 07:08:52 -04:00
2024-02-03 14:45:51 -05:00
- Scale is a relative scale where 220 ~~ 1
- Colour is an indicative colour mapping.
- https://github.com/RobTillaart/map2colour for continuous scale.
2023-12-06 13:16:29 -05:00
2021-08-15 13:35:40 -04:00
#### PPB versus UGM3
2023-01-21 08:28:43 -05:00
There is no 1 to 1 relation between the PPB and the uG/m3 readings as this relation depends
2021-09-24 06:31:48 -04:00
on the weight of the individual molecules.
2021-08-15 13:35:40 -04:00
PPB is therefore an more an absolute indicator where uG/m3 is sort of relative indicator.
2021-09-24 06:31:48 -04:00
If the gas is unknown, PPB is in my opinion the preferred measurement.
From an unverified source the following formula:
M = molecular weight of the gas.
2023-01-21 08:28:43 -05:00
**μg/m3 = ppb \* M \* 12.187 / (273.15 + °C)**
2021-09-24 06:31:48 -04:00
2023-01-21 08:28:43 -05:00
Simplified formula for 1 atm @ 25°C:
2021-09-24 06:31:48 -04:00
2024-02-03 14:45:51 -05:00
**μg/m3 = ppb \* M \* 0.04087539829**
2021-09-24 06:31:48 -04:00
Some known gasses
2023-01-21 08:28:43 -05:00
| gas | Common name | ratio ppb-μg/m3 | molecular weight M |
|:-------|:--------------------|:----------------------|:--------------------:|
| SO2 | Sulphur dioxide | 1 ppb = 2.62 μg/m3 | 64 gr/mol |
| NO2 | Nitrogen dioxide | 1 ppb = 1.88 μg/m3 | 46 gr/mol |
| NO | Nitrogen monoxide | 1 ppb = 1.25 μg/m3 | 30 gr/mol |
| O3 | Ozone | 1 ppb = 2.00 μg/m3 | 48 gr/mol |
| CO | Carbon Monoxide | 1 ppb = 1.145 μg/m3 | 28 gr/mol |
| C6H6 | Benzene | 1 ppb = 3.19 μg/m3 | 78 gr/mol |
2021-08-15 13:35:40 -04:00
2024-02-03 14:45:51 -05:00
- https://github.com/RobTillaart/AtomicWeight (determine M from chemical formula)
2023-01-21 08:28:43 -05:00
#### Read the sensor
2021-08-13 07:08:52 -04:00
2021-09-24 06:31:48 -04:00
WARNING: The datasheet advises to take 3 seconds between reads.
2022-08-12 06:15:08 -04:00
Tests gave stable results at 1.5 second intervals.
Use this faster rate at your own risk.
2021-08-13 07:08:52 -04:00
2021-09-24 06:31:48 -04:00
- **uint32_t readPPB()** reads PPB (parts per billion) from device.
Typical value should be between 1 .. 999999.
Returns **lastPPB()** value if failed so one does not get sudden jumps in graphs.
2023-01-21 08:28:43 -05:00
Check **lastStatus()** and **lastError()** to get more info about success.
2021-09-24 06:31:48 -04:00
Time needed is ~35 milliseconds.
2023-01-21 08:28:43 -05:00
- **uint32_t readUGM3()** reads UGM3 (microgram per cubic meter) current value from device.
2021-09-24 06:31:48 -04:00
Typical values depend on the molecular weight of the TVOC.
Returns **lastUGM3()** if failed so one does not get sudden jumps in graphs.
2023-01-21 08:28:43 -05:00
- **float readPPM()** returns parts per million (PPM).
2021-09-24 06:31:48 -04:00
This function is a wrapper around readPPB().
2023-01-21 08:28:43 -05:00
Typical value should be between 0.01 .. 999.99
2021-09-24 06:31:48 -04:00
- **float readMGM3()** returns milligram per cubic meter.
- **float readUGF3()** returns microgram per cubic feet.
2023-01-21 08:28:43 -05:00
#### Error Codes
2022-08-12 06:15:08 -04:00
2023-01-21 08:28:43 -05:00
| ERROR_CODES | value |
|:----------------------------|:-------:|
| AGS02MA_OK | 0 |
| AGS02MA_ERROR | -10 |
| AGS02MA_ERROR_CRC | -11 |
| AGS02MA_ERROR_READ | -12 |
| AGS02MA_ERROR_NOT_READY | -13 |
2022-01-22 04:17:50 -05:00
2021-09-24 06:31:48 -04:00
#### Cached values
2023-01-21 08:28:43 -05:00
- **float lastPPM()** returns last readPPM (parts per million) value (cached).
2021-09-24 06:31:48 -04:00
- **uint32_t lastPPB()** returns last read PPB (parts per billion) value (cached). Should be between 1..999999.
- **uint32_t lastUGM3()** returns last read UGM3 (microgram per cubic meter) value (cached).
2021-08-13 07:08:52 -04:00
2023-01-21 08:28:43 -05:00
#### Calibration
2021-08-13 07:08:52 -04:00
2021-08-15 13:35:40 -04:00
- **bool zeroCalibration()** to be called after at least 5 minutes in fresh air.
See example sketch.
2022-04-25 08:17:43 -04:00
- **bool manualZeroCalibration(uint16_t value = 0)** Set the zero calibration value manually.
To be called after at least 5 minutes in fresh air.
2022-08-12 06:15:08 -04:00
- For v117: 0-65535 = automatic calibration.
- For v118: 0 = automatic calibration, 1-65535 manual calibration.
2024-02-03 14:45:51 -05:00
- **bool getZeroCalibrationData(ZeroCalibrationData &data)** fills a data struct with the
current zero calibration status and value.
2022-04-25 08:17:43 -04:00
Returns true on success.
2022-08-12 06:15:08 -04:00
2023-01-21 08:28:43 -05:00
#### Other
2022-08-12 06:15:08 -04:00
2022-05-02 07:50:27 -04:00
- **bool readRegister(uint8_t address, RegisterData &reg)** fills a data struct with the chip's register data at that address.
Primarily intended for troubleshooting and analysis of the sensor. Not recommended to build applications on top of this method's raw data.
2024-02-03 14:45:51 -05:00
Returns true when the **RegisterData** is filled, false when the data could not be read.
Note: unlike other public methods, CRC errors don't return false or show up in `lastError()`,
instead the CRC result is stored in `RegisterData.crcValid`.
2021-08-13 07:08:52 -04:00
- **int lastError()** returns last error.
2021-08-15 13:35:40 -04:00
- **uint8_t lastStatus()** returns status byte from last read.
2022-01-22 04:17:50 -05:00
Read datasheet or table below for details. A new read is needed to update this.
2021-09-24 06:31:48 -04:00
- **uint8_t dataReady()** returns RDY bit from last read.
2021-08-13 07:08:52 -04:00
2021-09-24 06:31:48 -04:00
#### Status bits.
2021-08-13 07:08:52 -04:00
2022-08-12 06:15:08 -04:00
| bit | description | notes |
|:-----:|:------------------------------------|:--------|
| 7-4 | internal use |
| 3-1 | 000 = PPB 001 = uG/M3 |
2023-01-21 08:28:43 -05:00
| 0 | RDY bit 0 = ready 1 = not ready | 1 == busy
2021-08-15 13:35:40 -04:00
2021-09-24 06:31:48 -04:00
## Future
2021-08-15 13:35:40 -04:00
2023-01-21 08:28:43 -05:00
#### Must
2023-09-25 11:36:00 -04:00
- improve documentation
2024-02-03 14:45:51 -05:00
- references?
2023-01-21 08:28:43 -05:00
#### Should
- check the mode bits of the status byte with internal \_mode.
- maximize robustness of state
- test with hardware
2024-02-03 14:45:51 -05:00
- different gasses ? indoor / outdoor?
- test with different processors
2023-01-21 08:28:43 -05:00
#### Could
- elaborate error handling.
- create an async interface for **readPPB()** if possible
- delay(30) blocks performance ==> async version of **readRegister()**
2024-02-03 14:45:51 -05:00
- could introduce complex I2C speed handling...
- separate state - request pending or so?
2023-01-21 08:28:43 -05:00
- move code to .cpp?
#### Wont
2021-08-13 07:08:52 -04:00
2023-09-25 11:36:00 -04:00
## Support
If you appreciate my libraries, you can support the development and maintenance.
Improve the quality of the libraries by providing issues and Pull Requests, or
donate through PayPal or GitHub sponsors.
Thank you,