Skip to main content

Read the gas concentration in PPB

What this example shows​

The electrochemical-singleSensor.py example creates one sensor at the module's default address, 0x49, starts it, and prints the gas concentration in PPB every 2.5 seconds.

Before you run it:

  • The example is written with SENSOR_O3, the module's ozone preset, so its values are not H2S values. Do not switch it to SENSOR_H2S unchanged: that preset is written for the SGX-4H2S-100 cell (1200 nA per ppm). The datasheet of this board's SGX-4H2S-300 cell gives an output signal of 460 ± 140 nA per ppm, so with that preset the reading comes out about 2.6 times too low. Pass a custom SensorConfig with the SENSOR_H2S settings and 460 nA per ppm instead, as Quick Start shows.
  • Change Pin(22) and Pin(21) to your controller's SCL and SDA pins if they differ.

The example code​

# FILE: electrochemical-singleSensor.py

from machine import I2C, Pin
import time
from ElectrochemicalGasSensor import ElectrochemicalGasSensor, SENSOR_O3

# Initialize I2C
i2c = I2C(0, scl=Pin(22), sda=Pin(21))

# Create sensor object - change SENSOR_O3 to match your sensor type
# Available types: SENSOR_CO, SENSOR_NO2, SENSOR_SO2, SENSOR_O3,
# SENSOR_NO, SENSOR_H2S, SENSOR_NH3, SENSOR_CL2
sensor = ElectrochemicalGasSensor(SENSOR_O3, i2c)

# If using a custom ADC address or a GPIO pin for LMP91000 MENB:
# sensor = ElectrochemicalGasSensor(SENSOR_O3, i2c, adc_addr=0x4B, config_pin=5)

# -------------------------------------------------------------------------
# Setup
# -------------------------------------------------------------------------
print("Electrochemical Gas Sensor - Single Sensor 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()

# If PPM is more relevant for your sensor, use:
# reading = sensor.getPPM()

print("Sensor reading: {:.5f} PPB".format(reading))

time.sleep_ms(2500)

Open the complete example

Expected result:

ShellMicroPython
Electrochemical Gas Sensor - Single Sensor Example
Sensor initialized successfully!

Then a line starting with Sensor reading: follows every 2.5 seconds, with the concentration in PPB to five decimals. If the sensor does not answer, the script prints ERROR: Can't init the sensor! Check connections! and waits.

Functions used​

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

Creates the sensor object. The sensor type holds the front-end and conversion settings for one gas cell. The address decides how begin() talks to the board: an address from 0x30 to 0x37 goes through the board's ATtiny404; any other address, such as the default 0x49, goes to an ADS1115 at that address and to the LMP91000 directly.

ReturnsThe sensor object.

Parameters

NameTypeDescription
sensor_typeSensorConfigPreset from the module (SENSOR_CO, SENSOR_NO2, SENSOR_SO2, SENSOR_O3, SENSOR_NO, SENSOR_H2S, SENSOR_NH3, SENSOR_CL2) or a custom SensorConfig. For this board's cell, pass a custom one with 460 nA per ppm.
i2cmachine.I2CThe I2C bus the board is on. Without it, the module opens one itself only on the boards it recognises: ESP32 builds of MicroPython and one Soldered board's own build (I2C bus 0, SCL 22, SDA 21), and ESP8266 builds (SCL 5, SDA 4). On any other board it raises Board not recognized, please pass an I2C object manually.
adc_addrintThe address begin() talks to. This example leaves it out, so the default, 0x49, is used.
config_pinintA GPIO the module holds high and pulls low while it configures the LMP91000 directly. Unused at addresses from 0x30 to 0x37. This example leaves it out.
begin()returns bool

With an address from 0x30 to 0x37, checks that the board's ATtiny404 answers, sends it the ADC gain from the sensor type and the slowest data rate, then has it configure the LMP91000 front end; each command is retried for up to 500 ms. With any other address, checks that an ADS1115 answers at that address, sets its gain and the slowest data rate, then configures the LMP91000 directly. Call it once before reading.

ReturnsTrue when every step succeeded, False otherwise.

getPPB()returns float

Takes a reading with getPPM() and multiplies it by 1000.

ReturnsGas concentration in PPB.

getPPM()returns float

The commented-out alternative in the example. Takes an ADC reading, subtracts the front end's internal zero (2.5 V times its percentage), adds the zero calibration, then divides by the amplifier gain and by the cell sensitivity in nA per ppm from the sensor type. A result below 0 is returned as 0.0.

ReturnsGas concentration in PPM.

This example also uses I2C and Pin from MicroPython's machine module, which is not part of the Soldered ElectrochemicalGasSensor module.

Putting it together​

Open the I2C bus, create one ElectrochemicalGasSensor with a sensor type, call begin() once, then call getPPB() or getPPM() whenever you want a reading.