Skip to main content

Use your own sensor configuration

What this example shows​

The module's electrochemical-customConfig.py example builds its own SensorConfig instead of using a preset, creates the sensor with it, and prints the gas concentration in ppb (parts per billion) every 2.5 seconds.

The comment above SENSOR_CUSTOM lists what each value sets; edit them to match your sensor's datasheet. For this board's SGX-4NO2 cell, the module's own SENSOR_NO2 preset uses nano_amperes_per_ppm=-600.0 and internal_zero_calibration=0, and the same values as this example for everything else. The example opens I2C bus 0 with SCL on pin 22 and SDA on pin 21; change them to match your controller.

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

Examples/electrochemical-customConfig.py at commit 623e1e6

→

Expected result:

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

Then every 2.5 seconds the example prints a line that starts with Sensor reading:, followed by the concentration in ppb with five decimals. If begin() fails, it prints ERROR: Can't init the sensor! Check connections! and waits 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 electrochemical cell: its sensitivity, the zero offset, the ADS1115 range and the LMP91000 front-end settings. The module's presets, such as SENSOR_NO2, are SensorConfig objects too.

ReturnsThe configuration object to pass to ElectrochemicalGasSensor.

Parameters

TypeNameDescription
floatnano_amperes_per_ppmThe cell's sensitivity in nA per ppm, from its datasheet.
floatinternal_zero_calibrationOffset in volts that getPPM() adds to the measured voltage.
intads_gainThe ADS1115 full-scale range, one of ADS_GAIN_6_144V, ADS_GAIN_4_096V, ADS_GAIN_2_048V, ADS_GAIN_1_024V, ADS_GAIN_0_512V, ADS_GAIN_0_256V from ads1115.py.
inttia_gainThe LMP91000 transimpedance (TIA) feedback resistor, TIA_GAIN_2_75_KOHM to TIA_GAIN_350_KOHM, or TIA_GAIN_EXTERNAL.
intrloadThe load resistor, RLOAD_10_OHM, RLOAD_33_OHM, RLOAD_50_OHM or RLOAD_100_OHM.
intref_sourceThe reference voltage source, REF_INTERNAL or REF_EXTERNAL.
intinternal_zeroThe internal zero, INTERNAL_ZERO_20_PERCENT, INTERNAL_ZERO_50_PERCENT, INTERNAL_ZERO_67_PERCENT or INTERNAL_ZERO_BYPASSED.
intbias_signThe bias polarity, BIAS_SIGN_NEGATIVE or BIAS_SIGN_POSITIVE.
intbiasThe bias as a percentage of the reference, BIAS_0_PERCENT to BIAS_24_PERCENT.
intfet_shortThe FET short, FET_SHORT_DISABLED or FET_SHORT_ENABLED.
intop_modeThe LMP91000 operating mode. OP_MODE_3LEAD_AMP_CELL is normal operation.
ElectrochemicalGasSensor(sensor_type, i2c=None, adc_addr=0x49, config_pin=None)returns ElectrochemicalGasSensor

Creates the sensor object. With a config_pin it also sets that pin as an output and drives it high. Nothing is sent over I2C until you call begin().

ReturnsThe sensor object.

Parameters

TypeNameDescription
SensorConfigsensor_typeThe cell's settings: a preset or your own SensorConfig. The example passes SENSOR_CUSTOM.
I2Ci2cThe I2C bus. With None, the module picks the pins by the port MicroPython reports: I2C bus 0 on pins 22 (SCL) and 21 (SDA) on the ESP32 port, scl=Pin(5), sda=Pin(4) on the ESP8266 port. On a port it does not recognise, it raises Board not recognized, please pass an I2C object manually.
intadc_addrThe address begin() talks to, 0x49 by default. From 0x30 to 0x37 the module sends its commands to the ATtiny404; from 0x48 to 0x4B it addresses the ADS1115 at adc_addr and the LMP91000 directly. The example leaves it at the default.
intconfig_pinGPIO number for the LMP91000's enable pin (LMPEN), used only at 0x48 to 0x4B. None by default; the example does not pass one.
sensor.begin()returns bool

Starts the sensor and writes the configuration to it. At 0x30 to 0x37 it checks that the ATtiny404 answers, then sends the ADC gain and data rate and the LMP91000 configuration, each step retrying until the board answers or 500 ms pass. At 0x48 to 0x4B it checks that the ADS1115 shows up in an I2C scan at adc_addr, sets its gain and its slowest data rate, and writes the configuration to the LMP91000 at 0x48. Call it once before reading.

ReturnsTrue when every step succeeded, False otherwise.

sensor.getPPB()returns float

Takes a new reading and returns the gas concentration in parts per billion: the reading in ppm, worked out with your configuration's sensitivity, TIA gain and internal zero, multiplied by 1000. A negative result is returned as 0.0.

ReturnsThe gas concentration in ppb.

Putting it together​

The example builds SENSOR_CUSTOM first, then creates the I2C bus and the sensor with it. begin() writes the configuration's ADC range and front-end settings; if it returns False, the example prints the error and stays in a loop. Each pass of the main loop calls getPPB(), prints the value with five decimals, and waits 2.5 seconds.