Skip to main content

Read two sensors

What this example shows​

The twoSensors example creates two sensor objects, each with its own address and configuration pin, and prints a reading from each every 2.5 seconds: the first in PPM, the second in PPB. It creates sensorCO with SENSOR_CO at 0x4A with pin 25, and sensorNO2 with SENSOR_NO2 at 0x49 with pin 32.

The example's presets are for carbon monoxide and nitrogen dioxide, so its values are not H2S values. Do not use 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
twoSensors.ino
// Include the required library
#include "Electrochemical-Gas-Sensor-SOLDERED.h"

// Create two different sensor objects
ElectrochemicalGasSensor sensorCO(SENSOR_CO, 0x4A, 25);
ElectrochemicalGasSensor sensorNO2(SENSOR_NO2, 0x49, 32);
// To see how the custom addresses are determined, check customAddress.ino

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

// Init breakout #1
if (!sensorCO.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("CO sensor initialized successfully!");

// Init breakout #2
if (!sensorNO2.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("NO2 sensor initialized successfully!");
}

void loop()
{
// Make the reading of CO and print it
double COreading = sensorCO.getPPM();

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

// Make the reading of NO2 and print it
double NO2reading = sensorNO2.getPPB();

// Print the reading with 5 digits of precision
Serial.print("NO2 sensor reading: ");
Serial.print(NO2reading, 5);
Serial.println(" PPB");

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

Expected result:

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

Then a CO sensor reading: line in PPM and an NO2 sensor reading: line in PPB follow every 2.5 seconds, each to five decimals. If a 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 one sensor object per gas cell, each with its own address and configuration pin. 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, 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 passes 0x4A for sensorCO and 0x49 for sensorNO2.
_configPinintA GPIO the library holds high and pulls low while it configures that sensor's LMP91000 directly. Ignored at addresses from 0x30 to 0x37. This example passes 25 and 32.
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, sets the configuration pin as an output held high, and pulls it low while it configures the LMP91000. Call it once for each sensor object.

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

Takes a reading with getPPM() and multiplies it by 1000.

ReturnsGas concentration in PPB.

Putting it together​

Create one ElectrochemicalGasSensor object per gas cell, each with its own address and configuration pin, call begin() on each, then read each one with getPPM() or getPPB().