Skip to main content

Single sensor

What this example shows​

The singleSensor example creates one sensor object with the SENSOR_CO configuration, starts it, and prints a reading in PPM every 2.5 seconds. It passes no address, so the constructor uses its default, DEFAULT_ADC_ADDR (0x49).

You do not need to wire LMPEN on this board, whatever the sketch's comments say: the board holds it low itself.

The example code​

Select Board
singleSensor.ino
/**
**************************************************
*
* @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);
}

What this board prints 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, and with the MicroPython library begin() failed for a sensor created at 0x49, the default this sketch uses. If begin() fails, the sketch prints ERROR: Can't init the sensor! Check connections! and stops.

When begin() succeeds, the sketch prints:

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

After it, a line Sensor reading: followed by a value with five decimals and PPM arrives every 2.5 seconds.

Functions used​

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

Creates the 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 in the example. The presets live in the library's src/sensorConfigData.h.
uint8_t_adcAddrThe address, DEFAULT_ADC_ADDR (0x49) when left out, as in the example.
int_configPinThe GPIO pin connected to LMPEN, -1 (none) when left out. Not used for an address from 0x30 to 0x37.
sensor.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.

Returnstrue when every step succeeded, false otherwise.

sensor.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, calculated with the sensor configuration you passed to the constructor.

sensor.getPPB()returns double

The commented-out alternative in the example: 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. The library has no preset for this board's SGX-4OX-ROHS oxygen cell yet, so the value the example prints is calculated with the CO cell's settings. The custom configuration page shows how to write your own.