Skip to main content

Custom sensor configuration

What this example shows​

The customConfig example builds its own sensorType, named SENSOR_CUSTOM, instead of using one of the library's presets, passes it to the constructor, and prints the gas concentration in ppm every 2.5 seconds. The long comment in the sketch lists every field and the values each one takes.

The example's own values are not those of this board's SGX-4CO carbon monoxide cell. The library's preset for that cell is SENSOR_CO, and two fields differ:

FieldIn the exampleSENSOR_CO value
nanoAmperesPerPPM100.0F70.0F
internalZeroCalibration00
adsGainADS_GAIN_4_096VADS_GAIN_4_096V
TIA_GAIN_IN_KOHMSTIA_GAIN_120_KOHMTIA_GAIN_14_KOHM
RLOADRLOAD_10_OHMRLOAD_10_OHM
REF_SOURCEREF_EXTERNALREF_EXTERNAL
INTERNAL_ZEROINTERNAL_ZERO_20_PERCENTINTERNAL_ZERO_20_PERCENT
BIAS_SIGNBIAS_SIGN_NEGATIVEBIAS_SIGN_NEGATIVE
BIASBIAS_0_PERCENTBIAS_0_PERCENT
FET_SHORTFET_SHORT_DISABLEDFET_SHORT_DISABLED
OP_MODEOP_MODE_3LEAD_AMP_CELLOP_MODE_3LEAD_AMP_CELL

Before you upload it, set nanoAmperesPerPPM to 70.0F and TIA_GAIN_IN_KOHMS to TIA_GAIN_14_KOHM in SENSOR_CUSTOM, so the configuration matches this board's cell. The other fields already have the SENSOR_CO values.

What it needs on this board​

The example creates the sensor at the constructor's default address, 0x49. At an address outside 0x30 to 0x37 the library does not go through the board's bridge: begin() starts the ADS1115 at that address and writes the LMP91000's configuration itself, over the controller's I2C. On this board those two chips sit on the internal bus, which the top header brings out as SDA2 and SCL2. 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. The example passes no config pin, and LMPEN is pulled to GND on the board; see Hardware Details.

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

The bridge's address, 0x30 to 0x37, is the one the Read the CO concentration example passes.

The example code​

Select Board
customConfig.ino
/**
**************************************************
*
* @file customConfig.ino
* @brief Create a custom config for the LMP91000 and upload it to the device.
* This enables you to use this breakout with any electrochemical sensor.
*
* To successfully run the sketch:
* - Edit the custom configuration
* - Connect LMPEN to any GPIO pin
* - Run the sketch and open serial monitor at 115200 baud!
*
* @authors Robert @ soldered.com
***************************************************/

// Include the required library
#include "Electrochemical-Gas-Sensor-SOLDERED.h"

