Skip to main content

Read the gas concentration

What this example shows​

The library's singleSensor example creates the sensor, starts it, and prints the gas concentration in ppm every 2.5 seconds.

The example creates the sensor with the carbon monoxide preset, SENSOR_CO. SENSOR_NO2 is the library's preset for this board's SGX-4NO2 cell; change it in the line that creates the sensor.

The example code​

Select Board
singleSensor.ino
/**
**************************************************
*
* @file singleSensor.ino
* @brief See how to init one sensor and make a simple reading of PPM of gas.
*
* To successfully run the sketch:
* - Connect LMPEN pin to GND or a GPIO pin so the breakout can be configured
* - Run the sketch and open serial monitor at 115200 baud!
*
*
* @authors Robert @ soldered.com
***************************************************/

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

// Configurations for each of the sensor types are in sensorConfigData.h in the library's 'src' folder
// Create the sensor object with the according type
ElectrochemicalGasSensor sensor(SENSOR_CO);

// If you are using a custom I2C address (for multiple sensors) use:
// ElectrochemicalGasSensor sensor(SENSOR_SO2, 0x4B, 5);
// For more details on this, see customAddress.ino

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!");
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 every 2.5 seconds the sketch prints a line that starts with Sensor reading:, followed by the concentration in ppm with five decimals. If begin() fails, it prints ERROR: Can't init the sensor! Check connections! and stops.

Functions used​

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

Creates the sensor object. Nothing is sent to the board until you call begin(). The address decides how begin() talks to the board: from 0x30 to 0x37 it sends its commands to the ATtiny404, and from 0x48 to 0x4B (DEFAULT_ADC_ADDR is 0x49) it addresses the ADS1115 at that address and the LMP91000 directly.

ReturnsThe sensor object.

Parameters

TypeNameDescription
sensorType_tThe cell's preset. SENSOR_NO2 is the preset for this board's SGX-4NO2 cell; the example passes SENSOR_CO.
uint8_t_adcAddrThe address begin() talks to, 0x49 by default; the example leaves it at the default.
int_configPinGPIO for the LMP91000 enable pin (LMPEN), used only at 0x48 to 0x4B. -1 (none) by default; the example does not pass one.
sensor.begin()returns bool

Starts Wire and writes the preset to the board. From 0x30 to 0x37 it checks that the ATtiny404 answers, then sends it the ADC gain and data rate and the LMP91000 configuration, each step waiting up to 500 ms for the answer. From 0x48 to 0x4B it starts the ADS1115 at that address, sets the preset's gain and the slowest data rate, and writes the LMP91000 configuration, pulling _configPin low while it does when one is given. Call it once in setup().

Returnstrue when every step succeeded, false otherwise.

sensor.getPPM()returns double

Takes a new ADC reading and turns it into a gas concentration: the voltage minus the internal zero, plus the calibration offset, divided by the TIA gain and by the cell's sensitivity in the preset. A negative result is returned as 0.

ReturnsThe gas concentration in ppm.

sensor.getPPB()returns double

The same reading as getPPM(), multiplied by 1000. The example mentions it in a comment as the line to use when parts per billion suit your sensor better.

ReturnsThe gas concentration in ppb.

Putting it together​

begin() runs once in setup(). If it fails, the sketch prints the error and stays in an endless loop. In loop(), each getPPM() call reads the cell again, and the sketch prints the value with five decimals and waits 2.5 seconds.