Skip to main content

Average several readings

What this example shows​

The averagedMeasurement example creates one sensor with the carbon monoxide preset, SENSOR_CO, and prints the average of five readings, taken 3 seconds apart, in parts per million (PPM).

Wire your controller to the top header for this example, not to a Qwiic connector: the controller's SDA to SDA2, its SCL to SCL2, its 3.3 V supply to 3V3 and its ground to GND. At this address the library talks to the ADS1115 and the LMP91000 directly on SDA2 and SCL2; the board's ATtiny404 bridge at 0x30 is not used. Over Qwiic, only the bridge example in the Quick Start works.

The example code​

NUM_READINGS sets how many readings go into each average, and SECS_BETWEEN_READINGS how many seconds the library waits after each one. Change SENSOR_CO to the preset for your cell. The sketch creates the sensor with the preset alone, so the library's default address, 0x49, is used.

The sketch's comment says to connect LMPEN to GND or a GPIO. On this board, LMPEN goes to the header and to the board's ATtiny404.

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);
}

🔗Open the complete example

examples/averagedMeasurement at commit c4df54b

→

Open the Serial Monitor at 115200 baud. The sketch prints:

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

Then one Sensor reading: line per average, with five decimals and PPM. Each average takes five readings with 3 seconds after each, so about 15 seconds, and the sketch pauses 2.5 seconds before the next one. If begin() fails, 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 for one board. The address decides how begin() talks to it: for an address from 0x30 to 0x37 it uses the protocol of the ATtiny404 bridge; for any other address it talks to an ADS1115 at that address and to the LMP91000 itself. The default is DEFAULT_ADC_ADDR (0x49).

ReturnsConstructor.

Parameters

NameTypeDescription
_tsensorTypeThe preset for your cell: SENSOR_CO, SENSOR_NO2, SENSOR_SO2, SENSOR_O3, SENSOR_NO, SENSOR_H2S, SENSOR_NH3, SENSOR_CL2, or your own sensorType.
_adcAddruint8_tThe address begin() uses. 0x30 to 0x37 selects the bridge protocol; this board answers at 0x30 out of the box, or at the address its switch sets. Any other value is taken as an ADS1115 address. 0x49 by default.
_configPinintA GPIO for the LMP91000 enable line. The library holds it high and pulls it low only while it writes the LMP91000's registers itself; for an address from 0x30 to 0x37 it is not used. -1 (none) by default.
bool begin()returns bool

Starts I2C with Wire.begin() and picks the protocol from the address. For 0x30 to 0x37 it checks that the bridge answers, sends the ADC gain from the preset and has the bridge write the LMP91000's registers, each step giving up after 500 ms. For any other address it checks that an ADS1115 answers at that address, sets its gain, and writes the LMP91000's registers when the LMP91000 reports ready, pulling _configPin low around the writes when one is given. 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 one, and returns their average. It blocks until it is done.

ReturnsThe averaged gas concentration in PPM.

Parameters

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

The same average in parts per billion. The example shows it as a commented-out alternative.

ReturnsThe averaged gas concentration in PPB, getAveragedPPM() x 1000.

Parameters

NameTypeDescription
_numMeasurementsuint8_tHow many readings to average. 5 by default.
_secondsDelayuint8_tSeconds to wait after each reading. 2 by default.

Putting it together​

Create the sensor with the preset for your cell and call begin() once. Then call getAveragedPPM() or getAveragedPPB() with the number of readings and the wait between them. Each call blocks for about _numMeasurements x _secondsDelay seconds, so your sketch does nothing else meanwhile.