Skip to main content

Read two sensors on one bus

What this example shows​

The library's twoSensors example reads two boards on one I2C bus. It creates one sensor object per board, starts both, and prints a carbon monoxide reading in ppm and an NO2 reading in ppb every 2.5 seconds.

The example creates sensorCO with the SENSOR_CO preset at address 0x4A with LMPEN on GPIO 25, and sensorNO2 with the SENSOR_NO2 preset, the library's preset for this board's SGX-4NO2 cell, at address 0x49 with LMPEN on GPIO 32.

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!

Then every 2.5 seconds the sketch prints two lines: one starting with CO sensor reading:, followed by that concentration in ppm, and one starting with NO2 sensor reading:, followed by the NO2 concentration in ppb, each with five decimals. If either sensor fails to start, 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 one 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. The example creates two, one per board.

ReturnsThe sensor object.

Parameters

TypeNameDescription
sensorType_tThe cell's preset. The example passes SENSOR_CO and SENSOR_NO2, the preset for this board's SGX-4NO2 cell.
uint8_t_adcAddrThe address begin() talks to, 0x49 by default; the example passes 0x4A and 0x49.
int_configPinGPIO for the LMP91000 enable pin (LMPEN), used only at 0x48 to 0x4B. -1 (none) by default; the example passes 25 and 32.
sensor.begin()returns bool

Starts Wire and writes the preset to one 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 for each sensor in setup().

Returnstrue when every step succeeded, false otherwise.

sensor.getPPM()returns double

Takes a new ADC reading from one sensor 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 reads the NO2 sensor with it.

ReturnsThe gas concentration in ppb.

Putting it together​

Both sensor objects are created before setup() runs and differ in their preset, address and LMPEN pin. setup() starts sensorCO, then sensorNO2, and stops in an endless loop at the first one that fails. Each pass of loop() reads sensorCO with getPPM() and sensorNO2 with getPPB(), prints both values with five decimals, and waits 2.5 seconds.