Skip to main content

Averaged measurement

What this example shows​

The electrochemical-averagedMeasurement.py example averages five readings taken three seconds apart and prints the result in parts per billion (ppb).

Before you run it, change SENSOR_O3 to SENSOR_CO in the import line and in the line that creates the sensor, as the example's comment says: "change SENSOR_O3 to match your sensor type". SENSOR_O3 is the module's preset for the ozone cell. This board carries the SGX-4CO carbon monoxide cell, and the module's preset for it is SENSOR_CO. With the ozone preset, the module converts the readings with the ozone cell's sensitivity and zero, so the averages are not CO concentrations.

The example opens I2C bus 0 with SCL on pin 22 and SDA on pin 21. Change them to your controller's I2C pins if they differ. 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 without adc_addr, so the module uses its default, 0x49. At an address outside 0x30 to 0x37 the module does not go through the board's bridge: begin() looks for the ADS1115 at that address with an I2C scan, then writes the LMP91000's configuration at 0x48, both on the bus you pass it. 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. The listing's connecting diagram shows a Qwiic line, but 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​

# 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)

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 Thonny shell:

ShellMicroPython
Electrochemical Gas Sensor - Averaged Measurement Example
Sensor initialized successfully!
Sensor reading: 291.79010 PPB
Sensor reading: 300.63653 PPB
Sensor reading: 294.22868 PPB

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 ppb with five decimals, and PPB. If begin() fails, the shell shows ERROR: Can't init the sensor! Check connections! and the script stops there.

Functions used​

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

Creates the sensor object. The example passes the preset and the I2C bus and leaves adc_addr and config_pin at their defaults.

ReturnsThe sensor object.

Parameters

ParameterTypeDescription
sensor_typeSensorConfigThe cell's preset. For this board's SGX-4CO cell the module's preset is SENSOR_CO.
i2cmachine.I2CAn initialised I2C bus. With None, the module opens I2C 0 on pins 22 and 21 on an ESP32, or pins 5 and 4 on an ESP8266, and raises Board not recognized, please pass an I2C object manually on any other board.
adc_addrintThe I2C address. Default 0x49. From 0x30 to 0x37 the module talks to the board's ATtiny404 bridge.
config_pinint or NoneA GPIO the module drives low while it sends the front end's configuration and high otherwise. Not used at a 0x30 to 0x37 address. Default None.
begin()returns bool

Starts the sensor. At an adc_addr from 0x30 to 0x37 it checks that the bridge answers, sends it the ADC gain and data rate from the preset, then the front end's configuration, retrying a bus error until a 500 ms timeout for each step. At any other adc_addr it checks that the ADC answers at that address, sets its gain and data rate, then writes the front end's configuration.

ReturnsTrue if every step succeeded, False otherwise.

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

Takes num_measurements readings, waiting seconds_delay seconds after each one, and returns their average in parts per billion. It blocks until all readings are done.

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

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: it adds up num_measurements results of getPPM(), sleeping seconds_delay seconds after each, and divides by num_measurements. The example shows it as a commented-out alternative.

ReturnsThe averaged 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​

Create the object with SENSOR_CO, the preset for this board's cell, call begin() once and check it, then call getAveragedPPB() or getAveragedPPM() in your loop. Each call takes num_measurements times seconds_delay seconds.