Skip to main content

Read the gas concentration

What this example shows​

The bridgeBoard 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 library talks to the board's ATtiny404, which configures the LMP91000 front end and reads the ADS1115 for you.

The example is written with SENSOR_CO, the library's carbon monoxide preset, so its PPM values are not H2S values. Do not switch it to the library's SENSOR_H2S preset 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 with SENSOR_H2S the reading comes out about 2.6 times too low. Pass a custom sensor type with the SENSOR_H2S settings and 460 nA per ppm instead, as Quick Start shows.

If you moved the board's address with the ADDRESS switch, change 0x30 to match.

The example code​

Select Board
bridgeBoard.ino
// Include the required library
#include "Electrochemical-Gas-Sensor-SOLDERED.h"

ElectrochemicalGasSensor sensor(SENSOR_CO, 0x30);

void setup()
{
Serial.begin(115200); // For debugging

// Init the breakout
if (!sensor.begin())
{
// Can't init? Notify the user and go to infinite loop
Serial.println("ERROR: Can't init the sensor! Check connections and jumper address!");
while (true)
delay(100);
}

Serial.println("Sensor initialized successfully!");
}

void loop()
{
// Make the reading
double reading = sensor.getPPM();

// If PPB is more relevant for your sensor, you can use:
// double reading = sensor.getPPB();

// Print the reading with 5 digits of precision
Serial.print("Sensor reading: ");
Serial.print(reading, 5);
Serial.println(" PPM");

// Wait a bit before reading again
delay(2500);
}

Expected result:

Serial MonitorAutoscrollShow timestampNewline115200 baud
Message (Enter to send message to the board)
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 sketch prints ERROR: Can't init the sensor! Check connections and jumper address! and stops.

Functions used​

ElectrochemicalGasSensor(sensorType _t, uint8_t _adcAddr = DEFAULT_ADC_ADDR, int _configPin = -1)returns none

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.

ReturnsConstructor.

Parameters

NameTypeDescription
_tsensorTypePreset from the library: SENSOR_CO, SENSOR_NO2, SENSOR_SO2, SENSOR_O3, SENSOR_NO, SENSOR_H2S, SENSOR_NH3 or SENSOR_CL2. Or a custom sensorType. 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 type with 460 nA per ppm.
_adcAddruint8_tThe address begin() talks to. This example passes 0x30, the ATtiny404's address (the ADDRESS switch sets 0x30 to 0x37). The default is 0x49.
_configPinintA GPIO the library holds high and pulls low while it configures the LMP91000 directly. Ignored at addresses from 0x30 to 0x37. This example leaves it out (-1, none).
bool begin()returns bool

Starts I2C and picks how to talk to the board from the address. With an address from 0x30 to 0x37 it checks that the board's ATtiny404 answers and sends it the ADC gain from the sensor type with the slowest data rate; each command waits up to 500 ms for an answer. With any other address it starts an ADS1115 at that address with the same gain and data rate. Either way it then configures the LMP91000 front end. Call it once before reading.

Returnstrue when every step succeeded, false when the board did not answer or reported an error.

double getPPM()returns double

Reads 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.

ReturnsGas concentration in PPM.

double getPPB()returns double

The commented-out alternative in the example: takes a reading with getPPM() and multiplies it by 1000.

ReturnsGas concentration in PPB.

Putting it together​

Create one ElectrochemicalGasSensor with a sensor preset and the board's address, call begin() once in setup(), 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.