Averaged reading
What this example shows
The averagedMeasurement example averages several readings before it prints
one. It takes NUM_READINGS (5) readings with SECS_BETWEEN_READINGS (3)
seconds between them, prints the average in PPM, then waits 2.5 seconds and
starts again. It creates the sensor with the SENSOR_CO configuration and no
address, so the constructor uses its default, DEFAULT_ADC_ADDR (0x49).
You do not need to wire LMPEN on this board, whatever the sketch's comments
say: the board holds it low itself.
The example code
/**
**************************************************
*
* @file averagedMeasurement.ino
* @brief See how to use a built-in function to make an averaged measurement
*
* To successfully run the sketch:
* - Connect LMPEN pin to GND or a GPIO pin so the breakout can be configured
* - Run the sketch and open serial monitor at 115200 baud!
*
*
* @authors Robert @ soldered.com
***************************************************/
// Include the required library
#include "Electrochemical-Gas-Sensor-SOLDERED.h"
// How many readings to average from and how long to wait between them
#define NUM_READINGS 5
#define SECS_BETWEEN_READINGS 3
// Configurations for each of the sensor types are in sensorConfigData.h in the library's 'src' folder
// Create the sensor object with the according type
ElectrochemicalGasSensor sensor(SENSOR_CO);
// If you are using a custom I2C address (for multiple sensors) use:
// ElectrochemicalGasSensor sensor(SENSOR_CO, 0x4A, 25);
// For more details on this, see customAddress.ino
void setup()
{
Serial.begin(115200); // For debugging
// Init the breakout
if (!sensor.begin())
{
// Can't init? Notify the user and go to infinite loop
Serial.println("ERROR: Can't init the sensor! Check connections!");
while (true)
delay(100);
}
Serial.println("Sensor initialized successfully!");
}
void loop()
{
// Make the averaged reading
// This is a blocking function, so it will take a while before code continues
double reading = sensor.getAveragedPPM(NUM_READINGS, SECS_BETWEEN_READINGS);
// If PPB is more relevant for your sensor, you can use:
// double reading = sensor.getAveragedPPB(NUM_READINGS, SECS_BETWEEN_READINGS);
// Print the reading with 5 digits of precision
Serial.print("Sensor reading: ");
Serial.print(reading, 5);
Serial.println(" PPM");
// Wait a bit before reading again
delay(2500);
}
What this board prints with the sketch as written has not been measured yet.
When this board was tested over Qwiic, an I2C scan found only one address,
0x30, and with the MicroPython library begin() failed for a sensor created
at 0x49, the default this sketch uses. If begin() fails, the sketch prints
ERROR: Can't init the sensor! Check connections! and stops.
When begin() succeeds, the sketch prints:
Sensor initialized successfully!
After it, a line Sensor reading: followed by a value with five decimals and
PPM arrives once per round of five readings.
Functions used
ElectrochemicalGasSensor(sensorType _t, uint8_t _adcAddr = DEFAULT_ADC_ADDR, int _configPin = -1)returns ElectrochemicalGasSensorCreates the sensor object with a sensor configuration, an address and an LMPEN pin. Nothing is sent until you call begin(). begin() picks how to talk to the board from the address: from 0x30 to 0x37 it uses the ATtiny404 bridge's commands and ignores the third argument; any other address is taken as the ADS1115's own address, and the LMP91000 is written directly.
ReturnsThe sensor object.
Parameters
| Type | Name | Description |
|---|---|---|
sensorType | _t | The front end and conversion settings, SENSOR_CO in the example. The presets live in the library's src/sensorConfigData.h. |
uint8_t | _adcAddr | The address, DEFAULT_ADC_ADDR (0x49) when left out, as in the example. |
int | _configPin | The GPIO pin connected to LMPEN, -1 (none) when left out. Not used for an address from 0x30 to 0x37. |
sensor.begin()returns boolStarts I2C with Wire.begin(). For an address from 0x30 to 0x37 it checks that the bridge answers, sends it the ADC gain and the slowest ADC data rate, then the LMP91000 settings; each bridge command waits up to 500 ms. For any other address it starts the ADS1115 at that address with the same gain and data rate, sets the LMPEN pin high if one was given, then writes the LMP91000 settings, holding that pin low while it does.
Returnstrue when every step succeeded, false otherwise.
sensor.getAveragedPPM(uint8_t _numMeasurements = 5, uint8_t _secondsDelay = 2)returns doubleTakes _numMeasurements getPPM() readings, waits _secondsDelay seconds after each one, and returns their mean. It blocks until every reading is taken. getPPM() returns 0 for a negative result, so such a reading counts as 0.
ReturnsThe averaged concentration in PPM.
Parameters
| Type | Name | Description |
|---|---|---|
uint8_t | _numMeasurements | How many readings to average, NUM_READINGS (5) in the example. |
uint8_t | _secondsDelay | Seconds to wait after each reading, SECS_BETWEEN_READINGS (3) in the example. |
sensor.getAveragedPPB(uint8_t _numMeasurements = 5, uint8_t _secondsDelay = 2)returns doubleThe commented-out alternative in the example: the getAveragedPPM() result multiplied by 1000.
ReturnsThe averaged concentration in PPB.
Parameters
| Type | Name | Description |
|---|---|---|
uint8_t | _numMeasurements | How many readings to average. |
uint8_t | _secondsDelay | Seconds to wait after each reading. |
Putting it together
SENSOR_CO is the preset for an SGX-4CO carbon monoxide cell. The library has
no preset for this board's SGX-4OX-ROHS oxygen cell yet, so the value the
example prints is calculated with the CO cell's settings. The
custom configuration
page shows how to write your own.