Read two sensors on one bus
What this example shows
The twoSensors example reads two boards on one I2C bus. It creates one
sensor object per board, each with its own preset and address: sensorCO at
ADC address 0x4A with LMPEN on GPIO 25, and sensorNO2 at 0x49 with
LMPEN on GPIO 32. It prints the first reading in ppm and the second in ppb
every 2.5 seconds. At these addresses the library talks to each board's
ADS1115 and LMP91000 directly, not through the ATtiny404 bridge, and the
sketch's header asks for each board's LMPEN connected to a GPIO. See the
library overview
for how the address decides this.
On this board the ADS1115 address is set by the solder jumpers JP2 (0x49,
closed as it comes), JP3 (0x4A) and JP4 (0x4B). The board for sensorNO2
keeps JP2 closed for 0x49. On the board for sensorCO, cut JP2 and then
bridge JP3 for 0x4A. Keep exactly one of JP2, JP3 and JP4 closed on each
board. See
Hardware Details
for the jumper table and which combinations to avoid.
The example passes SENSOR_CO and SENSOR_NO2, the library's presets for
carbon monoxide and nitrogen dioxide cells. For a board with this
SGX-4NO-250 nitric oxide cell, use SENSOR_NO on its line: 400 nA per ppm, a
120 kΩ amplifier gain and a positive 12% bias.
The example code
The listing is the library's file at the pinned commit. Only comment lines are left out: the one that names the old connector, and the header lines that link other products' store pages.
/**
**************************************************
*
* @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, with SENSOR_NO on both lines (readings in clean air; yours
will differ):
CO sensor initialized successfully!
NO2 sensor initialized successfully!
CO sensor reading: 0.00606 PPM
NO2 sensor reading: 5.89242 PPB
CO sensor reading: 0.00588 PPM
NO2 sensor reading: 5.92183 PPB
CO sensor reading: 0.00621 PPM
NO2 sensor reading: 6.27184 PPB
The readings come in pairs of lines starting CO sensor reading: (in ppm, ending
PPM) and NO2 sensor reading: (in ppb, ending PPB), one pair every 2.5
seconds, each value to five decimals. The labels come from the sketch, whatever
preset you pass. If either begin() fails, 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 ElectrochemicalGasSensorCreates one sensor object per board: sensorCO and sensorNO2 in the example. Nothing is sent over I2C until you call begin().
ReturnsThe sensor object.
Parameters
| Type | Name | Description |
|---|---|---|
sensorType | _t | The cell preset, SENSOR_CO and SENSOR_NO2 in the example. SENSOR_NO is the one for this board's cell. |
uint8_t | _adcAddr | The board's I2C address, 0x4A and 0x49 in the example. For an address from 0x30 to 0x37 the library talks to that board's ATtiny404 bridge; for any other it talks to the ADS1115 at that address and to the LMP91000 directly. |
int | _configPin | The GPIO wired to that board's LMPEN, 25 and 32 in the example. The library holds it low only while it configures that board's LMP91000. Ignored at addresses 0x30 to 0x37. |
sensorCO.begin()returns boolStarts I2C with Wire.begin() and starts one board. For an address from 0x30 to 0x37 it checks that the board's bridge answers and sends the ADC and front-end settings through it, waiting up to 500 ms for each step. For any other address it starts the ADS1115 at that address with the preset's gain and the slowest data rate, then configures the LMP91000 (gain, load, reference, internal zero, bias and mode), holding the _configPin GPIO low while it does. sensorNO2.begin() does the same for the second board.
Returnstrue when every step succeeded, false otherwise.
sensorCO.getPPM()returns doubleTakes one ADC reading from that board and converts it: subtracts the internal zero (2.5 V times the preset's internal zero percentage), adds the zero calibration, divides by the amplifier gain to get the cell current, and divides that by the preset's nA per ppm.
ReturnsThe gas concentration in ppm. A negative result is returned as 0.
sensorNO2.getPPB()returns doubleTakes one reading from the second board with getPPM() and multiplies it by 1000.
ReturnsThe gas concentration in ppb.
Putting it together
Each board gets its own sensor object at its own address, and both share one
I2C bus. The sketch starts the first board, then the second, and stops at the
first one that fails. In loop() it reads both boards and prints the two
readings together.