Skip to main content

Two sensors on one bus

What this example shows​

The twoSensors example reads two boards on the same I2C bus every 2.5 seconds. It creates one sensor object per board, each with its own address and config pin: sensorCO with SENSOR_CO at 0x4A and GPIO 25, sensorNO2 with SENSOR_NO2 at 0x49 and GPIO 32. It prints the first in ppm and the second in parts per billion (ppb).

What it needs on this board​

The example's addresses, 0x4A and 0x49, are ADS1115 addresses, not the board's bridge addresses. At an address outside 0x30 to 0x37 the library does not go through the bridge: begin() starts each ADS1115 at its address and writes each LMP91000's configuration itself, over the controller's I2C, with that object's config pin held low. The example's header asks for different addresses set with the jumpers on the back of each breakout, and for each board's LMPEN connected to a GPIO pin.

On this board the ADS1115 and the LMP91000 sit on the internal bus, which the top header brings out as SDA2 and SCL2; JP2 to JP4 set the ADS1115's address on that bus. The Qwiic connectors and SDA1/SCL1 reach only the ATtiny404 bridge, and an I2C scan over Qwiic finds only the bridge, at 0x30 as shipped. LMPEN is on the bottom header, pulled to GND through 10 kΩ and also wired to the ATtiny404.

So wire each board's top header for this example, not the Qwiic cable: connect your controller's SDA to each board's SDA2, its SCL to each board's SCL2, its 3.3 V pin to 3V3 and its GND to GND. Over Qwiic only each board's bridge answers.

Over Qwiic, two of these boards share one bus through their bridges: each needs its own address from 0x30 to 0x37, set with its three-switch ADDRESS DIP switch, and its own sensor object at that address. The switch and the jumpers are described on Hardware Details.

SENSOR_NO2 is the library's preset for the nitrogen dioxide cell. With two of these CO boards, change SENSOR_NO2 to SENSOR_CO in the line that creates sensorNO2, so both objects use the preset for this board's SGX-4CO carbon monoxide cell.

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 lines below are from a run of this example with two boards on one bus, each wired to the controller through its header's SDA2, SCL2, 3V3 and GND pins as described above.

Expected result in the Serial Monitor at 115200 baud:

Serial MonitorAutoscrollShow timestampNewline115200 baud
Message (Enter to send message to the board)
CO sensor initialized successfully!
NO2 sensor initialized successfully!
CO sensor reading: 0.29550 PPM
NO2 sensor reading: 325.58790 PPB
CO sensor reading: 0.29626 PPM
NO2 sensor reading: 316.98846 PPB
CO sensor reading: 0.32462 PPM
NO2 sensor reading: 292.77867 PPB

Then two lines every 2.5 seconds: CO sensor reading: with the first board's value in ppm to five decimals and PPM, and NO2 sensor reading: with the second board's value in ppb and PPB. A result below zero is printed as 0.00000. If either board does not 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 none

Creates one sensor object. The example creates two, one per board, each with a preset, an ADS1115 address (0x4A, then 0x49) and a config pin.

ReturnsConstructor.

Parameters

ParameterTypeDescription
_tsensorTypeThe cell's preset. For this board's SGX-4CO cell use SENSOR_CO (70 nA/ppm, ADC range 4.096 V, 14 kΩ amplifier gain, 20% internal zero).
_adcAddruint8_tThe I2C address. Default DEFAULT_ADC_ADDR, 0x49. From 0x30 to 0x37 the library talks to the board's ATtiny404 bridge.
_configPinintA GPIO the library drives low while it sends the front end's configuration and high otherwise. Not used at a 0x30 to 0x37 address. Default -1, none.
bool begin()returns bool

Starts one sensor. It starts I2C with Wire.begin(). At an address from 0x30 to 0x37 it checks that the bridge answers, sends it the ADC gain and data rate from the preset and the front end's configuration, waiting up to 500 ms for each reply. At any other address it starts the ADC at that address, sets its gain and its slowest data rate, then writes the front end's configuration with that object's config pin held low.

Returnstrue if every step succeeded, false otherwise.

double getPPM()returns double

Makes one measurement on that object's board and converts it to ppm with the object's preset.

ReturnsThe gas concentration in ppm. A negative result is returned as 0.

double getPPB()returns double

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

ReturnsgetPPM() multiplied by 1000.

Putting it together​

Give each board its own address, create one object per board with SENSOR_CO and that address, call begin() on each in setup() and check it, then read each object with getPPM() or getPPB() in your loop. The example's addresses are ADS1115 addresses, each with a config pin on that board's LMPEN; through the boards' bridges, the addresses are the ones on their ADDRESS switches, from 0x30 to 0x37, and the config pin is not used.