Skip to main content

Average several readings

What this example shows​

The module's electrochemical-averagedMeasurement.py example takes five readings three seconds apart, averages them, and prints the result in ppb (parts per billion).

The example uses the ozone preset, SENSOR_O3, and its comment asks you to change it to match your sensor. SENSOR_NO2 is the module's preset for this board's SGX-4NO2 cell; change it in the import line and in the line that creates the sensor. The example opens I2C bus 0 with SCL on pin 22 and SDA on pin 21; change them to match your controller. NUM_READINGS and SECS_BETWEEN_READINGS at the top set how many readings go into each average and how long to wait between them.

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

Examples/electrochemical-averagedMeasurement.py at commit 623e1e6

→

Expected result:

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

Then the example prints a line that starts with Sensor reading:, followed by the averaged concentration in ppb with five decimals. Each average waits three seconds after each of its five readings, and the loop then waits another 2.5 seconds, so a new line appears at least every 17.5 seconds. If begin() fails, it prints ERROR: Can't init the sensor! Check connections! and waits in a loop.

Functions used​

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

Creates the sensor object. With a config_pin it also sets that pin as an output and drives it high. Nothing is sent over I2C until you call begin().

ReturnsThe sensor object.

Parameters

TypeNameDescription
SensorConfigsensor_typeThe cell's preset. SENSOR_NO2 is the preset for this board's SGX-4NO2 cell.
I2Ci2cThe I2C bus. With None, the module picks the pins by the port MicroPython reports: I2C bus 0 on pins 22 (SCL) and 21 (SDA) on the ESP32 port, scl=Pin(5), sda=Pin(4) on the ESP8266 port. On a port it does not recognise, it raises Board not recognized, please pass an I2C object manually.
intadc_addrThe address begin() talks to, 0x49 by default; the example leaves it at the default. From 0x30 to 0x37 the module sends its commands to the ATtiny404; from 0x48 to 0x4B it addresses the ADS1115 at adc_addr and the LMP91000 directly.
intconfig_pinGPIO number for the LMP91000's enable pin (LMPEN), used only at 0x48 to 0x4B. None by default; the example does not pass one.
sensor.begin()returns bool

Starts the sensor and writes the preset to it. At 0x30 to 0x37 it checks that the ATtiny404 answers, then sends the ADC gain and data rate and the LMP91000 configuration, each step retrying until the board answers or 500 ms pass. At 0x48 to 0x4B it checks that the ADS1115 shows up in an I2C scan at adc_addr, sets its gain and its slowest data rate, and writes the configuration to the LMP91000 at 0x48. Call it once before reading.

ReturnsTrue when every step succeeded, False otherwise.

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

The same average as getAveragedPPM(), multiplied by 1000. It blocks until every reading is taken.

ReturnsThe averaged gas concentration in ppb.

Parameters

TypeNameDescription
intnum_measurementsHow many readings to average. 5 by default; the example passes NUM_READINGS (5).
intseconds_delaySeconds to wait after each reading. 2 by default; the example passes SECS_BETWEEN_READINGS (3).
sensor.getAveragedPPM(num_measurements=5, seconds_delay=2)returns float

Calls getPPM() num_measurements times, waits seconds_delay seconds after each reading, and returns the average. It blocks until every reading is taken. The example names it in a comment as the line to use when ppm suits your sensor better.

ReturnsThe averaged gas concentration in ppm.

Parameters

TypeNameDescription
intnum_measurementsHow many readings to average. 5 by default.
intseconds_delaySeconds to wait after each reading. 2 by default.

Putting it together​

The example creates the I2C bus first and passes it to the sensor, so it does not depend on the port's default pins. If begin() returns False, it prints the error and stays in a loop. Otherwise each pass of the main loop calls getAveragedPPB(), which takes all five readings before it returns, prints the average with five decimals, and waits 2.5 seconds.