Skip to main content

Read one sensor in PPB

What this example shows​

The electrochemical-singleSensor.py example starts one sensor with the ozone preset and prints the gas concentration in parts per billion (PPB) every 2.5 seconds.

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​

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-singleSensor.py
# AUTHOR: Fran Fodor @ Soldered
# BRIEF: Single sensor PPM reading example.
# 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

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

# If using a custom ADC address or a GPIO pin for LMP91000 MENB:
# sensor = ElectrochemicalGasSensor(SENSOR_O3, i2c, adc_addr=0x4B, config_pin=5)

# -------------------------------------------------------------------------
# Setup
# -------------------------------------------------------------------------
print("Electrochemical Gas Sensor - Single Sensor 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:
reading = sensor.getPPB()

# If PPM is more relevant for your sensor, use:
# reading = sensor.getPPM()

print("Sensor reading: {:.5f} PPB".format(reading))

time.sleep_ms(2500)

🔗Open the complete example

Examples/electrochemical-singleSensor.py at commit 623e1e6

→

The shell shows:

ShellMicroPython
Electrochemical Gas Sensor - Single Sensor Example
Sensor initialized successfully!

Then a line every 2.5 seconds: Sensor reading: , the concentration with five decimals, and PPB. 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.

getPPB()returns float

Takes one reading of the ADS1115's channel 0 (through the bridge for 0x30 to 0x37) and converts it to a concentration with the preset: it removes the internal zero offset, adds the zero calibration, divides by the amplifier gain and by the cell's sensitivity in nA per PPM, and multiplies by 1000. A negative result is returned as 0.

ReturnsThe gas concentration in PPB, getPPM() x 1000.

getPPM()returns float

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

ReturnsThe gas concentration in PPM.

Putting it together​

Create the sensor with the preset for your cell and the I2C bus, call begin() once and stop if it returns False, then call getPPB() or getPPM() for each reading.