Skip to main content

Average several readings

What this example shows​

The averagedMeasurement example takes five readings with a three-second wait after each, averages them, prints the average in PPM, waits 2.5 seconds and starts again. It creates the sensor at the library's default address, 0x49.

NUM_READINGS and SECS_BETWEEN_READINGS set how many readings are averaged and how many seconds pass after each one.

The example is written with SENSOR_CO, the library's carbon monoxide preset, so its PPM values are not H2S values. Do not switch it to the library's SENSOR_H2S preset unchanged: that preset is written for the SGX-4H2S-100 cell, with a sensitivity of 1200 nA per ppm, while this board carries the SGX-4H2S-300 cell, whose datasheet gives an output signal of 460 ± 140 nA per ppm. With SENSOR_H2S the reading comes out about 2.6 times too low. Pass a custom sensor type with the SENSOR_H2S settings and 460 nA per ppm instead, as Quick Start shows.

The example code​

Select Board
averagedMeasurement.ino
// 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);
}

Expected result:

Serial MonitorAutoscrollShow timestampNewline115200 baud
Message (Enter to send message to the board)
Sensor initialized successfully!

Then a line starting with Sensor reading: follows with the averaged concentration in PPM to five decimals. With the example's settings, each average takes five readings with a 3-second wait after each, then the sketch waits 2.5 seconds, so a new line appears about every 17.5 seconds. If the sensor does not answer, the sketch prints ERROR: Can't init the sensor! Check connections! and stops.

Functions used​

ElectrochemicalGasSensor(sensorType _t, uint8_t _adcAddr = DEFAULT_ADC_ADDR, int _configPin = -1)returns none

Creates the sensor object. The sensor type holds the front-end and conversion settings for one gas cell. The address decides how begin() talks to the board: an address from 0x30 to 0x37 goes through the board's ATtiny404; any other address, such as the default 0x49, goes to an ADS1115 at that address and to the LMP91000 directly.

ReturnsConstructor.

Parameters

NameTypeDescription
_tsensorTypePreset from the library (SENSOR_CO, SENSOR_NO2, SENSOR_SO2, SENSOR_O3, SENSOR_NO, SENSOR_H2S, SENSOR_NH3, SENSOR_CL2) or a custom sensorType. For this board's cell, pass a custom one with 460 nA per ppm.
_adcAddruint8_tThe address begin() talks to. This example leaves it out, so the default, 0x49 (DEFAULT_ADC_ADDR), is used.
_configPinintA GPIO the library holds high and pulls low while it configures the LMP91000 directly. Ignored at addresses from 0x30 to 0x37. This example leaves it out (-1, none).
bool begin()returns bool

Starts I2C and picks how to talk to the board from the address. With an address from 0x30 to 0x37 it checks that the board's ATtiny404 answers and sends it the ADC gain from the sensor type with the slowest data rate; each command waits up to 500 ms for an answer. With any other address it starts an ADS1115 at that address with the same gain and data rate. Either way it then configures the LMP91000 front end. Call it once before reading.

Returnstrue when every step succeeded, false otherwise.

double getAveragedPPM(uint8_t _numMeasurements = 5, uint8_t _secondsDelay = 2)returns double

Takes _numMeasurements readings with getPPM(), waiting _secondsDelay seconds after each, and returns their mean. It blocks until every reading is taken.

ReturnsAveraged gas concentration in PPM.

Parameters

NameTypeDescription
_numMeasurementsuint8_tHow many readings to average. The example passes NUM_READINGS (5).
_secondsDelayuint8_tSeconds to wait after each reading. The example passes SECS_BETWEEN_READINGS (3).
double getAveragedPPB(uint8_t _numMeasurements = 5, uint8_t _secondsDelay = 2)returns double

The commented-out alternative in the example: returns getAveragedPPM() with the same arguments, multiplied by 1000. It blocks until every reading is taken.

ReturnsAveraged gas concentration in PPB.

Parameters

NameTypeDescription
_numMeasurementsuint8_tHow many readings to average.
_secondsDelayuint8_tSeconds to wait after each reading.

Putting it together​

Create one ElectrochemicalGasSensor with a sensor type, call begin() once in setup(), then call getAveragedPPM() or getAveragedPPB() with the number of readings and the wait after each. Each call blocks for about _numMeasurements times _secondsDelay seconds.