Skip to main content

Read in PPB

What this example shows​

The electrochemical-singleSensor.py example creates one sensor object, starts it, and prints a reading in PPB every 2.5 seconds. It passes no address, so the constructor uses its default, adc_addr=0x49.

The example opens I2C bus 0 with SCL on pin 22 and SDA on pin 21. Change them to the pins your controller's Qwiic connector or your wiring uses.

You do not need to wire LMPEN on this board, whatever the example's comments say: the board holds it low itself.

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

→

What this board prints with the example as written has not been measured yet. When this board was tested over Qwiic, an I2C scan found only one address, 0x30, and begin() failed for a sensor created at 0x49, the default this example uses. If begin() fails, the example prints ERROR: Can't init the sensor! Check connections! after its title line and stops there.

When begin() succeeds, the shell shows:

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

After that, a line Sensor reading: followed by a value with five decimals and PPB arrives every 2.5 seconds.

Functions used​

I2C(id, scl, sda)returns I2C

Creates the I2C bus object from MicroPython's machine module, which the sensor object uses. The example uses bus 0 with SCL on Pin(22) and SDA on Pin(21). I2C and Pin come with MicroPython and are not part of this library.

ReturnsThe I2C bus object.

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 to the board until you call begin(). The example passes the sensor configuration and the bus, and leaves adc_addr and config_pin at their defaults.

ReturnsThe sensor object.

Parameters

TypeNameDescription
SensorConfigsensor_typeThe front end and conversion settings, such as SENSOR_O3. The presets are defined in ElectrochemicalGasSensor.py.
I2Ci2cThe bus to use. When left out, the module picks pins only on boards it recognises (ESP32: bus 0, SCL 22, SDA 21; ESP8266: SCL 5, SDA 4) and raises an exception otherwise.
intadc_addrThe address the module talks to, 0x49 by default, as in this example. An address from 0x30 to 0x37 makes begin() use the bridge commands; any other address is taken as the ADS1115's own address.
intconfig_pinA GPIO pin the module holds high and pulls low while it writes the LMP91000 settings. None by default; not used for an address from 0x30 to 0x37.
sensor.begin()returns bool

Configures the ADC and the LMP91000 from the sensor configuration. For an adc_addr from 0x30 to 0x37 it checks that the board's bridge answers and sends it the ADC gain, the slowest ADC data rate and the LMP91000 settings, retrying each command for up to 500 ms. For any other address it sets up the ADS1115 at that address with the same gain and data rate and writes the LMP91000 settings directly, pulling config_pin low while it does when one is given.

ReturnsTrue when every step succeeded, False otherwise.

sensor.getPPB()returns float

Takes one reading and returns it in parts per billion: the getPPM() value multiplied by 1000.

ReturnsThe concentration in PPB.

sensor.getPPM()returns float

The commented-out alternative in the example. Takes one ADC reading and turns the voltage into a concentration: it subtracts the internal zero (a share of the board's 2.5 V reference), adds the configuration's zero calibration, divides by the TIA gain to get the cell current, then divides by the configuration's nanoamperes per PPM. A negative result is returned as 0.0.

ReturnsThe concentration in PPM.

Putting it together​

SENSOR_O3 is the preset for an SGX-403-20 ozone cell. The module has no preset for this board's SGX-4OX-ROHS oxygen cell yet, so the value the example prints is calculated with the ozone cell's settings. The custom configuration page shows how to write your own.