Skip to main content

Averaged measurement

What this example shows​

The library's averagedMeasurement example takes five readings three seconds apart, averages them with one function call, and prints the result.

The example creates the sensor with SENSOR_CO, the carbon monoxide preset. The library calculates ppm from the preset's sensitivity and gain settings; the preset for this board's chlorine cell is SENSOR_CL2.

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

The header comment asks for LMPEN to be connected. On this board a 10 kΩ resistor holds LMPEN low and it is wired to the ATtiny404, so you leave it unconnected (see the pin table on Hardware details).

Expected result:

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

Each average takes five readings with a 3 s wait after each, so the first Sensor reading: line (the average in ppm with five decimals, then PPM) comes about 15 s later, and the next ones about every 17.5 s, counting the 2.5 s pause at the end of loop(). 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 ElectrochemicalGasSensor

Creates the sensor object. It stores the settings only; nothing is sent to the board until begin().

ReturnsThe sensor object.

Parameters

TypeNameDescription
sensorType_tThe cell preset. The example passes SENSOR_CO; this board's SGX-4CL2 cell has the preset SENSOR_CL2.
uint8_t_adcAddrThe I2C address, DEFAULT_ADC_ADDR (0x49) when left out, as in the example. For an address from 0x30 to 0x37, the range of this board's ADDRESS switch, begin() talks to the board's ATtiny404; for any other address it looks for an ADS1115 at that address and the LMP91000 directly on the bus.
int_configPinA GPIO that begin() drives low while it configures the LMP91000 directly. -1 (none) when left out. Unused for addresses 0x30 to 0x37.
bool begin()returns bool

Starts Wire, checks that the board answers, sets the preset's ADC gain and data rate, and configures the LMP91000 with the preset's settings. Call it once in setup() before reading.

Returnstrue when the board answered and both configuration steps 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 mean. It blocks until it is done.

ReturnsThe averaged gas concentration in ppm.

Parameters

TypeNameDescription
uint8_t_numMeasurementsHow many readings to average. 5 by default; the example passes NUM_READINGS (5).
uint8_t_secondsDelaySeconds to wait after each reading. 2 by default; the example passes SECS_BETWEEN_READINGS (3).
double getAveragedPPB(uint8_t _numMeasurements = 5, uint8_t _secondsDelay = 2)returns double

Same as getAveragedPPM(), times 1000. The example shows it in a comment as the alternative.

ReturnsThe averaged gas concentration in ppb.

Parameters

TypeNameDescription
uint8_t_numMeasurementsHow many readings to average. 5 by default.
uint8_t_secondsDelaySeconds to wait after each reading. 2 by default.

The example also uses Serial and delay(), which come with the Arduino core, not with this library.

Putting it together​

Change NUM_READINGS and SECS_BETWEEN_READINGS to trade response time for a steadier value. While getAveragedPPM() runs, the rest of loop() waits.