Skip to main content

Read two sensors on one bus

What this example shows​

The twoSensors example reads two boards on the same I2C bus, one with the carbon monoxide preset and one with the nitrogen dioxide preset, and prints the CO reading in parts per million (PPM) and the NO2 reading in parts per billion (PPB) every 2.5 seconds.

Wire both boards to your controller through their top headers for this example, not through the Qwiic connectors: the controller's SDA to each board's SDA2, its SCL to each board's SCL2, its 3.3 V supply to each 3V3 and its ground to each GND. At these addresses the library talks to the ADS1115 and the LMP91000 directly on SDA2 and SCL2; the boards' ATtiny404 bridges are not used. Over Qwiic, only the bridge example in the Quick Start works.

The example code​

Each board gets its own ElectrochemicalGasSensor object with its own address: sensorCO at 0x4A with GPIO 25 as _configPin, and sensorNO2 at 0x49 with GPIO 32. Wire the LMPEN pin on the 0x4A board's header to GPIO 25 and the one on the 0x49 board's header to GPIO 32. On this board, LMPEN also goes to the board's ATtiny404.

Some of the sketch's comments do not match this board. They say to set different addresses with the jumpers on the back of the breakout. On this board, the address on the Qwiic bus is set with the ADDRESS switch on the top of the board, and JP2 to JP4 set the address of the ADS1115 on the board's own bus (SDA2 and SCL2), the bus this example uses. JP2 is closed as shipped (0x49), so the 0x49 board stays as it is. On the 0x4A board, open JP2 before you close JP3, and leave JP4 open: closing JP3 with JP2 still closed ties SDA2 to 3V3 and stops the bus. Hardware details lists every jumper and the combinations to avoid.

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

🔗Open the complete example

examples/twoSensors at commit c4df54b

→

Open the Serial Monitor at 115200 baud. The sketch prints:

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, a CO sensor reading: line ending in PPM and a NO2 sensor reading: line ending in PPB, each value with five decimals. If either begin() fails, the sketch prints ERROR: Can't init the sensor! Check connections! and stops; the last "initialized" line shows which board answered.

Functions used​

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

Creates the sensor object for one board; the example creates one per board. The address decides how begin() talks to it: for an address from 0x30 to 0x37 it uses the protocol of the ATtiny404 bridge; for any other address it talks to an ADS1115 at that address and to the LMP91000 itself. The default is DEFAULT_ADC_ADDR (0x49).

ReturnsConstructor.

Parameters

NameTypeDescription
_tsensorTypeThe preset for the board's cell: SENSOR_CO, SENSOR_NO2, SENSOR_SO2, SENSOR_O3, SENSOR_NO, SENSOR_H2S, SENSOR_NH3, SENSOR_CL2, or your own sensorType.
_adcAddruint8_tThe address begin() uses, different for each board. 0x30 to 0x37 selects the bridge protocol; this board answers at 0x30 out of the box, or at the address its switch sets. Any other value is taken as an ADS1115 address. 0x49 by default.
_configPinintA GPIO for the LMP91000 enable line, one per board. The library holds it high and pulls it low only while it writes the LMP91000's registers itself; for an address from 0x30 to 0x37 it is not used. -1 (none) by default.
bool begin()returns bool

Starts I2C with Wire.begin() and picks the protocol from the address. For 0x30 to 0x37 it checks that the bridge answers, sends the ADC gain from the preset and has the bridge write the LMP91000's registers, each step giving up after 500 ms. For any other address it checks that an ADS1115 answers at that address, sets its gain, and writes the LMP91000's registers when the LMP91000 reports ready, pulling _configPin low around the writes when one is given. Call it once for each board.

Returnstrue when every step succeeded, false otherwise.

double getPPM()returns double

Takes one reading of the ADS1115's channel 0 (through the bridge for 0x30 to 0x37) and converts it to a concentration with the preset: it removes the internal zero offset, adds the zero calibration, divides by the amplifier gain and by the cell's sensitivity in nA per PPM. A negative result is returned as 0. The example reads the CO board with it.

ReturnsThe gas concentration in PPM.

double getPPB()returns double

The same reading as getPPM(), in parts per billion. The example reads the NO2 board with it.

ReturnsThe gas concentration in PPB, getPPM() x 1000.

Putting it together​

Create one sensor object per board, each with its own address and preset. Call begin() on each in setup() and stop if one returns false, then read each board with getPPM() or getPPB().