Skip to main content

Read the gas concentration

What this example shows​

The bridgeBoard example starts the board at its default address, 0x30, sets up the analog front end for one gas cell, and prints the gas concentration in parts per million (PPM) every 2.5 seconds.

Behind address 0x30 is the board's ATtiny404. It configures the LMP91000 and reads the ADS1115 on the board's own internal bus, so your sketch only ever talks to one I2C device.

The example code​

The sketch uses the carbon monoxide preset, SENSOR_CO. Change it to the preset for your cell: SENSOR_NO2, SENSOR_SO2, SENSOR_O3, SENSOR_NO, SENSOR_H2S, SENSOR_NH3 or SENSOR_CL2. The sketch's comments call the address setting "jumpers on the back of the board"; on this board it is the ADDRESS switch on the top. If you set it to another address, change 0x30 to match. The comments also say LMPEN is tied to GND; on this board it goes to the header and to the ATtiny404, and the library does not drive it. The comment that mentions "legacy boards with 0x49/0x4A/0x4B" means boards read at those addresses, where the library talks to an ADS1115 directly instead of to the bridge. This board answers at 0x30 to 0x37.

Select Board
bridgeBoard.ino
/**
**************************************************
*
* @file bridgeBoard.ino
* @brief See how to init a sensor on the new ATtiny404 bridge board revision,
*
* To successfully run the sketch:
* - Set the desired address using the jumpers on the back of the board
* (address = 0x30 + jumper offset, giving a range of 0x30-0x37)
* - Run the sketch and open serial monitor at 115200 baud!
*
* Note: on this board revision, LMPEN is hardwired to GND, so there's
* no configPin argument to pass - the constructor's third argument
* is ignored whenever the address falls in the bridge range (0x30-0x37).
*
*
* @authors @ soldered.com
***************************************************/

// Include the required library
#include "Electrochemical-Gas-Sensor-SOLDERED.h"

// Any additional boards on the same bus just need a different jumper setting
// (0x30-0x37), same as twoSensors.ino does for legacy boards with 0x49/0x4A/0x4B.
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, in the Serial Monitor at 115200 baud:

Serial MonitorAutoscrollShow timestampNewline115200 baud
Message (Enter to send message to the board)
Sensor initialized successfully!

Then a line every 2.5 seconds: Sensor reading: , the concentration with five decimals, and PPM. 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 for one board. The address decides how begin() talks to it: for an address from 0x30 to 0x37 it uses the protocol of the ATtiny404 bridge; for any other address it talks to an ADS1115 at that address and to the LMP91000 itself. The default is DEFAULT_ADC_ADDR (0x49).

ReturnsConstructor.

Parameters

NameTypeDescription
_tsensorTypeThe preset for your cell: SENSOR_CO, SENSOR_NO2, SENSOR_SO2, SENSOR_O3, SENSOR_NO, SENSOR_H2S, SENSOR_NH3, SENSOR_CL2, or your own sensorType.
_adcAddruint8_tThe address begin() uses. 0x30 to 0x37 selects the bridge protocol; this board answers at 0x30 out of the box, or at the address its switch sets. The example passes 0x30.
_configPinintA GPIO for the LMP91000 enable line. The library holds it high and pulls it low only while it writes the LMP91000's registers itself; for an address from 0x30 to 0x37 it is not used. -1 (none) by default.
bool begin()returns bool

Starts I2C with Wire.begin(), checks that the bridge answers, sends the ADC gain from the preset, and configures the LMP91000 with the preset's gain, load, reference, bias and mode. Call it once before reading.

Returnstrue when the bridge answered and both configurations succeeded, false otherwise. Each step gives up after 500 ms without an answer.

double getPPM()returns double

Takes one ADC reading through the bridge and converts it to a concentration with the preset: it removes the internal zero offset, adds the zero calibration, divides by the amplifier gain and by the cell's sensitivity in nA per PPM. A negative result is returned as 0.

ReturnsThe gas concentration in PPM.

double getPPB()returns double

The same reading as getPPM(), in parts per billion. The example shows it as a commented-out alternative to getPPM().

ReturnsThe gas concentration in PPB, getPPM() x 1000.

Putting it together​

Create one ElectrochemicalGasSensor per board, with the preset for its cell and its address. Call begin() once in setup() and stop if it returns false. Then call getPPM() or getPPB() as often as you need a reading.

To run more than one board on the same bus, give each its own address with the address switch (0x30 to 0x37) and create one object per address.