Average several readings
What this example shows
The averagedMeasurement example takes five readings, three seconds apart, and prints their average in ppm. One averaged value smooths out the small changes between single readings.
The example creates the sensor with only the cell preset, so the library uses
its default address, DEFAULT_ADC_ADDR (0x49). At that address the library
talks to the ADS1115 and the LMP91000 directly, not through the board's
ATtiny404 bridge, and the sketch's header asks for LMPEN connected to GND or
a GPIO. See the
library overview
for how the address decides this.
The example passes SENSOR_CO, the library's preset for a carbon monoxide
cell. For a nitric oxide reading, use SENSOR_NO, the preset for this
board's SGX-4NO-250 cell: 400 nA per ppm, a 120 kΩ amplifier gain and a
positive 12% bias.
The example code
The listing is the library's file at the pinned commit. Only comment lines are left out: the one that names the old connector, and the header lines that link other products' store pages.
/**
**************************************************
*
* @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, with SENSOR_NO (readings in clean air; yours will differ):
Sensor initialized successfully!
Sensor reading: 0.00584 PPM
Sensor reading: 0.00590 PPM
Sensor reading: 0.00595 PPM
Each Sensor reading: line is one average, with the
concentration in ppm to five decimals and PPM at the end. Each average takes
five readings with a 3 second pause after each, and the sketch then waits
another 2.5 seconds, so a new line appears about every 17.5 seconds. If
begin() fails, the sketch prints
ERROR: Can't init the sensor! Check connections! and stops there.
Functions used
ElectrochemicalGasSensor(sensorType _t, uint8_t _adcAddr = DEFAULT_ADC_ADDR, int _configPin = -1)returns ElectrochemicalGasSensorCreates the sensor object for one board. Nothing is sent over I2C until you call begin().
ReturnsThe sensor object.
Parameters
| Type | Name | Description |
|---|---|---|
sensorType | _t | The cell preset, SENSOR_CO in the example. SENSOR_NO is the one for this board's cell. The library also has SENSOR_NO2, SENSOR_SO2, SENSOR_O3, SENSOR_H2S, SENSOR_NH3 and SENSOR_CL2. |
uint8_t | _adcAddr | The I2C address. Left out in the example, so it is DEFAULT_ADC_ADDR, 0x49. For an address from 0x30 to 0x37 the library talks to the board's ATtiny404 bridge; for any other it talks to the ADS1115 at that address and to the LMP91000 directly. |
int | _configPin | The GPIO wired to LMPEN. Left out in the example (-1), so the library switches no pin. |
sensor.begin()returns boolStarts I2C with Wire.begin(). For an address from 0x30 to 0x37 it checks that the board's bridge answers and sends the ADC and front-end settings through it, waiting up to 500 ms for each step. For any other address it starts the ADS1115 at that address with the preset's gain and the slowest data rate, then configures the LMP91000 (gain, load, reference, internal zero, bias and mode), holding the _configPin GPIO low while it does when one is given.
Returnstrue when every step succeeded, false otherwise.
sensor.getAveragedPPM(uint8_t _numMeasurements = 5, uint8_t _secondsDelay = 2)returns doubleTakes _numMeasurements readings with getPPM(), waiting _secondsDelay seconds after each, and returns their average. It blocks until all readings are done. Each getPPM() takes one ADC reading, subtracts the internal zero (2.5 V times the preset's internal zero percentage), adds the zero calibration, divides by the amplifier gain and by the preset's nA per ppm; a negative reading counts as 0.
ReturnsThe averaged gas concentration in ppm.
Parameters
| Type | Name | Description |
|---|---|---|
uint8_t | _numMeasurements | How many readings to average, NUM_READINGS (5) in the example. |
uint8_t | _secondsDelay | Seconds to wait after each reading, SECS_BETWEEN_READINGS (3) in the example. |
sensor.getAveragedPPB(uint8_t _numMeasurements = 5, uint8_t _secondsDelay = 2)returns doubleThe alternative the sketch's comment offers: the same average as getAveragedPPM(), multiplied by 1000.
ReturnsThe averaged gas concentration in ppb.
Parameters
| Type | Name | Description |
|---|---|---|
uint8_t | _numMeasurements | How many readings to average. |
uint8_t | _secondsDelay | Seconds to wait after each reading. |
Putting it together
begin() runs once in setup(). If it returns false, the sketch prints the
error and stays in an endless loop. In loop(), getAveragedPPM() blocks
while it collects five readings, then the sketch prints the average with five
decimals and waits 2.5 seconds before the next one.