Use your own front-end settings
What this example shows
The customConfig example builds its own preset, a sensorType named
SENSOR_CUSTOM, instead of using one of the library's presets. A preset holds
the cell's sensitivity, the ADC range and every LMP91000 front-end setting, so
this is the way to try other values for a cell. The sketch then prints a
reading in ppm every 2.5 seconds.
The example creates the sensor with only the preset, so the library uses its
default address, DEFAULT_ADC_ADDR (0x49). At that address the library talks
to the ADS1115 and the LMP91000 directly, not through the board's ATtiny404
bridge, and the sketch's header asks for LMPEN connected to a GPIO. See the
library overview
for how the address decides this.
For this board's SGX-4NO-250 cell, the library's own SENSOR_NO preset uses
the values in the table below. The example's SENSOR_CUSTOM differs from it
in the sensitivity (100 nA per ppm) and the bias (negative, 0%).
| Field | SENSOR_NO value |
|---|---|
nanoAmperesPerPPM | 400.0F |
internalZeroCalibration | 0 |
adsGain | ADS_GAIN_4_096V |
TIA_GAIN_IN_KOHMS | TIA_GAIN_120_KOHM |
RLOAD | RLOAD_10_OHM |
REF_SOURCE | REF_EXTERNAL |
INTERNAL_ZERO | INTERNAL_ZERO_20_PERCENT |
BIAS_SIGN | BIAS_SIGN_POSITIVE |
BIAS | BIAS_12_PERCENT |
FET_SHORT | FET_SHORT_DISABLED |
OP_MODE | OP_MODE_3LEAD_AMP_CELL |
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 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);
}
Expected result, with SENSOR_CUSTOM set to the SENSOR_NO values (readings
in clean air; yours will differ):
Sensor initialized successfully!
Sensor reading:
0.00610 PPM
Sensor reading:
0.00601 PPM
Sensor reading:
0.00600 PPM
Sensor reading:
0.00614 PPM
Every 2.5 seconds comes a line Sensor reading: and, on the next
line, the concentration in ppm to five decimals with PPM at the end: the
sketch prints the label with Serial.println(), so the value starts a new
line. If begin() fails, the sketch prints
ERROR: Can't init the sensor! Check connections! and stops there.
Functions used
sensorTypereturns sensorTypeThe struct that holds one preset. Its fields, in order: nanoAmperesPerPPM, internalZeroCalibration, adsGain, TIA_GAIN_IN_KOHMS, RLOAD, REF_SOURCE, INTERNAL_ZERO, BIAS_SIGN, BIAS, FET_SHORT and OP_MODE. The library's presets, such as SENSOR_NO, are sensorType values in sensorConfigData.h. begin() sends the front-end fields to the LMP91000 and the gain to the ADC; getPPM() uses the sensitivity and the zero calibration.
ReturnsThe preset you pass to ElectrochemicalGasSensor.
ElectrochemicalGasSensor(sensorType _t, uint8_t _adcAddr = DEFAULT_ADC_ADDR, int _configPin = -1)returns ElectrochemicalGasSensorCreates the sensor object for one board. Nothing is sent over I2C until you call begin().
ReturnsThe sensor object.
Parameters
| Type | Name | Description |
|---|---|---|
sensorType | _t | The preset, SENSOR_CUSTOM in the example. |
uint8_t | _adcAddr | The I2C address. Left out in the example, so it is DEFAULT_ADC_ADDR, 0x49. For an address from 0x30 to 0x37 the library talks to the 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 LMPEN. Left out in the example (-1), so the library switches no pin. |
sensor.begin()returns boolStarts I2C with Wire.begin(). 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 when one is given.
Returnstrue when every step succeeded, false otherwise.
sensor.getPPM()returns doubleTakes one ADC reading 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.
Putting it together
The preset is plain data: define it once, pass it to the constructor, and
begin() sends its front-end settings when it starts the sensor. To try other
values, edit the fields of SENSOR_CUSTOM and upload the sketch again.