/**
* Here are parameters to configure for the custom sensor:
*
* nanoAmperesPerPPM: From the datasheet of the particular electrochemical
* sensor, used in the calculation of PPM/PPB
*
* internalZeroCalibration: A fixed value which is summed to the voltage
* measured by the ADS. The value here should make the reading be
* just barely 0 PPM when the sensor is in a place with none of the target gas
*
* adsGain: The gain of the ADS
* The voltage here represents the max range which it can measure
* ADS_GAIN_6_144V 6.144 V
* ADS_GAIN_4_096V 4.096 V
* ADS_GAIN_2_048V 2.048 V
* ADS_GAIN_1_024V 1.024 V
* ADS_GAIN_0_512V 0.512 V
* ADS_GAIN_0_256V 0.256 V
*
* TIA_GAIN_IN_KOHMS: The gain of the amplifier
* This increases the sensitivity of the measurement
* TIA_GAIN_EXTERNAL External resistance
* TIA_GAIN_2_75_KOHM 2.75 kOhm
* TIA_GAIN_3_5_KOHM 3.5 kOhm
* TIA_GAIN_7_KOHM 7 kOhm
* TIA_GAIN_14_KOHM 14 kOhm
* TIA_GAIN_35_KOHM 35 kOhm
* TIA_GAIN_120_KOHM 120 kOhm
* TIA_GAIN_350_KOHM 350 kOhm
*
* RLOAD: The load resistor in the LMP91000
* Specified in the electrochemical sensor's datasheet
* RLOAD_10_OHM 10 Ohm
* RLOAD_33_OHM 33 Ohm
* RLOAD_50_OHM 50 Ohm
* RLOAD_100_OHM 100 Ohm
*
* REF_SOURCE: Source of the control voltage reference
* For our breakout, use external, there's a 2.5V external
* reference voltage
* REF_INTERNAL Internal
* REF_EXTERNAL External
*
* INTERNAL_ZERO: Internal zero selection
* If the electrochemical sensor has a positive polarity, you
* would usually want to use 20% for the most headroom in the signal
* INTERNAL_ZERO_20_PERCENT 20%
* INTERNAL_ZERO_50_PERCENT 50%
* INTERNAL_ZERO_67_PERCENT 67%
* INTERNAL_ZERO_BYPASSED Internal zero bypassed
*
* BIAS_SIGN: The polarity of the bias voltage
* BIAS_SIGN_NEGATIVE Negative
* BIAS_SIGN_POSITIVE Positive
*
* BIAS: Added bias, percentage of the reference voltage
* Most sensors don't require this, check each sensor's datasheet
* BIAS_0_PERCENT 0%
* BIAS_1_PERCENT 1%
* BIAS_2_PERCENT 2%
* BIAS_4_PERCENT 4%
* BIAS_6_PERCENT 6%
* ...
* BIAS_22_PERCENT 22%
* BIAS_24_PERCENT 24%
*
* FET_SHORT: Short the FET
* Used to perserve the electrochemical sensor
* Has to be disabled for the sensor to be operable
* FET_SHORT_DISABLED Disabled
* FET_SHORT_ENABLED Enabled
*
* OP_MODE: Mode of operation
* OP_MODE_DEEP_SLEEP Deep Sleep (default)
* OP_MODE_2LEAD_GROUND_CELL 2-lead ground referred galvanic cell
* OP_MODE_STANDBY Standby
* OP_MODE_3LEAD_AMP_CELL 3-lead amperometric cell
* OP_MODE_TEMPERATURE_TIA_OFF Temperature measurement (TIA Off)
* OP_MODE_TEMPERATURE_TIA_ON Temperature measurement (TIA On)
*
* For more details, check thge LMP91000 datasheet, chapter 7.6
*/

// Here is an example of a custom configuration:
const sensorType SENSOR_CUSTOM = {
100.0F, // nanoAmperesPerPPM
0, // internalZeroCalibration
ADS_GAIN_4_096V, // adsGain
TIA_GAIN_120_KOHM, // TIA_GAIN_IN_KOHMS
RLOAD_10_OHM, // RLOAD
REF_EXTERNAL, // REF_SOURCE
INTERNAL_ZERO_20_PERCENT, // INTERNAL_ZERO
BIAS_SIGN_NEGATIVE, // BIAS_SIGN
BIAS_0_PERCENT, // BIAS
FET_SHORT_DISABLED, // FET_SHORT
OP_MODE_3LEAD_AMP_CELL, // OP_MODE
};

// Create the sensor object with the custom type
ElectrochemicalGasSensor sensor(SENSOR_CUSTOM);

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

// Print the reading with 5 digits of precision
Serial.println("Sensor reading: ");
Serial.print(reading, 5);
Serial.println(" PPM");

// Wait a bit before reading again
delay(2500);
}

The lines below are from a run of this example on this board, with the controller wired to the 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)
Sensor initialized successfully!
Sensor reading:
0.31747 PPM
Sensor reading:
0.31796 PPM
Sensor reading:
0.30993 PPM
Sensor reading:
0.29262 PPM

Then every 2.5 seconds the sketch prints Sensor reading: on a line of its own, followed by a line with the concentration in ppm to five decimals and PPM. A result below zero is printed as 0.00000. If begin() fails, 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 the sensor object. The example passes SENSOR_CUSTOM and leaves the address and the config pin at their defaults. A sensorType holds, in order: nanoAmperesPerPPM, internalZeroCalibration, adsGain, TIA_GAIN_IN_KOHMS, RLOAD, REF_SOURCE, INTERNAL_ZERO, BIAS_SIGN, BIAS, FET_SHORT and OP_MODE.

ReturnsConstructor.

Parameters

ParameterTypeDescription
_tsensorTypeThe cell's configuration: a library preset or your own sensorType. For this board's SGX-4CO cell the library's preset is SENSOR_CO.
_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 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 configuration and the front-end settings, 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 settings.

Returnstrue if every step succeeded, false otherwise.

double getPPM()returns double

Makes one ADC conversion of the front end's output, takes off the configuration's internal zero, adds its zero calibration, and converts the cell current to ppm with its sensitivity and amplifier gain.

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

Putting it together​

Fill in a sensorType with your cell's values (for this board, the SENSOR_CO values above), pass it to the constructor, call begin() once in setup() and check its result, then read with getPPM().