Skip to main content

Calibrate the zero point

What this example shows​

The module's electrochemical-calibrateSensor.py example adds your own offset to the voltage the sensor measures, then prints the gas concentration in ppb (parts per billion) every 2.5 seconds.

CUSTOM_CALIBRATION at the top is that offset, in volts. As the example's comment says, put the board in air with none of the target gas and adjust the value until the reading is just barely 0 ppb. The example starts at 0.

The example creates the sensor with the ozone preset, SENSOR_O3. SENSOR_NO2 is the module's preset for this board's SGX-4NO2 cell; change it in the import line and in the line that creates the sensor. 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-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

→

Expected result:

ShellMicroPython
Electrochemical Gas Sensor - Calibration 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​

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 preset. SENSOR_NO2 is the preset for this board's SGX-4NO2 cell.
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; the example passes 0x49. 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.
intconfig_pinGPIO number for the LMP91000's enable pin (LMPEN), used only at 0x48 to 0x4B. None by default; the example passes 32.
sensor.begin()returns bool

Starts the sensor and writes the preset 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, pulling config_pin low while it does. Call it once before reading.

ReturnsTrue when every step succeeded, False otherwise.

sensor.setCustomZeroCalibration(calibration)returns None

Sets the calibration offset that getPPM() adds to the measured voltage before it turns it into a concentration. It changes that value in the preset object you passed when you created the sensor.

ReturnsNothing.

Parameters

TypeNameDescription
floatcalibrationThe offset in volts. The example passes CUSTOM_CALIBRATION.
sensor.getPPB()returns float

Takes a new reading and returns the gas concentration in parts per billion: getPPM() multiplied by 1000, with the calibration offset applied.

ReturnsThe gas concentration in ppb.

Putting it together​

The example creates the I2C bus first and passes it to the sensor. It calls begin() and stops in a loop if that fails, then sets the offset with setCustomZeroCalibration() before the first reading, so every value it prints already includes it. Each pass of the main loop calls getPPB(), prints the value with five decimals, and waits 2.5 seconds.