Skip to main content

Averaged measurement

What this example shows​

The library's averagedMeasurement example takes five readings three seconds apart and prints their average in ppm. Averaging smooths out noise in a small signal.

The example creates the sensor with the CO cell preset, SENSOR_CO. SENSOR_SO2 is the library's preset for the SGX-4SO2 cell on this board: change SENSOR_CO to SENSOR_SO2 in the line that creates sensor to get SO2 values.

On this board the LMP91000 and ADS1115 sit on the board's own bus 2, and your controller reaches them through the board's address on the Qwiic bus: 0x30 as shipped, 0x30 to 0x37 with the ADDRESS switch. The example passes no address, so it uses the library's default, 0x49, which talks to an ADS1115 directly. Pass the board's address as the second argument of the constructor, for example ElectrochemicalGasSensor sensor(SENSOR_SO2, 0x30);. Where the header says to connect LMPEN to GND or a GPIO pin, on this board LMPEN is already pulled to GND by a 10 kΩ resistor and also runs to the on-board microcontroller, and at the board's addresses the library ignores the enable-pin argument (see Hardware Details).

The listing is the library's file at the pinned commit, apart from a few comment-only lines in its header, which name the connector by an older name or link to other Soldered products, and are left out.

The example code​

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

Expected result:

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

Then a Sensor reading: line with the averaged concentration in ppm appears for every set of five readings. Each set takes 15 seconds (five readings with a 3-second wait after each), and the sketch waits another 2.5 seconds before the next set.

Functions used​

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

Creates the sensor object from a cell preset, an address and an optional enable pin. The example passes only the preset, so the address is the default, 0x49.

ReturnsConstructor.

Parameters

ParameterTypeDescription
_tsensorTypeThe cell preset. SENSOR_SO2 is the preset for this board's cell.
_adcAddruint8_tThe address the library talks to. 0x30 to 0x37 is the bridge range, set by the board's ADDRESS switch; any other value talks to an ADS1115 directly at that address. Default 0x49.
_configPinintGPIO for the front end's enable line. Ignored at addresses 0x30 to 0x37. Default -1.
bool begin()returns bool

Starts I2C, checks that the board answers, and sends the ADC and LMP91000 settings from the preset to the board.

Returnstrue when the board answered and took every setting, false otherwise.

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

Takes several getPPM() readings and returns their average. It waits the given number of seconds after each reading and blocks until it is done.

ReturnsThe averaged gas concentration in ppm.

Parameters

ParameterTypeDescription
_numMeasurementsuint8_tHow many readings to average. Default 5.
_secondsDelayuint8_tSeconds to wait after each reading. Default 2.
double getAveragedPPB(uint8_t _numMeasurements = 5, uint8_t _secondsDelay = 2)returns double

The same average as getAveragedPPM(), in parts per billion. The example shows it in a comment as an alternative.

ReturnsThe averaged gas concentration in ppb.

Parameters

ParameterTypeDescription
_numMeasurementsuint8_tHow many readings to average. Default 5.
_secondsDelayuint8_tSeconds to wait after each reading. Default 2.

Putting it together​

Change NUM_READINGS and SECS_BETWEEN_READINGS to trade response speed for a steadier value. The call blocks for NUM_READINGS x SECS_BETWEEN_READINGS seconds, so nothing else in loop() runs meanwhile.