Read one sensor in ppb
What this example shows
The electrochemical-singleSensor.py example opens I2C, creates one sensor,
and prints the gas concentration in parts per billion (ppb) every 2.5 seconds.
Before you run it, change SENSOR_O3 to SENSOR_CO in the import line and
in the line that creates the sensor, as the example's comment says: "change
SENSOR_O3 to match your sensor type". SENSOR_O3 is the module's preset for
the ozone cell. This board carries the SGX-4CO carbon monoxide cell, and the
module's preset for it is SENSOR_CO. With the ozone preset, the module
converts the reading with the ozone cell's sensitivity and zero, so the
numbers are not CO concentrations.
The example opens I2C bus 0 with SCL on pin 22 and SDA on pin 21. Change
them to your controller's I2C pins if they differ.
What it needs on this board
The example creates the sensor without adc_addr, so the module uses its
default, 0x49. At an address outside 0x30 to 0x37 the module does not go
through the board's bridge: begin() looks for the ADS1115 at that address
with an I2C scan, then writes the LMP91000's configuration at 0x48, both on the
bus you pass it. On this board those two chips sit on the internal bus, which
the top header brings out as SDA2 and SCL2. The Qwiic connectors and
SDA1/SCL1 reach only the ATtiny404 bridge, and an I2C scan over Qwiic finds
only the bridge, at 0x30 as shipped. The example passes no config_pin, and
LMPEN is pulled to GND on the board; see
Hardware Details.
So wire the top header for this example, not the Qwiic cable: connect your
controller's SDA to SDA2, its SCL to SCL2, its 3.3 V pin to 3V3 and its
GND to GND. The listing's connecting diagram shows a Qwiic line, but over
Qwiic only the bridge answers.
The bridge's address, 0x30 to 0x37, is the one the Read the CO concentration example passes.
The example code
# 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)
The lines below are from a run of this example on this board, with the
controller wired to the header's SDA2, SCL2, 3V3 and GND pins as
described above.
Expected result in the Thonny shell:
Electrochemical Gas Sensor - Single Sensor Example
Sensor initialized successfully!
Sensor reading: 324.33022 PPB
Sensor reading: 325.16876 PPB
Sensor reading: 319.71396 PPB
Sensor reading: 301.64568 PPB
Then a line every 2.5 seconds: Sensor reading: , the concentration in ppb
with five decimals, and PPB. A result below zero is printed as 0.00000.
If begin() fails, the shell shows
ERROR: Can't init the sensor! Check connections! and the script stops there.
Functions used
ElectrochemicalGasSensor(sensor_type, i2c=None, adc_addr=0x49, config_pin=None)returns ElectrochemicalGasSensorCreates the sensor object. The example passes the preset and the I2C bus and leaves adc_addr and config_pin at their defaults.
ReturnsThe sensor object.
Parameters
| Parameter | Type | Description |
|---|---|---|
sensor_type | SensorConfig | The cell's preset. For this board's SGX-4CO cell the module's preset is SENSOR_CO. |
i2c | machine.I2C | An initialised I2C bus. With None, the module opens I2C 0 on pins 22 and 21 on an ESP32, or pins 5 and 4 on an ESP8266, and raises Board not recognized, please pass an I2C object manually on any other board. |
adc_addr | int | The I2C address. Default 0x49. From 0x30 to 0x37 the module talks to the board's ATtiny404 bridge. |
config_pin | int or None | A GPIO the module drives low while it sends the front end's configuration and high otherwise. Not used at a 0x30 to 0x37 address. Default None. |
begin()returns boolStarts the sensor. At an adc_addr from 0x30 to 0x37 it checks that the bridge answers, sends it the ADC gain and data rate from the preset, then the front end's configuration, retrying a bus error until a 500 ms timeout for each step. At any other adc_addr it checks that the ADC answers at that address, sets its gain and data rate, then writes the front end's configuration.
ReturnsTrue if every step succeeded, False otherwise.
getPPB()returns floatMakes one measurement and returns it in parts per billion.
ReturnsgetPPM() multiplied by 1000.
getPPM()returns floatReads the ADC, takes off the internal zero and adds the zero calibration, then converts the cell current to ppm with the preset's sensitivity. The example shows it as a commented-out alternative.
ReturnsThe gas concentration in ppm; 0.0 when the result would be negative.
Putting it together
Open the I2C bus, create the object with SENSOR_CO, the preset for this
board's cell, call begin() once and check it, then call getPPB() (or
getPPM()) in your loop.