Calibrate the zero point
What this example shows
The electrochemical-calibrateSensor.py example adds a zero-point offset to
the sensor's readings and 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
CUSTOM_CALIBRATION is a voltage the library adds to each ADC reading. As the
example's comment says, in air with none of the target gas, adjust it until
the reading is just barely 0 PPB. The example uses the ozone preset and opens
I2C bus 0 with SCL on pin 22 and SDA on pin 21, and creates the sensor
with adc_addr=0x49 and config_pin=32.
As the connecting diagram in the example's comments shows, wire the board's
LMPEN pin on the header to GPIO 32, the example's config_pin. On this
board, LMPEN also goes to the board's ATtiny404.
# FILE: electrochemical-calibrateSensor.py
# AUTHOR: Fran Fodor @ Soldered
# BRIEF: Sensor zero-point calibration example.
# LAST UPDATED: 2026-05-21
# Connecting diagram:
#
# Qwiic------------------------>Qwiic
# LMPEN------------------------>32 (GPIO pin, required for configuration)
from machine import I2C, Pin
import time
from ElectrochemicalGasSensor import ElectrochemicalGasSensor, SENSOR_O3
# This value is added to the voltage read from the ADC.
# In an environment with 0 of the target gas, the reading after calibration
# should be just barely 0 PPB — adjust this value until that is the case.
CUSTOM_CALIBRATION = 0
# Initialize I2C
i2c = I2C(0, scl=Pin(22), sda=Pin(21))
# Custom ADC address (0x49 default) and GPIO pin for LMPEN
sensor = ElectrochemicalGasSensor(SENSOR_O3, i2c, adc_addr=0x49, config_pin=32)
# -------------------------------------------------------------------------
# Setup
# -------------------------------------------------------------------------
print("Electrochemical Gas Sensor - Calibration Example")
if not sensor.begin():
print("ERROR: Can't init the sensor! Check connections!")
while True:
time.sleep_ms(100)
# Apply the zero-point calibration offset
sensor.setCustomZeroCalibration(CUSTOM_CALIBRATION)
print("Sensor initialized successfully!")
# -------------------------------------------------------------------------
# Main loop
# -------------------------------------------------------------------------
while True:
reading = sensor.getPPB()
# O3 is typically measured in PPB
# Switch to getPPM() if PPM is more relevant for your use case
print("Sensor reading: {:.5f} PPB".format(reading))
time.sleep_ms(2500)
🔗Open the complete example
Examples/electrochemical-calibrateSensor.py at commit 623e1e6
The shell shows:
Electrochemical Gas Sensor - Calibration 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. The functions below are the
library's.
ElectrochemicalGasSensor(sensor_type, i2c=None, adc_addr=0x49, config_pin=None)returns ElectrochemicalGasSensorCreates 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
| Name | Type | Description |
|---|---|---|
sensor_type | SensorConfig | The preset for your cell: SENSOR_CO, SENSOR_NO2, SENSOR_SO2, SENSOR_O3, SENSOR_NO, SENSOR_H2S, SENSOR_NH3, SENSOR_CL2, or your own SensorConfig. |
i2c | machine.I2C | The 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_addr | int | The 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_pin | int | A 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 boolSets up the connection for adc_addr and configures the LMP91000 with the preset. 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.
setCustomZeroCalibration(calibration)returns NoneSets the zero-point offset, in volts, that getPPM() and getPPB() add to each reading after removing the internal zero. It stores the value in the preset object you passed to the constructor, so every sensor created with that same preset uses it too.
ReturnsNothing.
Parameters
| Name | Type | Description |
|---|---|---|
calibration | float | The offset in volts. |
getPPB()returns floatTakes one reading of the ADS1115's channel 0 (through the bridge for 0x30 to 0x37) and converts it to a concentration with the preset: it removes the internal zero offset, adds the zero calibration, divides by the amplifier gain and by the cell's sensitivity in nA per PPM, and multiplies by 1000. A negative result is returned as 0.
ReturnsThe gas concentration in PPB, getPPM() x 1000.
getPPM()returns floatThe same reading in parts per million. The example's comment names it as the alternative.
ReturnsThe gas concentration in PPM.
Putting it together
Create the sensor and call begin(). Then call setCustomZeroCalibration()
with your offset before you read. In air with none of the target gas, adjust
the offset until the reading is just barely 0. getPPM() returns 0 for any
negative result, so a reading of exactly 0 does not show how far below zero
the uncorrected value is.