Skip to main content

Two sensors

What this example shows​

The library's twoSensors example reads two boards on one I2C bus. It creates sensorCO with the CO cell preset at address 0x4A with GPIO 25 as its enable pin, and sensorNO2 with the NO2 cell preset at address 0x49 with GPIO 32. Every 2.5 seconds it prints the first reading in ppm and the second in ppb.

SENSOR_SO2 is the library's preset for the SGX-4SO2 cell on this board: put it in place of the preset of each sensor that is one of these boards. This board's address switch and jumpers are described on Hardware Details.

On this board the LMP91000 and ADS1115 sit on the board's own bus 2, and your controller reaches them through the board's address on the Qwiic bus, 0x30 to 0x37 with the ADDRESS switch (0x30 as shipped). The example's 0x4A and 0x49 talk to an ADS1115 directly. For two of these boards, give each its own ADDRESS switch address and pass that address as the second argument of its constructor. Where the header says to connect the LMPEN pins to a GPIO pin, on this board LMPEN is already pulled to GND by a 10 kΩ resistor and also runs to the on-board microcontroller, and at the board's addresses the library ignores the enable-pin argument, here GPIO 25 and GPIO 32.

The header's "Set different addresses using the jumpers on the back of the breakout" does not give this board's procedure. Leave the address jumpers JP2 to JP4 as shipped: moving JP3 or JP4 may stop the board giving readings, and closing two of them shorts 3V3 to SDA2, 3V3 to SCL2, or SDA2 to SCL2, depending on which pair. See Hardware Details.

The listing is the library's file at the pinned commit, apart from a few comment-only lines in its header, which name the connector by an older name or link to other Soldered products, and are left out.

The example code​

Select Board
twoSensors.ino
/**
**************************************************
*
* @file twoSensors.ino
* @brief See how to init two sensors and make two readings of PPM of gas.
*
* To successfully run the sketch:
* - Set different addresses using the jumpers on the back of the breakout
* - Connect LMPEN pins to a GPIO pin so the breakouts 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"

// 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!

After those lines, a CO sensor reading: line in ppm and a NO2 sensor reading: line in ppb follow every 2.5 seconds, each to five decimal places. If a board does not start, the sketch prints ERROR: Can't init the sensor! Check connections! and stops there.

Functions used​

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

Creates one sensor object from a cell preset, an address and an optional enable pin. The example creates two, one per board.

ReturnsConstructor.

Parameters

ParameterTypeDescription
_tsensorTypeThe cell preset. SENSOR_SO2 is the preset for this board's cell.
_adcAddruint8_tThe address the library talks to. 0x30 to 0x37 is the bridge range, set by the board's ADDRESS switch; any other value talks to an ADS1115 directly at that address. Default 0x49.
_configPinintGPIO for the front end's enable line. Ignored at addresses 0x30 to 0x37. Default -1.
bool begin()returns bool

Starts I2C, checks that the board answers, and sends the ADC and LMP91000 settings from the preset to the board. The example calls it once for each sensor.

Returnstrue when the board answered and took every setting, false otherwise.

double getPPM()returns double

Has the board take one ADC reading and turns it into a gas concentration, using the 2.5 V reference, the front end gain and the cell's sensitivity from the preset. A result below zero is returned as 0.

ReturnsGas concentration in ppm.

double getPPB()returns double

The same reading as getPPM(), in parts per billion. The example uses it for the second sensor.

ReturnsGas concentration in ppb (getPPM() x 1000).

Putting it together​

Each ElectrochemicalGasSensor object keeps its own address and preset, so one sketch can read several boards on the same bus, as long as no two share an address.