Skip to main content

Two sensors

What this example shows​

The twoSensors example creates two sensor objects on one I2C bus, starts both, and every 2.5 seconds prints a reading from each: sensorCO in PPM and sensorNO2 in PPB. sensorCO uses the SENSOR_CO configuration, address 0x4A and pin 25 for LMPEN; sensorNO2 uses SENSOR_NO2, address 0x49 and pin 32.

You do not need to wire LMPEN on this board, whatever the sketch's comments say: the board holds it low itself. Where the comments mention address jumpers on the back of the breakout, this board's own address, from 0x30 to 0x37, is set with the three-position ADDRESS switch on its top side.

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

What two of these boards print with the sketch as written has not been measured yet. When this board was tested over Qwiic, an I2C scan found only one address, 0x30, so nothing answered at 0x4A or 0x49, the addresses this sketch passes, and with the MicroPython library begin() failed for a sensor created at 0x49. If either begin() fails, the sketch prints ERROR: Can't init the sensor! Check connections! and stops.

When both begin() calls succeed, the sketch prints:

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

After that, every 2.5 seconds, a line CO sensor reading: with a value with five decimals and PPM, and a line NO2 sensor reading: with a value with five decimals and PPB.

Functions used​

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

Creates one sensor object with a sensor configuration, an address and an LMPEN pin. Nothing is sent until you call begin(). begin() picks how to talk to the board from the address: from 0x30 to 0x37 it uses the ATtiny404 bridge's commands and ignores the third argument; any other address is taken as the ADS1115's own address, and the LMP91000 is written directly.

ReturnsThe sensor object.

Parameters

TypeNameDescription
sensorType_tThe front end and conversion settings: SENSOR_CO for sensorCO, SENSOR_NO2 for sensorNO2.
uint8_t_adcAddrThe address: 0x4A for sensorCO, 0x49 for sensorNO2.
int_configPinThe GPIO pin connected to that board's LMPEN: 25 for sensorCO, 32 for sensorNO2. Not used for an address from 0x30 to 0x37.
sensorCO.begin()returns bool

Starts I2C with Wire.begin(). For an address from 0x30 to 0x37 it checks that the bridge answers, sends it the ADC gain and the slowest ADC data rate, then the LMP91000 settings; each bridge command waits up to 500 ms. For any other address it starts the ADS1115 at that address with the same gain and data rate, sets the LMPEN pin high if one was given, then writes the LMP91000 settings, holding that pin low while it does. The example calls it on sensorCO, then on sensorNO2.

Returnstrue when every step succeeded, false otherwise.

sensorCO.getPPM()returns double

Takes one ADC reading and turns the voltage into a concentration: it subtracts the internal zero (a share of the 2.5 V reference), adds the configuration's zero calibration, divides by the TIA gain to get the cell current, then divides by the configuration's nanoamperes per PPM. A negative result is returned as 0.

ReturnsThe concentration in PPM.

sensorNO2.getPPB()returns double

The same reading as getPPM(), multiplied by 1000.

ReturnsThe concentration in PPB.

Putting it together​

SENSOR_CO is the preset for an SGX-4CO carbon monoxide cell and SENSOR_NO2 for an SGX-4NO2 nitrogen dioxide cell. The library has no preset for this board's SGX-4OX-ROHS oxygen cell yet, so the values the example prints are calculated with the carbon monoxide (CO) and nitrogen dioxide (NO2) presets' settings. The custom configuration page shows how to write your own.