Skip to main content

Calibrate the zero point

What this example shows​

The calibrateSensor example adds a voltage offset to every reading with setCustomZeroCalibration(). The library reports any negative result as 0, so in air with none of the target gas you adjust customCalibration until the reading is roughly 0, as the sketch's comment describes. It then prints a reading in ppm every 2.5 seconds.

The example creates the sensor at address 0x49 with LMPEN on GPIO 32. At that address the library talks to the ADS1115 and the LMP91000 directly, not through the board's ATtiny404 bridge. See the library overview for how the address decides this.

The sketch's header asks you to uncomment the #define in the library header file. That line is //#define ELECTROCHEMICAL_SENSOR_DEBUG in Electrochemical-Gas-Sensor-SOLDERED.h. With it defined, every getPPM() also prints the raw voltage, the voltage without the reference value and the voltage after calibration, which helps you pick the offset.

The example passes SENSOR_NH3, the library's preset for an ammonia cell. For a nitric oxide reading, use SENSOR_NO, the preset for this board's SGX-4NO-250 cell: 400 nA per ppm, a 120 kΩ amplifier gain and a positive 12% bias.

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.

Select Board
calibrateSensor.ino
/**
**************************************************
*
* @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, with SENSOR_NO (readings in air with no NO; yours will differ). The customCalibration value these readings were taken with is not recorded, so they may differ from what the listing's 0.05 gives: they show the shape of the output, and your numbers depend on the offset you set.

Serial MonitorAutoscrollShow timestampNewline115200 baud
Message (Enter to send message to the board)
Sensor initialized successfully!
Sensor reading: 0.00588 PPM
Sensor reading: 0.00602 PPM
Sensor reading: 0.00608 PPM
Sensor reading: 0.00581 PPM

A Sensor reading: line comes every 2.5 seconds, with the concentration in ppm to five decimals and PPM at the end. If begin() fails, the sketch prints ERROR: Can't init the sensor! Check connections! and stops there.

Functions used​

ElectrochemicalGasSensor(sensorType _t, uint8_t _adcAddr = DEFAULT_ADC_ADDR, int _configPin = -1)returns ElectrochemicalGasSensor

Creates the sensor object for one board. Nothing is sent over I2C until you call begin().

ReturnsThe sensor object.

Parameters

TypeNameDescription
sensorType_tThe cell preset, SENSOR_NH3 in the example. SENSOR_NO is the one for this board's cell.
uint8_t_adcAddrThe I2C address, 0x49 in the example. 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_configPinThe GPIO wired to LMPEN, 32 in the example. The library holds it low only while it configures the LMP91000. Ignored at addresses 0x30 to 0x37.
sensor.begin()returns bool

Starts 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.setCustomZeroCalibration(double calibration)returns void

Sets the zero calibration of this sensor's preset: a voltage that getPPM() adds to every reading before it converts it.

ReturnsNothing.

Parameters

TypeNameDescription
doublecalibrationThe offset in volts, customCalibration (0.05) in the example.
sensor.getPPM()returns double

Takes 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. 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​

begin() configures the front end once, then setCustomZeroCalibration() sets the offset before the first reading. Change customCalibration, upload the sketch again in air with none of the target gas, and repeat until the readings sit just at 0.