Skip to main content

Zero calibration

What this example shows​

The electrochemical-calibrateSensor.py example applies a zero-point calibration offset with setCustomZeroCalibration() and then prints the gas concentration in parts per billion (ppb) every 2.5 seconds.

The offset is CUSTOM_CALIBRATION at the top of the script, 0 in the example. The example's comment explains how to use it: the value is added to the voltage read from the ADC, and in air with none of the target gas you adjust it until the reading is just barely 0 ppb.

Before you run it, change SENSOR_O3 to SENSOR_CO in the import line and in the line that creates the sensor. SENSOR_O3 is the module's preset for the ozone cell. This board carries the SGX-4CO carbon monoxide cell, and the module's preset for it is SENSOR_CO. The offset is added before the module converts with the preset's sensitivity and zero, so tune it with SENSOR_CO in place: an offset found with the ozone preset does not zero this 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 with adc_addr=0x49 and config_pin=32. 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.

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 module drives pin 32 low while it writes the front end's configuration and high otherwise, and the example's connecting comment wires that pin to LMPEN. On the board, LMPEN is on the bottom header, pulled to GND through 10 kΩ and also wired to the ATtiny404; see Hardware Details.

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

The example code​

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

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 - Calibration Example
Sensor initialized successfully!
Sensor reading: 291.45891 PPB
Sensor reading: 314.34645 PPB
Sensor reading: 300.00720 PPB
Sensor reading: 318.97074 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​

ElectrochemicalGasSensor(sensor_type, i2c=None, adc_addr=0x49, config_pin=None)returns ElectrochemicalGasSensor

Creates the sensor object. The example passes the preset, the I2C bus, adc_addr=0x49 and config_pin=32.

ReturnsThe sensor object.

Parameters

ParameterTypeDescription
sensor_typeSensorConfigThe cell's preset. For this board's SGX-4CO cell the module's preset is SENSOR_CO.
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 preset, then the front end's configuration, 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's configuration with config_pin held low.

ReturnsTrue if every step succeeded, False otherwise.

setCustomZeroCalibration(calibration)returns None

Sets the zero-calibration offset. getPPM() and getPPB() add it, in volts, to the measured voltage after taking off the internal zero and before converting to a concentration. It is stored in the preset object passed to the constructor.

ReturnsNothing.

Parameters

ParameterTypeDescription
calibrationfloatThe offset in volts. The example passes CUSTOM_CALIBRATION, which is 0.
getPPB()returns float

Makes one measurement and returns it in parts per billion. The example's comment says to switch to getPPM() if ppm suits you better.

ReturnsgetPPM() multiplied by 1000.

Putting it together​

Create the object with SENSOR_CO, the preset for this board's cell, call begin() once and check it, then call setCustomZeroCalibration() with your offset before you start reading. In air with none of the target gas, change CUSTOM_CALIBRATION and run the script again until the reading is just barely 0 ppb, as the example's comment says.