Read the gas concentration
What this example shows
The electrochemical-bridgeBoard.py example starts the breakout at its I2C
address 0x30 and prints the gas concentration in PPM every 2.5 seconds.
Because the address is in the range 0x30 to 0x37, the module talks to the
board's ATtiny404, which configures the LMP91000 front end and reads the ADS1115
for you.
Before you run it:
- The example is written with
SENSOR_O3, the module's ozone preset, so its PPM values are not H2S values. Do not switch it to the module'sSENSOR_H2Spreset unchanged: that preset is written for the SGX-4H2S-100 cell, with a sensitivity of 1200 nA per ppm, while this board carries the SGX-4H2S-300 cell, whose datasheet gives an output signal of 460 ± 140 nA per ppm.getPPM()divides by the preset's sensitivity, so withSENSOR_H2Sthe reading comes out about 2.6 times too low. Pass a customSensorConfigwith theSENSOR_H2Ssettings and 460 nA per ppm instead, as Quick Start shows. - Change
Pin(22)andPin(21)to your controller's SCL and SDA pins if they differ. - If you moved the board's address with the ADDRESS switch, change
0x30to match.
The example code
# FILE: electrochemical-bridgeBoard.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))
sensor = ElectrochemicalGasSensor(SENSOR_O3, i2c, adc_addr=0x30)
# -------------------------------------------------------------------------
# Setup
# -------------------------------------------------------------------------
print("Electrochemical Gas Sensor - ATtiny Bridge Board Example")
if not sensor.begin():
print("ERROR: Can't init the sensor! Check connections and jumper address!")
while True:
time.sleep_ms(100)
print("Sensor initialized successfully!")
# -------------------------------------------------------------------------
# Main loop
# -------------------------------------------------------------------------
while True:
reading = sensor.getPPM()
# If PPB is more relevant for your sensor, use:
# reading = sensor.getPPB()
print("Sensor reading: {:.5f} PPM".format(reading))
time.sleep_ms(2500)
Expected result:
Electrochemical Gas Sensor - ATtiny Bridge Board Example
Sensor initialized successfully!
Then a line starting with Sensor reading: follows every 2.5 seconds, with the
concentration in PPM to five decimals. If the board does not answer, the script
prints ERROR: Can't init the sensor! Check connections and jumper address! and
waits.
Functions used
ElectrochemicalGasSensor(sensor_type, i2c=None, adc_addr=0x49, config_pin=None)returns ElectrochemicalGasSensorCreates 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: 0x30 to 0x37 goes through this board's ATtiny404.
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 or SENSOR_CL2. Or a custom SensorConfig. SENSOR_H2S is written for the SGX-4H2S-100 cell (1200 nA per ppm) and reads about 2.6 times too low with this board's SGX-4H2S-300 cell (460 ± 140 nA per ppm in its datasheet): pass a custom SensorConfig with 460 nA per ppm. |
i2c | machine.I2C | The 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_addr | int | The address begin() talks to. This example passes 0x30, the ATtiny404's address (the ADDRESS switch sets 0x30 to 0x37). With any other address (the default is 0x49), the module talks to an ADS1115 at that address and to the LMP91000 directly instead. |
config_pin | int | A GPIO the module holds high and pulls low while it configures the LMP91000 directly. Unused when the address is 0x30 to 0x37. |
begin()returns boolChecks that the board answers, sends the ADC configuration, then configures the LMP91000 front end. Each command to the board is retried for up to 500 ms. Call it once before reading.
ReturnsTrue when every step succeeded, False when the board did not answer or reported an error.
getPPM()returns floatReads the ADC through the board, 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.
getPPB()returns floatThe commented-out alternative in the example: takes a reading 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 the I2C bus, create one ElectrochemicalGasSensor with a sensor preset and
the board's address, call begin() once, then call getPPM() whenever you want
a reading. To use several boards on one bus, give each a different address with
its ADDRESS switch and create one object per address.