Read one sensor in PPM
What this example shows
The singleSensor example creates one sensor with the carbon monoxide preset,
SENSOR_CO, starts it, 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
The sketch creates the sensor with the preset alone, so the library's default
address, 0x49, and no _configPin are used. Change SENSOR_CO to the preset for your
cell: SENSOR_NO2, SENSOR_SO2, SENSOR_O3, SENSOR_NO, SENSOR_H2S,
SENSOR_NH3 or SENSOR_CL2.
The sketch's comment says to connect LMPEN to GND or a GPIO. On this
board, LMPEN goes to the header and to the board's ATtiny404.
/**
**************************************************
*
* @file singleSensor.ino
* @brief See how to init one sensor and make a simple reading of PPM of gas.
*
* To successfully run the sketch:
* - 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"
// Configurations for each of the sensor types are in sensorConfigData.h in the library's 'src' folder
// Create the sensor object with the according type
ElectrochemicalGasSensor sensor(SENSOR_CO);
// If you are using a custom I2C address (for multiple sensors) use:
// ElectrochemicalGasSensor sensor(SENSOR_SO2, 0x4B, 5);
// For more details on this, see customAddress.ino
void setup()
{
Serial.begin(115200); // For debugging
// Init 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);
}
Serial.println("Sensor initialized successfully!");
}
void loop()
{
// Make the reading
double reading = sensor.getPPM();
// If PPB is more relevant for your sensor, you can use:
// double reading = sensor.getPPB();
// 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/singleSensor at commit c4df54b
Open the Serial Monitor at 115200 baud. The sketch prints:
Sensor initialized successfully!
Then a line every 2.5 seconds: Sensor reading: , the concentration with five
decimals, and PPM. 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 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
| Name | Type | Description |
|---|---|---|
_t | sensorType | The preset for your cell: SENSOR_CO, SENSOR_NO2, SENSOR_SO2, SENSOR_O3, SENSOR_NO, SENSOR_H2S, SENSOR_NH3, SENSOR_CL2, or your own sensorType. |
_adcAddr | uint8_t | The 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. |
_configPin | int | A 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 boolStarts 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.
double getPPM()returns doubleTakes 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.
double getPPB()returns doubleThe same reading as getPPM(), in parts per billion.
ReturnsThe gas concentration in PPB, getPPM() x 1000.
Putting it together
Create one ElectrochemicalGasSensor with the preset for your cell, call
begin() once in setup() and stop if it returns false, then call
getPPM() or getPPB() for each reading.