Skip to main content

Custom configuration

What this example shows​

The library's electrochemical-customConfig.py example builds its own SensorConfig: the cell's sensitivity, a zero offset, the ADC range and the LMP91000 settings (gain, load resistor, reference, internal zero, bias, operating mode). begin() sends those settings to the board. The script creates the I2C bus on GPIO 22 (SCL) and GPIO 21 (SDA).

The module's own SENSOR_SO2 preset uses nano_amperes_per_ppm=400.0, internal_zero_calibration=0, ADS_GAIN_4_096V, TIA_GAIN_120_KOHM, RLOAD_10_OHM, REF_EXTERNAL, INTERNAL_ZERO_20_PERCENT, BIAS_SIGN_POSITIVE, BIAS_0_PERCENT, FET_SHORT_DISABLED and OP_MODE_3LEAD_AMP_CELL. The example's values are for a different cell, so import the names you need and start from these for the SO2 cell.

On this board the LMP91000 and ADS1115 sit on the board's own bus 2, and your controller reaches them through the board's address on the Qwiic bus: 0x30 as shipped, 0x30 to 0x37 with the ADDRESS switch. The example passes no adc_addr, so it uses the module's default, 0x49, which talks to an ADS1115 directly. Pass the board's address as adc_addr, for example sensor = ElectrochemicalGasSensor(SENSOR_CUSTOM, i2c, adc_addr=0x30). Where the connecting diagram says LMPEN goes to a GPIO pin, on this board LMPEN is already pulled to GND by a 10 kΩ resistor and also runs to the on-board microcontroller, and at the board's addresses the module does not use config_pin (see Hardware Details).

The listing is the library's file at the pinned commit, apart from two comment-only lines, which link to another Soldered product or name a controller board in the connecting diagram, and are left out.

The example code​

# FILE: electrochemical-customConfig.py
# AUTHOR: Fran Fodor @ Soldered
# BRIEF: Custom LMP91000 configuration example for any electrochemical sensor.
# LAST UPDATED: 2026-05-21

# Connecting diagram:
#
# Qwiic------------------------>Qwiic
# LMPEN------------------------>GPIO pin (see config_pin parameter below)

from machine import I2C, Pin
import time
from ElectrochemicalGasSensor import (
ElectrochemicalGasSensor, SensorConfig,
TIA_GAIN_35_KOHM, RLOAD_10_OHM, REF_EXTERNAL,
INTERNAL_ZERO_67_PERCENT, BIAS_SIGN_NEGATIVE, BIAS_0_PERCENT,
FET_SHORT_DISABLED, OP_MODE_3LEAD_AMP_CELL,
)
from ads1115 import ADS_GAIN_2_048V

# Custom sensor configuration — edit these values to match your sensor's datasheet.
#
# nanoAmperesPerPPM: Sensitivity from the sensor datasheet (nA/PPM)
# internalZeroCalibration: Voltage offset added to zero the reading in clean air
# adsGain: ADS1115 full-scale range (ADS_GAIN_6_144V .. ADS_GAIN_0_256V)
# tia_gain: TIA feedback resistor (TIA_GAIN_2_75_KOHM .. TIA_GAIN_350_KOHM)
# rload: Load resistor (RLOAD_10_OHM .. RLOAD_100_OHM)
# ref_source: Reference voltage source (REF_INTERNAL / REF_EXTERNAL)
# internal_zero: Internal zero percentage (INTERNAL_ZERO_20/50/67_PERCENT or _BYPASSED)
# bias_sign: Bias polarity (BIAS_SIGN_NEGATIVE / BIAS_SIGN_POSITIVE)
# bias: Bias percentage of Vref (BIAS_0_PERCENT .. BIAS_24_PERCENT)
# fet_short: FET short (FET_SHORT_DISABLED / FET_SHORT_ENABLED)
# op_mode: Operating mode (OP_MODE_3LEAD_AMP_CELL for normal operation)
SENSOR_CUSTOM = SensorConfig(
nano_amperes_per_ppm=-1000.0,
internal_zero_calibration=-0.0012,
ads_gain=ADS_GAIN_2_048V,
tia_gain=TIA_GAIN_35_KOHM,
rload=RLOAD_10_OHM,
ref_source=REF_EXTERNAL,
internal_zero=INTERNAL_ZERO_67_PERCENT,
bias_sign=BIAS_SIGN_NEGATIVE,
bias=BIAS_0_PERCENT,
fet_short=FET_SHORT_DISABLED,
op_mode=OP_MODE_3LEAD_AMP_CELL,
)

