Skip to main content

Averaged measurement

What this example shows​

The averagedMeasurement example creates the sensor with the SENSOR_CO preset, the library's preset for this board's SGX-4CO cell, and prints the average of five readings taken three seconds apart, in ppm. NUM_READINGS and SECS_BETWEEN_READINGS at the top set how many readings are averaged and how long to wait between them.

What it needs on this board​

The example creates the sensor at the constructor's default address, 0x49. At an address outside 0x30 to 0x37 the library does not go through the board's bridge: begin() starts the ADS1115 at that address and writes the LMP91000's configuration itself, over the controller's I2C. On this board those two chips sit on the internal bus, which the top header brings out as SDA2 and SCL2. The Qwiic connectors and SDA1/SCL1 reach only the ATtiny404 bridge, and an I2C scan over Qwiic finds only the bridge, at 0x30 as shipped. The example passes no config pin, and LMPEN is pulled to GND on the board; see Hardware Details.

So wire the top header for this example, not the Qwiic cable: connect your controller's SDA to SDA2, its SCL to SCL2, its 3.3 V pin to 3V3 and its GND to GND. Over Qwiic only the bridge answers.

The bridge's address, 0x30 to 0x37, is the one the Read the CO concentration example passes.

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 lines below are from a run of this example on this board, with the controller wired to the header's SDA2, SCL2, 3V3 and GND pins as described above.

Expected result in the Serial Monitor at 115200 baud:

Serial MonitorAutoscrollShow timestampNewline115200 baud
Message (Enter to send message to the board)
Sensor initialized successfully!
Sensor reading: 0.32488 PPM
Sensor reading: 0.29840 PPM
Sensor reading: 0.32744 PPM

Each average takes five readings with a 3-second wait after each one, and the loop then waits another 2.5 seconds, so a new line arrives about every 17.5 seconds: Sensor reading: , the averaged concentration in ppm with five decimals, and PPM. 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. The example passes SENSOR_CO and leaves the address and the config pin at their defaults.

ReturnsConstructor.

Parameters

ParameterTypeDescription
_tsensorTypeThe cell's preset. For this board's SGX-4CO cell use SENSOR_CO (70 nA/ppm, ADC range 4.096 V, 14 kΩ amplifier gain, 20% internal zero).
_adcAddruint8_tThe I2C address. Default DEFAULT_ADC_ADDR, 0x49. From 0x30 to 0x37 the library talks to the board's ATtiny404 bridge.
_configPinintA GPIO the library drives low while it sends the front end's configuration and high otherwise. Not used at a 0x30 to 0x37 address. Default -1, none.
bool begin()returns bool

Starts I2C with Wire.begin(). At an address from 0x30 to 0x37 it checks that the bridge answers, sends it the ADC gain and data rate from the preset and the front end's configuration, waiting up to 500 ms for each reply. At any other address it starts the ADC at that address, sets its gain and its slowest data rate, then writes the front end's configuration.

Returnstrue if every step succeeded, false otherwise.

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

Adds up _numMeasurements results of getPPM(), waiting _secondsDelay seconds after each, and divides by _numMeasurements. It blocks until all readings are done.

ReturnsThe averaged 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 in parts per billion. The example shows it as a commented-out alternative.

ReturnsgetAveragedPPM() with the same arguments, multiplied by 1000.

Parameters

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

Putting it together​

Create the object with SENSOR_CO, call begin() once in setup() and check its result, then call getAveragedPPM() or getAveragedPPB() in your loop. Each call takes _numMeasurements times _secondsDelay seconds.