Skip to main content

Average several readings

What this example shows​

The electrochemical-averagedMeasurement.py example takes five readings three seconds apart, averages them, prints the average in PPB, waits 2.5 seconds and starts again.

The example creates the sensor at the module's default address, 0x49.

Before you run it:

  • The example is written with SENSOR_O3, the module's ozone preset, so its values are not H2S values. Do not switch it to SENSOR_H2S unchanged: that preset is written for the SGX-4H2S-100 cell (1200 nA per ppm). The datasheet of this board's SGX-4H2S-300 cell gives an output signal of 460 ± 140 nA per ppm, so with that preset the reading comes out about 2.6 times too low. Pass a custom SensorConfig with the SENSOR_H2S settings and 460 nA per ppm instead, as Quick Start shows.
  • Change Pin(22) and Pin(21) to your controller's SCL and SDA pins if they differ.
  • NUM_READINGS and SECS_BETWEEN_READINGS set how many readings are averaged and how many seconds pass between them.

The example code​

# FILE: electrochemical-averagedMeasurement.py

from machine import I2C, Pin
import time
from ElectrochemicalGasSensor import ElectrochemicalGasSensor, SENSOR_O3

# How many readings to average and how many seconds to wait between them
NUM_READINGS = 5
SECS_BETWEEN_READINGS = 3

# Initialize I2C
i2c = I2C(0, scl=Pin(22), sda=Pin(21))

# Create sensor object - change SENSOR_O3 to match your sensor type
# Available types: SENSOR_CO, SENSOR_NO2, SENSOR_SO2, SENSOR_O3,
# SENSOR_NO, SENSOR_H2S, SENSOR_NH3, SENSOR_CL2
sensor = ElectrochemicalGasSensor(SENSOR_O3, i2c)

# -------------------------------------------------------------------------
# Setup
# -------------------------------------------------------------------------
print("Electrochemical Gas Sensor - Averaged Measurement Example")

if not sensor.begin():
print("ERROR: Can't init the sensor! Check connections!")
while True:
time.sleep_ms(100)

print("Sensor initialized successfully!")

# -------------------------------------------------------------------------
# Main loop
# -------------------------------------------------------------------------
while True:
# getAveragedPPM is a blocking call — it waits between each reading
reading = sensor.getAveragedPPB(NUM_READINGS, SECS_BETWEEN_READINGS)

# If PPM is more relevant for your sensor, use:
# reading = sensor.getAveragedPPM(NUM_READINGS, SECS_BETWEEN_READINGS)

print("Sensor reading: {:.5f} PPB".format(reading))

time.sleep_ms(2500)

Open the complete example

Expected result:

ShellMicroPython
Electrochemical Gas Sensor - Averaged Measurement Example
Sensor initialized successfully!

Then a line starting with Sensor reading: follows with the averaged concentration in PPB to five decimals. With the example's settings, each average takes five readings with a 3-second wait after each, then the script waits 2.5 seconds, so a new line appears about every 17.5 seconds. If the board does not answer, the script prints ERROR: Can't init the sensor! Check connections! and waits.

Functions used​

ElectrochemicalGasSensor(sensor_type, i2c=None, adc_addr=0x49, config_pin=None)returns ElectrochemicalGasSensor

Creates the sensor object. The sensor type holds the front-end and conversion settings for one gas cell. The address decides how begin() talks to the board: an address from 0x30 to 0x37 goes through the board's ATtiny404; any other address, such as the default 0x49, goes to an ADS1115 at that address and to the LMP91000 directly.

ReturnsThe sensor object.

Parameters

NameTypeDescription
sensor_typeSensorConfigPreset from the module (SENSOR_CO, SENSOR_NO2, SENSOR_SO2, SENSOR_O3, SENSOR_NO, SENSOR_H2S, SENSOR_NH3, SENSOR_CL2) or a custom SensorConfig. For this board's cell, pass a custom one with 460 nA per ppm.
i2cmachine.I2CThe I2C bus the board is on. Without it, the module opens one itself only on the boards it recognises: ESP32 builds of MicroPython and one Soldered board's own build (I2C bus 0, SCL 22, SDA 21), and ESP8266 builds (SCL 5, SDA 4). On any other board it raises Board not recognized, please pass an I2C object manually.
adc_addrintThe address begin() talks to. This example leaves it out, so the default, 0x49, is used.
config_pinintA GPIO the module holds high and pulls low while it configures the LMP91000 directly. Unused at addresses from 0x30 to 0x37. This example leaves it out.
begin()returns bool

With an address from 0x30 to 0x37, checks that the board's ATtiny404 answers, sends it the ADC gain from the sensor type and the slowest data rate, then has it configure the LMP91000 front end; each command is retried for up to 500 ms. With any other address, checks that an ADS1115 answers at that address, sets its gain and the slowest data rate, then configures the LMP91000 directly. Call it once before reading.

ReturnsTrue when every step succeeded, False otherwise.

getAveragedPPB(num_measurements=5, seconds_delay=2)returns float

Returns getAveragedPPM() with the same arguments, multiplied by 1000. It blocks until every reading is taken.

ReturnsAveraged gas concentration in PPB.

Parameters

NameTypeDescription
num_measurementsintHow many readings to average. The example passes NUM_READINGS (5).
seconds_delayintSeconds to wait after each reading. The example passes SECS_BETWEEN_READINGS (3).
getAveragedPPM(num_measurements=5, seconds_delay=2)returns float

The commented-out alternative in the example. Takes num_measurements readings with getPPM(), waiting seconds_delay seconds after each, and returns their mean. It blocks until every reading is taken.

ReturnsAveraged gas concentration in PPM.

Parameters

NameTypeDescription
num_measurementsintHow many readings to average.
seconds_delayintSeconds to wait after each reading.

This example also uses I2C and Pin from MicroPython's machine module, which is not part of the Soldered ElectrochemicalGasSensor module.

Putting it together​

Open the I2C bus, create one ElectrochemicalGasSensor with a sensor type, call begin() once, then call getAveragedPPB() or getAveragedPPM() with the number of readings and the wait between them. Each call blocks for about num_measurements times seconds_delay seconds.