Skip to main content

Average several readings

What this example shows​

The electrochemical-averagedMeasurement.py example starts one sensor with the ozone preset and prints the average of five readings, taken 3 seconds apart, in parts per billion (PPB).

Wire your controller to the top header for this example, not to a Qwiic connector: the controller's SDA to SDA2, its SCL to SCL2, its 3.3 V supply to 3V3 and its ground to GND. At this address the library talks to the ADS1115 and the LMP91000 directly on SDA2 and SCL2; the board's ATtiny404 bridge at 0x30 is not used. Over Qwiic, only the bridge example in the Quick Start works.

The example code​

NUM_READINGS sets how many readings go into each average, and SECS_BETWEEN_READINGS how many seconds the library waits after each one. The example opens I2C bus 0 with SCL on pin 22 and SDA on pin 21. Change SENSOR_O3 to the preset for your cell, as its comment lists.

The connecting diagram in the example's comments wires LMPEN to GND or a GPIO. On this board, LMPEN goes to the header and to the board's ATtiny404.

# 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

→

The shell shows:

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

Then one Sensor reading: line per average, with five decimals and PPB. Each average takes five readings with 3 seconds after each, so about 15 seconds, and the script pauses 2.5 seconds before the next one. If begin() fails, the script prints ERROR: Can't init the sensor! Check connections! and waits.

Functions used​

I2C and Pin come from MicroPython's own machine module, and time.sleep_ms() from its time module. The functions below are the library's.

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

Creates the sensor object for one board. The library reads adc_addr to decide how to talk to it: for an address from 0x30 to 0x37 it uses the protocol of the ATtiny404 bridge; for any other address it talks to an ADS1115 at that address and to the LMP91000 at 0x48 itself.

ReturnsThe sensor object.

Parameters

NameTypeDescription
sensor_typeSensorConfigThe preset for your cell: SENSOR_CO, SENSOR_NO2, SENSOR_SO2, SENSOR_O3, SENSOR_NO, SENSOR_H2S, SENSOR_NH3, SENSOR_CL2, or your own SensorConfig.
i2cmachine.I2CThe I2C bus. When left out, the library opens I2C bus 0 with SCL on pin 22 and SDA on pin 21 on an ESP32 port, or I2C with SCL on pin 5 and SDA on pin 4 on an ESP8266 port, and raises an exception on any other board.
adc_addrintThe address begin() uses. 0x30 to 0x37 selects the bridge protocol; this board answers at 0x30 out of the box, or at the address its switch sets. Any other value is taken as an ADS1115 address. The default is 0x49.
config_pinintA GPIO number for the LMP91000 enable line. The library holds it high and pulls it low only while it writes the LMP91000's registers itself; for an address from 0x30 to 0x37 it is not used. None by default.
begin()returns bool

Sets up the connection for adc_addr and configures the LMP91000 with the preset. For 0x30 to 0x37 it checks that the bridge answers, sends the ADC gain and has the bridge write the LMP91000's registers, each step giving up after 500 ms. For any other address it checks with an I2C scan that a device answers at adc_addr, then writes the LMP91000's registers at 0x48 when its status reads ready.

ReturnsTrue when every step succeeded, False otherwise.

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

Takes num_measurements readings with getPPM(), waiting seconds_delay seconds after each one, and returns their average in parts per billion. It blocks until it is done.

ReturnsThe averaged gas concentration in PPB, getAveragedPPM() x 1000.

Parameters

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

The same average in parts per million. The example shows it as a commented-out alternative.

ReturnsThe averaged gas concentration in PPM.

Parameters

NameTypeDescription
num_measurementsintHow many readings to average. 5 by default.
seconds_delayintSeconds to wait after each reading. 2 by default.

Putting it together​

Create the sensor with the preset for your cell and call begin() once. Then call getAveragedPPB() or getAveragedPPM() with the number of readings and the wait between them. Each call blocks for about num_measurements x seconds_delay seconds, so your script does nothing else meanwhile.