Read two sensors
What this example shows
The electrochemical-twoSensors.py example creates two sensor objects on one
I2C bus, each with its own ADC address and configuration pin, and prints both
readings in PPB every 2.5 seconds. It creates sensor 1 with adc_addr=0x4A and
config_pin=25, and sensor 2 with adc_addr=0x49 and config_pin=32.
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 toSENSOR_H2Sunchanged: 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 readings come out about 2.6 times too low. Pass a customSensorConfigwith theSENSOR_H2Ssettings and 460 nA per ppm to both sensors instead, as Quick Start shows. - Change
Pin(22)andPin(21)to your controller's SCL and SDA pins if they differ.
The example code
# FILE: electrochemical-twoSensors.py
from machine import I2C, Pin
import time
from ElectrochemicalGasSensor import ElectrochemicalGasSensor, SENSOR_O3
# Initialize shared I2C bus
i2c = I2C(0, scl=Pin(22), sda=Pin(21))
# Sensor 1: ADC address 0x4A, LMPEN on pin 25
sensor1 = ElectrochemicalGasSensor(SENSOR_O3, i2c, adc_addr=0x4A, config_pin=25)
# Sensor 2: ADC address 0x49, LMPEN on pin 32
sensor2 = ElectrochemicalGasSensor(SENSOR_O3, i2c, adc_addr=0x49, config_pin=32)
# -------------------------------------------------------------------------
# Setup
# -------------------------------------------------------------------------
print("Electrochemical Gas Sensor - Two Sensors Example")
if not sensor1.begin():
print("ERROR: Can't init sensor 1! Check connections!")
while True:
time.sleep_ms(100)
print("Sensor 1 initialized successfully!")
if not sensor2.begin():
print("ERROR: Can't init sensor 2! Check connections!")
while True:
time.sleep_ms(100)
print("Sensor 2 initialized successfully!")
# -------------------------------------------------------------------------
# Main loop
# -------------------------------------------------------------------------
while True:
reading1 = sensor1.getPPB()
reading2 = sensor2.getPPB()
print("Sensor 1 reading: {:.5f} PPB".format(reading1))
print("Sensor 2 reading: {:.5f} PPB".format(reading2))
time.sleep_ms(2500)
Expected result:
Electrochemical Gas Sensor - Two Sensors Example
Sensor 1 initialized successfully!
Sensor 2 initialized successfully!
Then a Sensor 1 reading: line and a Sensor 2 reading: line follow every 2.5
seconds, each with the concentration in PPB to five decimals. If a sensor does
not answer, the script prints ERROR: Can't init sensor 1! Check connections!
(or sensor 2) and waits.
Functions used
ElectrochemicalGasSensor(sensor_type, i2c=None, adc_addr=0x49, config_pin=None)returns ElectrochemicalGasSensorCreates one sensor object per gas cell, each with its own address and configuration pin. 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. A config_pin, when given, is set as an output and driven high.
ReturnsThe sensor object.
Parameters
| Name | Type | Description |
|---|---|---|
sensor_type | SensorConfig | Preset 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. |
i2c | machine.I2C | The I2C bus to talk on. Both objects share the same bus. |
adc_addr | int | The address begin() talks to. This example passes 0x4A for sensor 1 and 0x49 for sensor 2. |
config_pin | int | A GPIO the module holds high and pulls low while it configures that sensor's LMP91000 directly. Unused at addresses from 0x30 to 0x37. This example passes 25 for sensor 1 and 32 for sensor 2. |
begin()returns boolWith an address from 0x30 to 0x37, checks that the board's ATtiny404 at that address 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, pulling config_pin low during the writes. Call it once for each sensor object.
ReturnsTrue when every step succeeded, False otherwise.
getPPB()returns floatTakes a reading for that sensor object with getPPM() and multiplies it by 1000.
ReturnsGas concentration in PPB.
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 one I2C bus, create one ElectrochemicalGasSensor object per gas
cell, each with its own address and configuration pin, call begin() on each, then read each one with getPPB().