Averaged reading
What this example shows
The electrochemical-averagedMeasurement.py example averages several readings
before it prints one. It takes NUM_READINGS (5) readings with
SECS_BETWEEN_READINGS (3) seconds between them, prints the average in PPB,
then waits 2.5 seconds and starts again. It passes no address, so the
constructor uses its default, adc_addr=0x49.
The example opens I2C bus 0 with SCL on pin 22 and SDA on pin 21. Change
them to the pins your controller's Qwiic connector or your wiring uses.
You do not need to wire LMPEN on this board, whatever the example's comments
say: the board holds it low itself.
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
What this board prints with the example as written has not been measured yet.
When this board was tested over Qwiic, an I2C scan found only one address,
0x30, and begin() failed for a sensor created at 0x49, the default this
example uses. If begin() fails, the example prints
ERROR: Can't init the sensor! Check connections! after its title line and
stops there.
When begin() succeeds, the shell shows:
Electrochemical Gas Sensor - Averaged Measurement Example
Sensor initialized successfully!
After that, a line Sensor reading: followed by a value with five decimals
and PPB arrives once per round of five readings.
Functions used
I2C(id, scl, sda)returns I2CCreates the I2C bus object from MicroPython's machine module, which the sensor object uses. The example uses bus 0 with SCL on Pin(22) and SDA on Pin(21). I2C and Pin come with MicroPython and are not part of this library.
ReturnsThe I2C bus object.
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 to the board until you call begin(). The example passes the sensor configuration and the bus, and leaves adc_addr and config_pin at their defaults.
ReturnsThe sensor object.
Parameters
| Type | Name | Description |
|---|---|---|
SensorConfig | sensor_type | The front end and conversion settings, such as SENSOR_O3. The presets are defined in ElectrochemicalGasSensor.py. |
I2C | i2c | The bus to use. When left out, the module picks pins only on boards it recognises (ESP32: bus 0, SCL 22, SDA 21; ESP8266: SCL 5, SDA 4) and raises an exception otherwise. |
int | adc_addr | The address the module talks to, 0x49 by default, as in this example. An address from 0x30 to 0x37 makes begin() use the bridge commands; any other address is taken as the ADS1115's own address. |
int | config_pin | A GPIO pin the module holds high and pulls low while it writes the LMP91000 settings. None by default; not used for an address from 0x30 to 0x37. |
sensor.begin()returns boolConfigures the ADC and the LMP91000 from the sensor configuration. For an adc_addr from 0x30 to 0x37 it checks that the board's bridge answers and sends it the ADC gain, the slowest ADC data rate and the LMP91000 settings, retrying each command for up to 500 ms. For any other address it sets up the ADS1115 at that address with the same gain and data rate and writes the LMP91000 settings directly, pulling config_pin low while it does when one is given.
ReturnsTrue when every step succeeded, False otherwise.
sensor.getAveragedPPB(num_measurements=5, seconds_delay=2)returns floatReturns the average of several readings in parts per billion: the getAveragedPPM() result multiplied by 1000. It blocks until every reading is taken.
ReturnsThe averaged 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 commented-out alternative in the example. Takes num_measurements getPPM() readings, sleeps seconds_delay seconds after each one, and returns their mean. A getPPM() reading below zero counts as 0.0.
ReturnsThe averaged 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
SENSOR_O3 is the preset for an SGX-403-20 ozone cell. The module has no
preset for this board's SGX-4OX-ROHS oxygen cell yet, so the value the example
prints is calculated with the ozone cell's settings. The
custom configuration
page shows how to write your own.