# Initialize I2C
i2c = I2C(0, scl=Pin(22), sda=Pin(21))

sensor = ElectrochemicalGasSensor(SENSOR_CUSTOM, i2c)

# -------------------------------------------------------------------------
# Setup
# -------------------------------------------------------------------------
print("Electrochemical Gas Sensor - Custom Config 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()

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

time.sleep_ms(2500)

🔗Open the complete example

electrochemical-customConfig.py at the pinned commit

→

Expected result:

ShellMicroPython
Electrochemical Gas Sensor - Custom Config Example
Sensor initialized successfully!

Then a Sensor reading: line in ppb follows every 2.5 seconds.

Functions used​

SensorConfig(nano_amperes_per_ppm, internal_zero_calibration, ads_gain, tia_gain, rload, ref_source, internal_zero, bias_sign, bias, fet_short, op_mode)returns SensorConfig

Holds the settings for one electrochemical cell. The module's presets, such as SENSOR_SO2, are SensorConfig objects too.

ReturnsThe configuration object.

Parameters

ParameterTypeDescription
nano_amperes_per_ppmfloatThe cell's sensitivity in nA per ppm, from its datasheet.
internal_zero_calibrationfloatVoltage offset added to zero the reading in clean air.
ads_gainintADC full-scale range, ADS_GAIN_6_144V to ADS_GAIN_0_256V (from ads1115).
tia_gainintFront end gain resistor, TIA_GAIN_2_75_KOHM to TIA_GAIN_350_KOHM, or TIA_GAIN_EXTERNAL.
rloadintLoad resistor, RLOAD_10_OHM to RLOAD_100_OHM.
ref_sourceintREF_INTERNAL or REF_EXTERNAL.
internal_zerointINTERNAL_ZERO_20_PERCENT, _50_PERCENT, _67_PERCENT or INTERNAL_ZERO_BYPASSED.
bias_signintBIAS_SIGN_NEGATIVE or BIAS_SIGN_POSITIVE.
biasintBias as a share of the reference, BIAS_0_PERCENT to BIAS_24_PERCENT.
fet_shortintFET_SHORT_DISABLED or FET_SHORT_ENABLED.
op_modeintOperating mode, OP_MODE_3LEAD_AMP_CELL for normal operation.
ElectrochemicalGasSensor(sensor_type, i2c=None, adc_addr=0x49, config_pin=None)returns ElectrochemicalGasSensor

Creates the sensor object from a configuration, an I2C object, an address and an optional enable pin. The example passes only the configuration and the I2C object, so the address is the default, 0x49.

ReturnsThe sensor object.

Parameters

ParameterTypeDescription
sensor_typeSensorConfigThe configuration, here SENSOR_CUSTOM.
i2cI2CAn initialised machine.I2C object.
adc_addrintThe address the module talks to. 0x30 to 0x37 selects the module's bridge mode, which talks to the board's on-board microcontroller; any other value talks to an ADS1115 directly at that address. Default 0x49.
config_pinintGPIO for the LMP91000's enable line. Not used at addresses 0x30 to 0x37. Default None.
begin()returns bool

Checks that the board answers and sends the ADC gain and the LMP91000 register values built from the configuration to it.

ReturnsTrue when the board answered and took every setting, False otherwise.

getPPB()returns float

Has the board take one ADC reading and returns the concentration in parts per billion, using the configuration's gain and sensitivity. A result below zero is returned as 0.

ReturnsGas concentration in ppb.

Putting it together​

The sensitivity scales every result, so a wrong value gives a wrong figure even when the hardware works. With TIA_GAIN_EXTERNAL, also call setCustomTiaGain() so the module knows the gain resistor's value.