Skip to main content

Zero-point calibration

What this example shows​

The calibrateSensor example adds a fixed voltage offset, customCalibration (0.05), to every reading, then prints readings in PPM every 2.5 seconds. It creates the sensor with the SENSOR_NH3 configuration, address 0x49 and pin 32 for LMPEN.

You do not need to wire LMPEN on this board, whatever the sketch's comments say: the board holds it low itself.

The example's own comment says how to use the offset: with none of the target gas present, adjust customCalibration until the reading after calibration is roughly 0.

To see the voltages behind each reading, uncomment //#define ELECTROCHEMICAL_SENSOR_DEBUG in the library's Electrochemical-Gas-Sensor-SOLDERED.h. getPPM() then also prints the raw voltage, the voltage without the reference value and the voltage after calibration.

The example code​

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);
}

What this board prints with the sketch as written has not been measured yet. When this board was tested over Qwiic, an I2C scan found only one address, 0x30, and with the MicroPython library begin() failed for a sensor created at 0x49, the address this sketch passes. If begin() fails, the sketch prints ERROR: Can't init the sensor! Check connections! and stops.

When begin() succeeds, the sketch prints:

Serial MonitorAutoscrollShow timestampNewline115200 baud
Message (Enter to send message to the board)
Sensor initialized successfully!

After it, a line Sensor reading: followed by a value with five decimals and PPM arrives every 2.5 seconds.

Functions used​

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

Creates the sensor object with a sensor configuration, an address and an LMPEN pin. Nothing is sent until you call begin(). begin() picks how to talk to the board from the address: from 0x30 to 0x37 it uses the ATtiny404 bridge's commands and ignores the third argument; any other address is taken as the ADS1115's own address, and the LMP91000 is written directly.

ReturnsThe sensor object.

Parameters

TypeNameDescription
sensorType_tThe front end and conversion settings, SENSOR_NH3 in the example. The presets live in the library's src/sensorConfigData.h.
uint8_t_adcAddrThe address, 0x49 in the example.
int_configPinThe GPIO pin connected to LMPEN, 32 in the example. Not used for an address from 0x30 to 0x37.
sensor.begin()returns bool

Starts I2C with Wire.begin(). For an address from 0x30 to 0x37 it checks that the bridge answers, sends it the ADC gain and the slowest ADC data rate, then the LMP91000 settings; each bridge command waits up to 500 ms. For any other address it starts the ADS1115 at that address with the same gain and data rate, sets the LMPEN pin high if one was given, then writes the LMP91000 settings, holding that pin low while it does.

Returnstrue when every step succeeded, false otherwise.

sensor.setCustomZeroCalibration(double calibration)returns void

Sets the zero-point offset, in volts, that getPPM() adds to the measured voltage. It replaces internalZeroCalibration in the sensor object's copy of the configuration.

ReturnsNothing.

Parameters

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

Takes one ADC reading and turns the voltage into a concentration: it subtracts the internal zero (a share of the 2.5 V reference), adds the zero-point offset, divides by the TIA gain to get the cell current, then divides by the configuration's nanoamperes per PPM. A negative result is returned as 0. With ELECTROCHEMICAL_SENSOR_DEBUG defined it also prints the three voltages.

ReturnsThe concentration in PPM.

Putting it together​

SENSOR_NH3 is the preset for an SGX-4NH3-300 ammonia cell. The library has no preset for this board's SGX-4OX-ROHS oxygen cell yet, so the value the example prints is calculated with the ammonia (NH3) preset's settings. The custom configuration page shows how to write your own; its internalZeroCalibration field sets the same offset when the configuration is created.