Skip to main content

Calibrate the zero point

What this example shows​

The calibrateSensor example adds a zero-point offset to the sensor's readings and prints the gas concentration in parts per million (PPM) every 2.5 seconds.

Wire your controller to the top header for this example, not to a Qwiic connector: the controller's SDA to SDA2, its SCL to SCL2, its 3.3 V supply to 3V3 and its ground to GND. At this address the library talks to the ADS1115 and the LMP91000 directly on SDA2 and SCL2; the board's ATtiny404 bridge at 0x30 is not used. Over Qwiic, only the bridge example in the Quick Start works.

The example code​

customCalibration is a voltage the library adds to each ADC reading. As the sketch's comment says, in an environment with none of the target gas, adjust it until the voltage after calibration is roughly 0. The sketch uses the ammonia preset, SENSOR_NH3, and creates the sensor at address 0x49 with GPIO 32 as _configPin.

To see the voltages while you adjust it, uncomment #define ELECTROCHEMICAL_SENSOR_DEBUG near the top of the library's Electrochemical-Gas-Sensor-SOLDERED.h, as the sketch's header asks. Each getPPM() call then also prints the raw voltage measurement, the voltage without the reference value and the voltage after calibration.

Wire the board's LMPEN pin on the header to GPIO 32, the sketch's _configPin. On this board, LMPEN also goes to the board's ATtiny404.

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

🔗Open the complete example

examples/calibrateSensor at commit c4df54b

→

Open the Serial Monitor at 115200 baud. The sketch prints:

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

Then a line every 2.5 seconds: Sensor reading: , the concentration with five decimals, and PPM. With the debug define uncommented, each reading is preceded by Electrochemical gas sensor readings: and the three voltage lines (Raw voltage measurement: , Voltage without reference value: , Voltage after calibration: , each 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 none

Creates the sensor object for one board. The address decides how begin() talks to it: for an address from 0x30 to 0x37 it uses the protocol of the ATtiny404 bridge; for any other address it talks to an ADS1115 at that address and to the LMP91000 itself. The default is DEFAULT_ADC_ADDR (0x49).

ReturnsConstructor.

Parameters

NameTypeDescription
_tsensorTypeThe preset for your cell: SENSOR_CO, SENSOR_NO2, SENSOR_SO2, SENSOR_O3, SENSOR_NO, SENSOR_H2S, SENSOR_NH3, SENSOR_CL2, or your own sensorType.
_adcAddruint8_tThe address begin() uses. 0x30 to 0x37 selects the bridge protocol; this board answers at 0x30 out of the box, or at the address its switch sets. Any other value is taken as an ADS1115 address. 0x49 by default.
_configPinintA GPIO for the LMP91000 enable line. The library holds it high and pulls it low only while it writes the LMP91000's registers itself; for an address from 0x30 to 0x37 it is not used. -1 (none) by default.
bool begin()returns bool

Starts I2C with Wire.begin() and picks the protocol from the address. For 0x30 to 0x37 it checks that the bridge answers, sends the ADC gain from the preset and has the bridge write the LMP91000's registers, each step giving up after 500 ms. For any other address it checks that an ADS1115 answers at that address, sets its gain, and writes the LMP91000's registers when the LMP91000 reports ready, pulling _configPin low around the writes when one is given. Call it once before reading.

Returnstrue when every step succeeded, false otherwise.

void setCustomZeroCalibration(double calibration)returns void

Sets the zero-point offset, in volts, that getPPM() adds to the voltage after removing the internal zero. It changes this sensor object's copy of the preset.

ReturnsNothing.

Parameters

NameTypeDescription
calibrationdoubleThe offset in volts.
double getPPM()returns double

Takes one reading of the ADS1115's channel 0 (through the bridge for 0x30 to 0x37) and converts it to a concentration with the preset: it removes the internal zero offset, adds the zero calibration, divides by the amplifier gain and by the cell's sensitivity in nA per PPM. A negative result is returned as 0.

ReturnsThe gas concentration in PPM.

Putting it together​

Create the sensor and call begin(). Then call setCustomZeroCalibration() with your offset before you read. In air with none of the target gas, adjust the offset until the voltage after calibration is roughly 0. getPPM() returns 0 for any negative result, so the debug voltages show more than the reading does while you adjust it.