Calibrate the zero point
What this example shows
The library's calibrateSensor example adds your own offset to the voltage the
sensor measures, then prints the gas concentration in ppm every 2.5 seconds.
customCalibration at the top is that offset, in volts; the example sets it to
0.05. As the example's comment says, in air with none of the target gas the
voltage after calibration should be roughly 0, so adjust customCalibration
until it is.
To see that voltage, uncomment #define ELECTROCHEMICAL_SENSOR_DEBUG in the
library's Electrochemical-Gas-Sensor-SOLDERED.h, as the example's header asks.
Each getPPM() call then also prints the raw voltage, the voltage without the
reference value and the voltage after calibration.
The example creates the sensor with the ammonia preset, SENSOR_NH3, at
address 0x49 with LMPEN on GPIO 32. SENSOR_NO2 is the library's preset for
this board's SGX-4NO2 cell; change it in the line that creates the sensor.
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 every 2.5 seconds the sketch prints a line that starts with
Sensor reading:, followed by the concentration in ppm with five decimals.
With the debug #define uncommented, each reading is preceded by the
lines Electrochemical gas sensor readings:, Raw voltage measurement:,
Voltage without reference value: and Voltage after calibration:, each
voltage in volts with ten decimals. 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 ElectrochemicalGasSensorCreates the sensor object. Nothing is sent to the board until you call begin(). The address decides how begin() talks to the board: from 0x30 to 0x37 it sends its commands to the ATtiny404, and from 0x48 to 0x4B (DEFAULT_ADC_ADDR is 0x49) it addresses the ADS1115 at that address and the LMP91000 directly.
ReturnsThe sensor object.
Parameters
| Type | Name | Description |
|---|---|---|
sensorType | _t | The cell's preset. SENSOR_NO2 is the preset for this board's SGX-4NO2 cell; the example passes SENSOR_NH3. |
uint8_t | _adcAddr | The address begin() talks to, 0x49 by default; the example passes 0x49. |
int | _configPin | GPIO for the LMP91000 enable pin (LMPEN), used only at 0x48 to 0x4B. -1 (none) by default; the example passes 32. |
sensor.begin()returns boolStarts Wire and writes the preset to the board. From 0x30 to 0x37 it checks that the ATtiny404 answers, then sends it the ADC gain and data rate and the LMP91000 configuration, each step waiting up to 500 ms for the answer. From 0x48 to 0x4B it starts the ADS1115 at that address, sets the preset's gain and the slowest data rate, and writes the LMP91000 configuration, pulling _configPin low while it does when one is given. Call it once in setup().
Returnstrue when every step succeeded, false otherwise.
sensor.setCustomZeroCalibration(double calibration)returns voidSets the calibration offset that getPPM() adds to the measured voltage before it turns it into a concentration. It replaces the internalZeroCalibration value of the sensor's copy of the preset.
ReturnsNothing.
Parameters
| Type | Name | Description |
|---|---|---|
double | calibration | The offset in volts. The example passes customCalibration (0.05). |
sensor.getPPM()returns doubleTakes a new ADC reading and turns it into a gas concentration: the voltage minus the internal zero, plus the calibration offset, divided by the TIA gain and by the cell's sensitivity in the preset. A negative result is returned as 0. With ELECTROCHEMICAL_SENSOR_DEBUG defined, it also prints the voltages it works with.
ReturnsThe gas concentration in ppm.
Putting it together
The sketch calls begin() and stops in a loop if that fails, then sets the
offset with setCustomZeroCalibration() before the first reading, so every
value it prints already includes it. In loop(), each getPPM() call reads
the cell again, and the sketch prints the value with five decimals and waits
2.5 seconds.