Calibrate the zero point
What this example shows
The library's calibrateSensor example adds a fixed voltage offset to every ADC reading before it is turned into ppm. In air with none of the target gas, adjust that offset until the reading is just barely 0.
The example creates the sensor with the NH3 cell preset, SENSOR_NH3, at address 0x49 with GPIO 32 as its enable pin. SENSOR_SO2 is the library's preset for the SGX-4SO2 cell on this board: change SENSOR_NH3 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's 0x49 talks to an ADS1115 directly. Pass the board's address as the second argument of the constructor instead, 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, here GPIO 32 (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 calibrateSensor.ino
* @brief See how to calibrate an electrochemical sensor
*
* To successfully run the sketch:
* - Uncomment the #define in the library header file
* - 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"
// This is the custom value we set for the calibration voltage
// This value is added to the voltage read from the ADC
// In an environment with 0 of the target gas, the voltage after calibration should be
// roughly 0, adjust customCalibration to get it to that value.
const double customCalibration = 0.05;
// Create the sensor object with the according type
ElectrochemicalGasSensor sensor(SENSOR_NH3, 0x49, 32);
void setup()
{
Serial.begin(115200); // For debugging
// Initialize 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);
}
//Set the custom calibration value
sensor.setCustomZeroCalibration(customCalibration);
Serial.println("Sensor initialized successfully!");
}
void loop()
{
// Make the reading
double reading = sensor.getPPM();
// This function will also print debug messages if the #define in
// the header file of the library is uncommented
// It will print:
// - The raw voltage measurement
// - Voltage without reference value
// - Voltage after calibration
// These values can help you adjust the internal zero calibration
// 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!
Then a Sensor reading: line in ppm follows every 2.5 seconds. To see the voltages behind each reading, remove the // in front of #define ELECTROCHEMICAL_SENSOR_DEBUG in the library's Electrochemical-Gas-Sensor-SOLDERED.h. Each reading then also prints the raw voltage, the voltage without the reference value and the voltage after calibration.
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 address 0x49 and GPIO 32.
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.
Returnstrue when the board answered and took every setting, false otherwise.
void setCustomZeroCalibration(double calibration)returns voidSets the voltage offset that is added to every reading before it is turned into ppm. It replaces the preset's own zero calibration value, which is 0 for SENSOR_SO2.
ReturnsNothing.
Parameters
| Parameter | Type | Description |
|---|---|---|
calibration | double | The offset in volts. |
double getPPM()returns doubleHas the board take one ADC reading and turns it into a gas concentration, including the zero calibration offset. A result below zero is returned as 0.
ReturnsGas concentration in ppm.
Putting it together
The example starts at 0.05. Put the board in air without SO2, watch the debug voltages, and set customCalibration so the voltage after calibration is roughly 0. Use the same value with setCustomZeroCalibration() in your own sketch.