Skip to main content

Averaged measurement

What this example shows​

The library's electrochemical-averagedMeasurement.py example takes five readings three seconds apart and prints their average in ppb (parts per billion). Averaging smooths out noise in a small signal.

The example creates the sensor with the ozone cell preset, SENSOR_O3. SENSOR_SO2 is the library's preset for this board's SO2 cell: change SENSOR_O3 to SENSOR_SO2 in the import line and in the line that creates sensor to get SO2 values. The script creates the I2C bus on GPIO 22 (SCL) and GPIO 21 (SDA); change those numbers if your board uses other pins.

On this board the LMP91000 and ADS1115 sit on the board's own bus 2, and your controller reaches them through the board's address on the Qwiic bus: 0x30 as shipped, 0x30 to 0x37 with the ADDRESS switch. The example passes no adc_addr, so it uses the module's default, 0x49, which talks to an ADS1115 directly. Pass the board's address as adc_addr, for example sensor = ElectrochemicalGasSensor(SENSOR_SO2, i2c, adc_addr=0x30). Where the connecting diagram says LMPEN goes to GND or a GPIO pin, on this board LMPEN is already pulled to GND by a 10 kΩ resistor and also runs to the on-board microcontroller, and at the board's addresses the module does not use config_pin (see Hardware Details).

The listing is the library's file at the pinned commit, apart from two comment-only lines, which link to another Soldered product or name a controller board in the connecting diagram, and are left out.

The example code​

# FILE: electrochemical-averagedMeasurement.py
# AUTHOR: Fran Fodor @ Soldered
# BRIEF: Averaged PPM reading example using the built-in averaging function.
# LAST UPDATED: 2026-05-21

# Connecting diagram:
#
# Qwiic------------------------>Qwiic
# LMPEN------------------------>GND or GPIO pin (see config_pin parameter below)

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

electrochemical-averagedMeasurement.py at the pinned commit

→

Expected result:

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

Then a Sensor reading: line with the averaged concentration in ppb appears for every set of five readings. Each set takes 15 seconds, and the script waits another 2.5 seconds before the next set.

Functions used​

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

Creates the sensor object from a cell preset, an I2C object, an address and an optional enable pin. The example passes only the preset and the I2C object, so the address is the default, 0x49.

ReturnsThe sensor object.

Parameters

ParameterTypeDescription
sensor_typeSensorConfigThe cell preset. SENSOR_SO2 is the preset for this board's cell.
i2cI2CAn initialised machine.I2C object.
adc_addrintThe address the module talks to. 0x30 to 0x37 selects the module's bridge mode, which talks to the board's on-board microcontroller; any other value talks to an ADS1115 directly at that address. Default 0x49.
config_pinintGPIO for the LMP91000's enable line. Not used at addresses 0x30 to 0x37. Default None.
begin()returns bool

Checks that the board answers and sends the ADC and LMP91000 settings from the preset to it.

ReturnsTrue when the board answered and took every setting, False otherwise.

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

Averages several readings and returns the result in parts per billion. It sleeps the given number of seconds after each reading and blocks until it is done.

ReturnsThe averaged gas concentration in ppb.

Parameters

ParameterTypeDescription
num_measurementsintHow many readings to average. Default 5.
seconds_delayintSeconds to wait after each reading. Default 2.
getAveragedPPM(num_measurements=5, seconds_delay=2)returns float

The same average in ppm. The example shows it in a comment as an alternative.

ReturnsThe averaged gas concentration in ppm.

Parameters

ParameterTypeDescription
num_measurementsintHow many readings to average. Default 5.
seconds_delayintSeconds to wait after each reading. Default 2.

Putting it together​

The SO2 cell's range is 0 to 20 ppm, so ppm is often the handier unit. To print ppm, swap the two reading = lines and change PPB to PPM in the print("Sensor reading: ...") line, so the label matches the value. The call blocks for NUM_READINGS x SECS_BETWEEN_READINGS seconds.