Skip to main content

Configure your own cell

What this example shows​

The electrochemical-customConfig.py example builds its own SensorConfig instead of using a preset, so you can set every value from your cell's datasheet. It then prints the gas concentration in parts per billion (PPB) every 2.5 seconds.

Wire your controller to the top header for this example, not to a Qwiic connector: the controller's SDA to SDA2, its SCL to SCL2, its 3.3 V supply to 3V3 and its ground to GND. At this address the library talks to the ADS1115 and the LMP91000 directly on SDA2 and SCL2; the board's ATtiny404 bridge at 0x30 is not used. Over Qwiic, only the bridge example in the Quick Start works.

The example code​

The values in SENSOR_CUSTOM are the library's ozone preset, SENSOR_O3. Replace them with your cell's sensitivity and the LMP91000 settings it needs; the comment above SENSOR_CUSTOM lists the range of each one. The example opens I2C bus 0 with SCL on pin 22 and SDA on pin 21.

The connecting diagram in the example's comments wires LMPEN to a GPIO. On this board, LMPEN goes to the header and to the board's ATtiny404.

# 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

→

The shell shows:

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

Then a line every 2.5 seconds: Sensor reading: , the concentration with five decimals, and PPB. If begin() fails, the script prints ERROR: Can't init the sensor! Check connections! and waits.

Functions used​

I2C and Pin come from MicroPython's own machine module, and time.sleep_ms() from its time module. ADS_GAIN_2_048V comes from the library's ads1115.py helper, and the other constants from ElectrochemicalGasSensor.py. The functions below are the library's.

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 values the library uses to turn a voltage into a concentration, and the register values it sends to the ADS1115 and LMP91000. The presets such as SENSOR_O3 are SensorConfig objects too.

ReturnsThe configuration object, passed to ElectrochemicalGasSensor() as sensor_type.

Parameters

NameTypeDescription
nano_amperes_per_ppmfloatThe cell's sensitivity in nA per PPM, from its datasheet. The library's NO2 and O3 presets use negative values.
internal_zero_calibrationfloatA voltage offset added to each reading, the same value setCustomZeroCalibration() sets.
ads_gainintThe ADS1115 full-scale range: ADS_GAIN_6_144V, ADS_GAIN_4_096V, ADS_GAIN_2_048V, ADS_GAIN_1_024V, ADS_GAIN_0_512V or ADS_GAIN_0_256V, from ads1115.
tia_gainintThe LMP91000's amplifier feedback resistor: TIA_GAIN_2_75_KOHM, TIA_GAIN_3_5_KOHM, TIA_GAIN_7_KOHM, TIA_GAIN_14_KOHM, TIA_GAIN_35_KOHM, TIA_GAIN_120_KOHM, TIA_GAIN_350_KOHM, or TIA_GAIN_EXTERNAL.
rloadintThe load resistor: RLOAD_10_OHM, RLOAD_33_OHM, RLOAD_50_OHM or RLOAD_100_OHM.
ref_sourceintThe reference: REF_INTERNAL or REF_EXTERNAL.
internal_zerointThe internal zero as a share of the reference: INTERNAL_ZERO_20_PERCENT, INTERNAL_ZERO_50_PERCENT, INTERNAL_ZERO_67_PERCENT or INTERNAL_ZERO_BYPASSED.
bias_signintThe bias polarity: BIAS_SIGN_NEGATIVE or BIAS_SIGN_POSITIVE.
biasintThe bias as a share of the reference, from BIAS_0_PERCENT to BIAS_24_PERCENT.
fet_shortintFET_SHORT_DISABLED or FET_SHORT_ENABLED.
op_modeintThe LMP91000's mode: OP_MODE_3LEAD_AMP_CELL for normal operation, or OP_MODE_DEEP_SLEEP, OP_MODE_2LEAD_GROUND_CELL, OP_MODE_STANDBY, OP_MODE_TEMPERATURE_TIA_OFF, OP_MODE_TEMPERATURE_TIA_ON.
ElectrochemicalGasSensor(sensor_type, i2c=None, adc_addr=0x49, config_pin=None)returns ElectrochemicalGasSensor

Creates the sensor object for one board. The library reads adc_addr to decide how to talk to it: for an address from 0x30 to 0x37 it uses the protocol of the ATtiny404 bridge; for any other address it talks to an ADS1115 at that address and to the LMP91000 at 0x48 itself.

ReturnsThe sensor object.

Parameters

NameTypeDescription
sensor_typeSensorConfigHere, the example's own SENSOR_CUSTOM.
i2cmachine.I2CThe I2C bus. When left out, the library opens I2C bus 0 with SCL on pin 22 and SDA on pin 21 on an ESP32 port, or I2C with SCL on pin 5 and SDA on pin 4 on an ESP8266 port, and raises an exception on any other board.
adc_addrintThe address begin() uses. 0x30 to 0x37 selects the bridge protocol; this board answers at 0x30 out of the box, or at the address its switch sets. Any other value is taken as an ADS1115 address. The default is 0x49.
config_pinintA GPIO number for the LMP91000 enable line. The library holds it high and pulls it low only while it writes the LMP91000's registers itself; for an address from 0x30 to 0x37 it is not used. None by default.
begin()returns bool

Sets up the connection for adc_addr and configures the LMP91000 with the configuration's gain, load, reference, bias and mode. For 0x30 to 0x37 it checks that the bridge answers, sends the ADC gain and has the bridge write the LMP91000's registers, each step giving up after 500 ms. For any other address it checks with an I2C scan that a device answers at adc_addr, then writes the LMP91000's registers at 0x48 when its status reads ready.

ReturnsTrue when every step succeeded, False otherwise.

getPPB()returns float

Takes one reading of the ADS1115's channel 0 (through the bridge for 0x30 to 0x37) and converts it to a concentration with the configuration: it removes the internal zero offset, adds the zero calibration, divides by the amplifier gain and by the sensitivity in nA per PPM, and multiplies by 1000. A negative result is returned as 0.

ReturnsThe gas concentration in PPB.

Putting it together​

Build a SensorConfig from your cell's datasheet and pass it to ElectrochemicalGasSensor() where you would pass a preset. begin() sends its settings to the LMP91000, and getPPB() uses its sensitivity and zero values for every reading.