Average several readings
What this example shows
The electrochemical-averagedMeasurement.py example takes five readings, three
seconds apart, and prints their average in ppb (parts per billion). One averaged
value smooths out the small changes between single readings.
The example creates the sensor with the cell preset and the I2C bus only, so
the module uses its default address, 0x49. It opens I2C bus 0 with SCL on
pin 22 and SDA on pin 21; change those pin numbers to your controller's I2C
pins if they differ. At 0x49 the module talks to the ADS1115 and the LMP91000
directly, not through the board's ATtiny404 bridge, and the example's comments
ask for LMPEN connected to GND or a GPIO. See the
library overview
for how the address decides this.
The example passes SENSOR_O3, the module's preset for an ozone cell. For a
nitric oxide reading, use SENSOR_NO in both places (the import line and
the ElectrochemicalGasSensor(...) line). SENSOR_NO is 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 module's file at the pinned commit. Only comment lines are left out: the header line that names another product's store page, and the connecting-diagram line that names an old board.
# 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, with SENSOR_NO (readings in clean air; yours will differ):
Electrochemical Gas Sensor - Averaged Measurement Example
Sensor initialized successfully!
Sensor reading: 5.94850 PPB
Sensor reading: 5.85231 PPB
Sensor reading: 6.31483 PPB
Each Sensor reading: line is one average, with the
concentration in ppb to five decimals and PPB at the end. Each average takes
five readings with a 3 second pause after each, and the example then waits
another 2.5 seconds, so a new line appears about every 17.5 seconds. If
begin() fails, the example prints
ERROR: Can't init the sensor! Check connections! and stays in a loop.
Functions used
I2C(0, scl=Pin(22), sda=Pin(21))returns I2CCreates hardware I2C bus 0 on the given SCL and SDA pins. machine.I2C and machine.Pin come with MicroPython and are not part of the Soldered module (see docs.micropython.org, machine.I2C and machine.Pin).
ReturnsThe I2C bus object you pass to the sensor.
Parameters
| Type | Name | Description |
|---|---|---|
int | id | The hardware I2C bus, 0 in the example. |
Pin | scl | The clock pin, Pin(22) in the example. |
Pin | sda | The data pin, Pin(21) in the example. |
ElectrochemicalGasSensor(sensor_type, i2c=None, adc_addr=0x49, config_pin=None)returns ElectrochemicalGasSensorCreates the sensor object. Nothing is sent over I2C until you call begin(). With i2c=None the module opens a bus itself on the boards it recognises, including an ESP32, where it opens I2C(0, scl=Pin(22), sda=Pin(21)), and an ESP8266, where it opens I2C(scl=Pin(5), sda=Pin(4)). On a board it does not recognise it raises an exception.
ReturnsThe sensor object.
Parameters
| Type | Name | Description |
|---|---|---|
SensorConfig | sensor_type | The cell preset, SENSOR_O3 in the example. SENSOR_NO is the one for this board's cell. The module also has SENSOR_CO, SENSOR_NO2, SENSOR_SO2, SENSOR_H2S, SENSOR_NH3 and SENSOR_CL2. |
I2C | i2c | The I2C bus the board is on. |
int | adc_addr | The I2C address. Left out in the example, so it is the module's default, 0x49. For 0x30 to 0x37 the module talks to the board's ATtiny404 bridge; for any other address it talks to the ADS1115 at that address and to the LMP91000 directly. |
int | config_pin | A GPIO for LMPEN. Left out in the example (None), so the module switches no pin. |
sensor.begin()returns boolFor 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 checks that the ADS1115 answers at adc_addr in an I2C scan, sets the preset's ADC gain and the slowest data rate, then configures the LMP91000 at 0x48 (gain, load, reference, internal zero, bias and mode), pulling config_pin low only during that configuration when one is given.
ReturnsTrue when every step succeeded, False otherwise.
sensor.getAveragedPPB(num_measurements=5, seconds_delay=2)returns floatTakes num_measurements readings with getPPM(), waiting seconds_delay seconds after each, and returns their average times 1000. It blocks until all readings are done.
ReturnsThe averaged gas concentration in ppb.
Parameters
| Type | Name | Description |
|---|---|---|
int | num_measurements | How many readings to average, NUM_READINGS (5) in the example. |
int | seconds_delay | Seconds to wait after each reading, SECS_BETWEEN_READINGS (3) in the example. |
sensor.getAveragedPPM(num_measurements=5, seconds_delay=2)returns floatThe alternative the example's comment offers: the same average in ppm. Each reading is one getPPM(): it reads ADC channel 0 (at 0x30 to 0x37, the bridge runs the conversion), subtracts the internal zero (2.5 V times the preset's internal zero percentage), adds the zero calibration and divides by the amplifier gain and by the preset's nA per ppm. A negative reading counts as 0.0.
ReturnsThe averaged gas concentration in ppm.
Parameters
| Type | Name | Description |
|---|---|---|
int | num_measurements | How many readings to average. |
int | seconds_delay | Seconds to wait after each reading. |
Putting it together
begin() runs once before the loop. If it returns False, the example prints
the error and waits forever, so you see the problem at once. In the loop,
getAveragedPPB() blocks while it collects five readings, then the example
prints the average and waits 2.5 seconds before the next one.