Skip to main content

Use your own front-end settings

What this example shows​

The electrochemical-customConfig.py example builds its own preset with SensorConfig instead of using one of the module's presets. A preset holds the cell's sensitivity, the ADC range and every LMP91000 front-end setting, so this is the way to try other values for a cell. The example then prints a reading in ppb (parts per billion) every 2.5 seconds.

The example creates the sensor with the 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 a GPIO. See the library overview for how the address decides this.

The example's SENSOR_CUSTOM holds the same values as the module's ozone preset, SENSOR_O3. For this board's SGX-4NO-250 cell, the module's SENSOR_NO preset uses the values in the table below.

FieldSENSOR_NO value
nano_amperes_per_ppm400.0
internal_zero_calibration0
ads_gainADS_GAIN_4_096V
tia_gainTIA_GAIN_120_KOHM
rloadRLOAD_10_OHM
ref_sourceREF_EXTERNAL
internal_zeroINTERNAL_ZERO_20_PERCENT
bias_signBIAS_SIGN_POSITIVE
biasBIAS_12_PERCENT
fet_shortFET_SHORT_DISABLED
op_modeOP_MODE_3LEAD_AMP_CELL

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-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

Examples/electrochemical-customConfig.py at commit 623e1e6

→

Expected result, with SENSOR_CUSTOM set to the SENSOR_NO values (readings in clean air; yours will differ):

ShellMicroPython
Electrochemical Gas Sensor - Custom Config Example
Sensor initialized successfully!
Sensor reading: 5.83691 PPB
Sensor reading: 6.04365 PPB
Sensor reading: 6.00634 PPB
Sensor reading: 5.94344 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​

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 cell. The module's presets, such as SENSOR_NO, are SensorConfig objects too. begin() sends the front-end fields to the LMP91000 and the ADC gain to the ADS1115; getPPM() uses the sensitivity and the zero calibration.

ReturnsThe preset you pass to ElectrochemicalGasSensor.

Parameters

TypeNameDescription
floatnano_amperes_per_ppmThe cell's sensitivity in nA per ppm, -1000.0 in the example.
floatinternal_zero_calibrationA voltage added to every reading, -0.0012 in the example.
intads_gainThe ADC's full-scale range, an ADS_GAIN_* constant from ads1115, ADS_GAIN_2_048V in the example.
inttia_gainThe front end's amplifier gain, TIA_GAIN_2_75_KOHM to TIA_GAIN_350_KOHM (or TIA_GAIN_EXTERNAL), TIA_GAIN_35_KOHM in the example.
intrloadThe load resistor, RLOAD_10_OHM to RLOAD_100_OHM, RLOAD_10_OHM in the example.
intref_sourceREF_INTERNAL or REF_EXTERNAL, REF_EXTERNAL in the example.
intinternal_zeroINTERNAL_ZERO_20_PERCENT, _50_PERCENT, _67_PERCENT or INTERNAL_ZERO_BYPASSED, INTERNAL_ZERO_67_PERCENT in the example.
intbias_signBIAS_SIGN_NEGATIVE or BIAS_SIGN_POSITIVE, BIAS_SIGN_NEGATIVE in the example.
intbiasBIAS_0_PERCENT to BIAS_24_PERCENT, BIAS_0_PERCENT in the example.
intfet_shortFET_SHORT_DISABLED or FET_SHORT_ENABLED, FET_SHORT_DISABLED in the example.
intop_modeThe front end's mode, OP_MODE_3LEAD_AMP_CELL in the example.
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 preset, SENSOR_CUSTOM in the example.
I2Ci2cThe I2C bus the board is on.
intadc_addrThe I2C address. Left out in the example, so it is the module's default, 0x49. 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. 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. getPPM() 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 ppb. A negative result is returned as 0.0.

Putting it together​

The preset is plain data: build it once, pass it to the constructor, and begin() sends its front-end settings when it starts the sensor. To try other values, edit the fields of SENSOR_CUSTOM and run the example again.