Read the gas concentration
What this example shows
The module's electrochemical-singleSensor.py example creates the sensor,
starts it, and prints the gas concentration in ppb (parts per billion) every
2.5 seconds.
The example uses the ozone preset, SENSOR_O3, and its comment asks you to
change it to match your sensor. SENSOR_NO2 is the module's preset for this
board's SGX-4NO2 cell; change it in the import line and in the line that
creates the sensor. The example opens I2C bus 0 with SCL on pin 22 and SDA
on pin 21; change them to match your controller.
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)
🔗Open the complete example
Examples/electrochemical-singleSensor.py at commit 623e1e6
Expected result:
Electrochemical Gas Sensor - Single Sensor Example
Sensor initialized successfully!
Then every 2.5 seconds the example prints a line that starts with
Sensor reading:, followed by the concentration in ppb with five decimals. If
begin() fails, it prints ERROR: Can't init the sensor! Check connections!
and waits in a loop.
Functions used
ElectrochemicalGasSensor(sensor_type, i2c=None, adc_addr=0x49, config_pin=None)returns ElectrochemicalGasSensorCreates the sensor object. With a config_pin it also sets that pin as an output and drives it high. Nothing is sent over I2C until you call begin().
ReturnsThe sensor object.
Parameters
| Type | Name | Description |
|---|---|---|
SensorConfig | sensor_type | The cell's preset. SENSOR_NO2 is the preset for this board's SGX-4NO2 cell. |
I2C | i2c | The I2C bus. With None, the module picks the pins by the port MicroPython reports: I2C bus 0 on pins 22 (SCL) and 21 (SDA) on the ESP32 port, scl=Pin(5), sda=Pin(4) on the ESP8266 port. On a port it does not recognise, it raises Board not recognized, please pass an I2C object manually. |
int | adc_addr | The address begin() talks to, 0x49 by default. From 0x30 to 0x37 the module sends its commands to the ATtiny404; from 0x48 to 0x4B it addresses the ADS1115 at adc_addr and the LMP91000 directly. The example leaves it at the default. |
int | config_pin | GPIO number for the LMP91000's enable pin (LMPEN), used only at 0x48 to 0x4B. None by default; the example does not pass one. |
sensor.begin()returns boolStarts the sensor and writes the preset to it. At 0x30 to 0x37 it checks that the ATtiny404 answers, then sends the ADC gain and data rate and the LMP91000 configuration, each step retrying until the board answers or 500 ms pass. At 0x48 to 0x4B it checks that the ADS1115 shows up in an I2C scan at adc_addr, sets its gain and its slowest data rate, and writes the configuration to the LMP91000 at 0x48. Call it once before reading.
ReturnsTrue when every step succeeded, False otherwise.
sensor.getPPB()returns floatTakes a new reading and returns the gas concentration in parts per billion: getPPM() multiplied by 1000.
ReturnsThe gas concentration in ppb.
sensor.getPPM()returns floatTakes a new reading of the gas channel and turns it into a gas concentration: the voltage minus the internal zero, plus the calibration offset, divided by the TIA gain and by the cell's sensitivity in the preset. A negative result is returned as 0.0. The example names it in a comment as the line to use when ppm suits your sensor better.
ReturnsThe gas concentration in ppm.
Putting it together
The example creates the I2C bus first and passes it to the sensor, so it does
not depend on the port's default pins. If begin() returns False, it prints
the error and stays in a loop. Otherwise each pass of the main loop calls
getPPB(), prints the value with five decimals, and waits 2.5 seconds.