Skip to main content

Read one sensor in ppb

What this example shows​

The electrochemical-singleSensor.py example starts one sensor and prints a reading in ppb (parts per billion) every 2.5 seconds.

The example creates the sensor with the cell preset and the I2C bus only, so the module uses its default address, 0x49. It opens I2C bus 0 with SCL on pin 22 and SDA on pin 21; change those pin numbers to your controller's I2C pins if they differ. At 0x49 the module talks to the ADS1115 and the LMP91000 directly, not through the board's ATtiny404 bridge, and the example's comments ask for LMPEN connected to GND or a GPIO. See the library overview for how the address decides this.

The example passes SENSOR_O3, the module's preset for an ozone cell. For a nitric oxide reading, use SENSOR_NO in both places (the import line and the ElectrochemicalGasSensor(...) line). SENSOR_NO is the preset for this board's SGX-4NO-250 cell: 400 nA per ppm, a 120 kΩ amplifier gain and a positive 12% bias.

The example code​

The listing is the module's file at the pinned commit. Only comment lines are left out: the header line that names another product's store page, and the connecting-diagram line that names an old board.

# 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, with SENSOR_NO (readings in clean air; yours will differ):

ShellMicroPython
Electrochemical Gas Sensor - Single Sensor Example
Sensor initialized successfully!
Sensor reading: 5.77892 PPB
Sensor reading: 6.29446 PPB
Sensor reading: 5.67951 PPB
Sensor reading: 5.90313 PPB

A Sensor reading: line comes every 2.5 seconds, with the concentration in ppb to five decimals and PPB at the end. If the board does not answer, the example prints ERROR: Can't init the sensor! Check connections! and stays in a loop.

Functions used​

I2C(0, scl=Pin(22), sda=Pin(21))returns I2C

Creates hardware I2C bus 0 on the given SCL and SDA pins. machine.I2C and machine.Pin come with MicroPython and are not part of the Soldered module (see docs.micropython.org, machine.I2C and machine.Pin).

ReturnsThe I2C bus object you pass to the sensor.

Parameters

TypeNameDescription
intidThe hardware I2C bus, 0 in the example.
PinsclThe clock pin, Pin(22) in the example.
PinsdaThe data pin, Pin(21) in the example.
ElectrochemicalGasSensor(sensor_type, i2c=None, adc_addr=0x49, config_pin=None)returns ElectrochemicalGasSensor

Creates the sensor object. Nothing is sent over I2C until you call begin(). With i2c=None the module opens a bus itself on the boards it recognises, including an ESP32, where it opens I2C(0, scl=Pin(22), sda=Pin(21)), and an ESP8266, where it opens I2C(scl=Pin(5), sda=Pin(4)). On a board it does not recognise it raises an exception.

ReturnsThe sensor object.

Parameters

TypeNameDescription
SensorConfigsensor_typeThe cell preset, SENSOR_O3 in the example. SENSOR_NO is the one for this board's cell. The module also has SENSOR_CO, SENSOR_NO2, SENSOR_SO2, SENSOR_H2S, SENSOR_NH3 and SENSOR_CL2.
I2Ci2cThe I2C bus the board is on.
intadc_addrThe I2C address. Left out in the example, so it is the module's default, 0x49; the example's comment shows the call with adc_addr=0x4B. For 0x30 to 0x37 the module talks to the board's ATtiny404 bridge; for any other address it talks to the ADS1115 at that address and to the LMP91000 directly.
intconfig_pinA GPIO for LMPEN (the LMP91000's MENB). Left out in the example (None), so the module switches no pin.
sensor.begin()returns bool

For an address from 0x30 to 0x37 it checks that the board's bridge answers and sends the ADC and front-end settings through it, waiting up to 500 ms for each step. For any other address it checks that the ADS1115 answers at adc_addr in an I2C scan, sets the preset's ADC gain and the slowest data rate, then configures the LMP91000 at 0x48 (gain, load, reference, internal zero, bias and mode), pulling config_pin low only during that configuration when one is given.

ReturnsTrue when every step succeeded, False otherwise.

sensor.getPPB()returns float

Takes one reading with getPPM() and multiplies it by 1000.

ReturnsThe gas concentration in ppb. A negative result is returned as 0.0.

sensor.getPPM()returns float

The alternative the example's comment offers. Reads ADC channel 0 (at 0x30 to 0x37, the bridge runs the conversion), subtracts the internal zero (2.5 V times the preset's internal zero percentage), adds the zero calibration, then divides by the amplifier gain and by the preset's nA per ppm.

ReturnsThe gas concentration in ppm. A negative result is returned as 0.0.

Putting it together​

begin() runs once before the loop. If it returns False, the example prints the error and waits forever, so you see the problem at once. In the loop, each getPPB() makes one fresh measurement and the example prints it with five decimals, then waits 2.5 seconds.