Zero calibration
What this example shows
The calibrateSensor example applies a zero-point calibration offset with
setCustomZeroCalibration() and then prints the gas concentration in ppm every
2.5 seconds.
The offset is customCalibration at the top of the sketch, 0.05 in the
example. The example's comment explains how to use it: the value is added to
the voltage read from the ADC, and in a place with none of the target gas you
adjust it until the voltage after calibration is roughly 0.
Before you upload it, change SENSOR_NH3 to SENSOR_CO in the line that
creates the sensor. SENSOR_NH3 is the library's preset for the ammonia
cell. This board carries the SGX-4CO carbon monoxide cell, and the library's
preset for it is SENSOR_CO. The offset is added before the library
converts with the preset's sensitivity and zero, so tune it with SENSOR_CO
in place.
To see the voltages while you tune, uncomment
#define ELECTROCHEMICAL_SENSOR_DEBUG in the library's
Electrochemical-Gas-Sensor-SOLDERED.h, as the example's header says. Each
getPPM() then also prints the raw voltage measurement, the voltage without
the reference value and the voltage after calibration.
What it needs on this board
The example creates the sensor at 0x49 with GPIO 32 as the config pin. 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.
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 library drives GPIO 32 low while it writes the front end's configuration
and high otherwise, and the example's header asks for LMPEN to be connected
to GND or a GPIO pin. On the board, LMPEN is on the bottom header, pulled
to GND through 10 kΩ and also wired to the ATtiny404; see
Hardware Details.
The bridge's address, 0x30 to 0x37, is the one the Read the CO concentration example passes.
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);
}
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:
Sensor initialized successfully!
Sensor reading: 0.31243 PPM
Sensor reading: 0.31667 PPM
Sensor reading: 0.30131 PPM
Sensor reading: 0.29969 PPM
Then a line every 2.5 seconds: Sensor reading: , the concentration in ppm
with 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 noneCreates the sensor object. The example passes a preset, the address 0x49 and GPIO 32 as the config pin.
ReturnsConstructor.
Parameters
| Parameter | Type | Description |
|---|---|---|
_t | sensorType | The cell's preset. For this board's SGX-4CO cell use SENSOR_CO (70 nA/ppm, ADC range 4.096 V, 14 kΩ amplifier gain, 20% internal zero). |
_adcAddr | uint8_t | The I2C address. Default DEFAULT_ADC_ADDR, 0x49. From 0x30 to 0x37 the library talks to the board's ATtiny404 bridge. |
_configPin | int | A 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 boolStarts 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 preset and the front end's configuration, 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's configuration with the config pin held low.
Returnstrue if every step succeeded, false otherwise.
void setCustomZeroCalibration(double calibration)returns voidSets the zero-calibration value, which the library's header describes as the voltage level at 0 ppm. getPPM() adds it, in volts, to the measured voltage after taking off the internal zero and before converting to ppm.
ReturnsNothing.
Parameters
| Parameter | Type | Description |
|---|---|---|
calibration | double | The offset in volts. The example passes customCalibration, 0.05. |
double getPPM()returns doubleMakes one ADC conversion of the front end's output, takes off the internal zero, adds the zero calibration, and converts the cell current to ppm with the preset's sensitivity. With ELECTROCHEMICAL_SENSOR_DEBUG defined it also prints the three voltages.
ReturnsThe gas concentration in ppm. A negative result is returned as 0.
Putting it together
Create the object with SENSOR_CO, call begin() once in setup() and check
its result, then call setCustomZeroCalibration() with your offset before you
start reading. In a place with none of the target gas, change
customCalibration and upload again until the voltage after calibration is
roughly 0, as the example's comment says.