Skip to main content

Read one sensor

What this example shows​

The singleSensor example creates one sensor at the library's default address, 0x49, starts it, and prints the gas concentration in PPM every 2.5 seconds.

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

The example code​

Select Board
singleSensor.ino
// 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 a line starting with Sensor reading: follows every 2.5 seconds, with the concentration in PPM to five decimals. If the sensor does not answer, the sketch 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 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. For this board's cell, pass a custom one with 460 nA per ppm.
_adcAddruint8_tThe address begin() talks to. This example leaves it out, so the default, 0x49 (DEFAULT_ADC_ADDR), is used.
_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 otherwise.

double getPPM()returns double

Reads the ADC, 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 type, call begin() once in setup(), then call getPPM() or getPPB() whenever you want a reading.