Single sensor
What this example shows
The library's singleSensor example creates one sensor object, starts it and prints the gas concentration in ppm every 2.5 seconds.
The example creates the sensor with the CO cell preset, SENSOR_CO. SENSOR_SO2 is the library's preset for the SGX-4SO2 cell on this board: change SENSOR_CO to SENSOR_SO2 in the line that creates sensor to get SO2 values.
On this board the LMP91000 and ADS1115 sit on the board's own bus 2, and your controller reaches them through the board's address on the Qwiic bus: 0x30 as shipped, 0x30 to 0x37 with the ADDRESS switch. The example passes no address, so it uses the library's default, 0x49, which talks to an ADS1115 directly. Pass the board's address as the second argument of the constructor, for example ElectrochemicalGasSensor sensor(SENSOR_SO2, 0x30);. Where the header says to connect LMPEN to GND or a GPIO pin, on this board LMPEN is already pulled to GND by a 10 kΩ resistor and also runs to the on-board microcontroller, and at the board's addresses the library ignores the enable-pin argument (see Hardware Details).
The listing is the library's file at the pinned commit, apart from a few comment-only lines in its header, which name the connector by an older name or link to other Soldered products, and are left out.
The example code
/**
**************************************************
*
* @file singleSensor.ino
* @brief See how to init one sensor and make a simple reading of PPM of gas.
*
* To successfully run the sketch:
* - Connect LMPEN pin to GND or a GPIO pin so the breakout 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"
// Configurations for each of the sensor types are in sensorConfigData.h in the library's 'src' folder
// Create the sensor object with the according type
ElectrochemicalGasSensor sensor(SENSOR_CO);
// If you are using a custom I2C address (for multiple sensors) use:
// ElectrochemicalGasSensor sensor(SENSOR_SO2, 0x4B, 5);
// For more details on this, see customAddress.ino
void setup()
{
Serial.begin(115200); // For debugging
// Init the breakout
if (!sensor.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("Sensor initialized successfully!");
}
void loop()
{
// Make the reading
double reading = sensor.getPPM();
// If PPB is more relevant for your sensor, you can use:
// double reading = sensor.getPPB();
// Print the reading with 5 digits of precision
Serial.print("Sensor reading: ");
Serial.print(reading, 5);
Serial.println(" PPM");
// Wait a bit before reading again
delay(2500);
}
Expected result:
Sensor initialized successfully!
After that line, a new Sensor reading: line follows every 2.5 seconds, with the concentration in ppm to five decimal places.
Functions used
ElectrochemicalGasSensor(sensorType _t, uint8_t _adcAddr = DEFAULT_ADC_ADDR, int _configPin = -1)returns noneCreates the sensor object from a cell preset, an address and an optional enable pin. The example passes only the preset, so the address is the default, 0x49.
ReturnsConstructor.
Parameters
| Parameter | Type | Description |
|---|---|---|
_t | sensorType | The cell preset. SENSOR_SO2 is the preset for this board's cell. |
_adcAddr | uint8_t | The address the library talks to. 0x30 to 0x37 is the bridge range, set by the board's ADDRESS switch; any other value talks to an ADS1115 directly at that address. Default 0x49. |
_configPin | int | GPIO for the front end's enable line. Ignored at addresses 0x30 to 0x37. Default -1. |
bool begin()returns boolStarts I2C, checks that the board answers, and sends the ADC and LMP91000 settings from the preset to the board. Call it once before any reading.
Returnstrue when the board answered and took every setting, false otherwise.
double getPPM()returns doubleHas the board take one ADC reading and turns it into a gas concentration, using the 2.5 V reference, the front end gain and the cell's sensitivity from the preset. A result below zero is returned as 0.
ReturnsGas concentration in ppm.
double getPPB()returns doubleThe same reading as getPPM(), in parts per billion. The example shows it in a comment as an alternative.
ReturnsGas concentration in ppb (getPPM() x 1000).
Putting it together
begin() sends the cell's settings once, then every getPPM() call asks the board for a fresh conversion. To print ppb instead, swap the two double reading = lines and change " PPM" to " PPB", so the label matches the value.