Skip to main content

Two sensors

What this example shows​

The library's twoSensors example creates two sensor objects, each with its own preset, address and pin, starts both, and every 2.5 s prints the first one's reading in ppm and the second one's in ppb.

The example's sensors use SENSOR_CO (at 0x4A, pin 25) and SENSOR_NO2 (at 0x49, pin 32). The library calculates each reading from its preset's sensitivity and gain settings; the preset for this board's chlorine cell is SENSOR_CL2.

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);
}

The header comment asks for different addresses set with jumpers on the back and for the LMPEN pins to be wired to GPIOs. On this board all jumpers are on the top side and none of them sets the board's address, and a 10 kΩ resistor holds LMPEN low while it is wired to the ATtiny404, so you leave it unconnected (see Hardware details).

Expected result:

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

Then two lines every 2.5 s: CO sensor reading: with the first sensor's value in ppm to five decimals and PPM, and NO2 sensor reading: with the second sensor's value in ppb to five decimals and PPB. If either begin() fails, 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 ElectrochemicalGasSensor

Creates a sensor object. It stores the settings only; nothing is sent to the board until begin(). The example creates two.

ReturnsThe sensor object.

Parameters

TypeNameDescription
sensorType_tThe cell preset. The example passes SENSOR_CO and SENSOR_NO2; this board's SGX-4CL2 cell has the preset SENSOR_CL2.
uint8_t_adcAddrThe I2C address, DEFAULT_ADC_ADDR (0x49) when left out; the example passes 0x4A and 0x49. For an address from 0x30 to 0x37, the range of this board's ADDRESS switch, begin() talks to the board's ATtiny404; for any other address it looks for an ADS1115 at that address and the LMP91000 directly on the bus.
int_configPinA GPIO that begin() drives low while it configures the LMP91000 directly; the example passes 25 and 32. -1 (none) when left out. Unused for addresses 0x30 to 0x37.
bool begin()returns bool

Starts Wire, checks that the board answers, sets the preset's ADC gain and data rate, and configures the LMP91000 with the preset's settings. The example calls it once for each sensor object in setup().

Returnstrue when the board answered and both configuration steps succeeded, false otherwise.

double getPPM()returns double

Takes one reading and converts it to ppm: it subtracts the internal zero voltage (the 2.5 V reference times the preset's internal zero percentage), adds the zero calibration, divides by the amplifier gain and by the preset's sensitivity in nA per ppm. A result below zero is returned as 0.

ReturnsThe gas concentration in ppm.

double getPPB()returns double

Same reading as getPPM(), times 1000. The example reads the second sensor with it.

ReturnsThe gas concentration in ppb.

The example also uses Serial and delay(), which come with the Arduino core, not with this library.

Putting it together​

Each sensor object keeps its own preset, address and pin, so the loop reads them one after the other with the same functions. Each object needs its own address.