Skip to main content

Custom sensor configuration

What this example shows​

The electrochemical-customConfig.py example builds its own SensorConfig, named SENSOR_CUSTOM, instead of using one of the module's presets, and prints the gas concentration in parts per billion (ppb) every 2.5 seconds.

The example's comment says to edit the values to match your sensor's datasheet. As written they are the values of the module's ozone preset, SENSOR_O3, so the readings are converted for the ozone cell, not for this board's SGX-4CO carbon monoxide cell.

Before you run it, set the SensorConfig(...) arguments to the values of SENSOR_CO, the module's preset for this board's cell, from the table below. Three of them are not imported by the example: add ADS_GAIN_4_096V to the from ads1115 import line, and TIA_GAIN_14_KOHM and INTERNAL_ZERO_20_PERCENT to the from ElectrochemicalGasSensor import list.

ArgumentIn the exampleSENSOR_CO value
nano_amperes_per_ppm-1000.070.0
internal_zero_calibration-0.00120
ads_gainADS_GAIN_2_048VADS_GAIN_4_096V
tia_gainTIA_GAIN_35_KOHMTIA_GAIN_14_KOHM
rloadRLOAD_10_OHMRLOAD_10_OHM
ref_sourceREF_EXTERNALREF_EXTERNAL
internal_zeroINTERNAL_ZERO_67_PERCENTINTERNAL_ZERO_20_PERCENT
bias_signBIAS_SIGN_NEGATIVEBIAS_SIGN_NEGATIVE
biasBIAS_0_PERCENTBIAS_0_PERCENT
fet_shortFET_SHORT_DISABLEDFET_SHORT_DISABLED
op_modeOP_MODE_3LEAD_AMP_CELLOP_MODE_3LEAD_AMP_CELL

The example opens I2C bus 0 with SCL on pin 22 and SDA on pin 21. Change them to your controller's I2C pins if they differ.

What it needs on this board​

The example creates the sensor without adc_addr, so the module uses its default, 0x49. At an address outside 0x30 to 0x37 the module does not go through the board's bridge: begin() looks for the ADS1115 at that address with an I2C scan, then writes the LMP91000's configuration at 0x48, both on the bus you pass it. On this board those two chips sit on the internal bus, which the top header brings out as SDA2 and SCL2. The Qwiic connectors and SDA1/SCL1 reach only the ATtiny404 bridge, and an I2C scan over Qwiic finds only the bridge, at 0x30 as shipped. The example passes no config_pin, and LMPEN is pulled to GND on the board; see Hardware Details.

So wire the top header for this example, not the Qwiic cable: connect your controller's SDA to SDA2, its SCL to SCL2, its 3.3 V pin to 3V3 and its GND to GND. The listing's connecting diagram shows a Qwiic line, but over Qwiic only the bridge answers.

The bridge's address, 0x30 to 0x37, is the one the Read the CO concentration example passes.

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)

The lines below are from a run of this example on this board, with the controller wired to the header's SDA2, SCL2, 3V3 and GND pins as described above.

Expected result in the Thonny shell:

ShellMicroPython
Electrochemical Gas Sensor - Custom Config Example
Sensor initialized successfully!
Sensor reading: 309.60422 PPB
Sensor reading: 316.97594 PPB
Sensor reading: 300.25541 PPB
Sensor reading: 324.21363 PPB

Then a line every 2.5 seconds: Sensor reading: , the concentration in ppb with five decimals, and PPB. A result below zero is printed as 0.00000. If begin() fails, the shell shows ERROR: Can't init the sensor! Check connections! and the script stops there.

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 (SENSOR_CO, SENSOR_O3 and the others) are SensorConfig objects; this example makes its own.

ReturnsThe configuration, ready to pass to ElectrochemicalGasSensor().

Parameters

ParameterTypeDescription
nano_amperes_per_ppmfloatThe cell's sensitivity in nA per ppm, from its datasheet.
internal_zero_calibrationfloatA voltage offset, in volts, added to zero the reading in clean air.
ads_gainintThe ADC's full-scale range, an ADS_GAIN_* constant from ads1115.
tia_gainintThe front end's feedback resistor, a TIA_GAIN_* constant.
rloadintThe load resistor, an RLOAD_* constant.
ref_sourceintThe reference source, REF_INTERNAL or REF_EXTERNAL.
internal_zerointThe internal zero as a percentage of the reference, an INTERNAL_ZERO_* constant.
bias_signintThe bias polarity, BIAS_SIGN_NEGATIVE or BIAS_SIGN_POSITIVE.
biasintThe bias as a percentage of the reference, a BIAS_*_PERCENT constant.
fet_shortintFET_SHORT_DISABLED or FET_SHORT_ENABLED.
op_modeintThe operating mode, an OP_MODE_* constant; 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. The example passes SENSOR_CUSTOM and the I2C bus and leaves adc_addr and config_pin at their defaults.

ReturnsThe sensor object.

Parameters

ParameterTypeDescription
sensor_typeSensorConfigThe cell's configuration: a module preset or your own SensorConfig.
i2cmachine.I2CAn initialised I2C bus. With None, the module opens I2C 0 on pins 22 and 21 on an ESP32, or pins 5 and 4 on an ESP8266, and raises Board not recognized, please pass an I2C object manually on any other board.
adc_addrintThe I2C address. Default 0x49. From 0x30 to 0x37 the module talks to the board's ATtiny404 bridge.
config_pinint or NoneA GPIO the module drives low while it sends the front end's configuration and high otherwise. Not used at a 0x30 to 0x37 address. Default None.
begin()returns bool

Starts the sensor. At an adc_addr from 0x30 to 0x37 it checks that the bridge answers, sends it the ADC gain and data rate from the configuration, then the configuration's front-end settings, retrying a bus error until a 500 ms timeout for each step. At any other adc_addr it checks that the ADC answers at that address, sets its gain and data rate, then writes the front-end settings.

ReturnsTrue if every step succeeded, False otherwise.

getPPB()returns float

Makes one measurement with the configuration's sensitivity and zero, and returns it in parts per billion.

ReturnsgetPPM() multiplied by 1000.

Putting it together​

Build a SensorConfig with your cell's values (for this board, the SENSOR_CO values above), pass it to ElectrochemicalGasSensor(), call begin() once and check it, then read with getPPB().