Single sensor
What this example shows
The library's singleSensor example creates one sensor with only a preset, so the address and the pin keep their defaults, and prints the concentration in ppm every 2.5 s.
The example creates the sensor with SENSOR_CO, the carbon monoxide preset.
The library calculates ppm from the preset's sensitivity and gain settings; the
preset for this board's chlorine cell is SENSOR_CL2.
The example code
/**
**************************************************
*
* @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);
}
The header comment asks for LMPEN to be connected. On this board a 10 kΩ
resistor holds LMPEN low and it is wired to the ATtiny404, so you leave it
unconnected (see the pin table on
Hardware details).
Expected result:
Sensor initialized successfully!
Then a line every 2.5 s: Sensor reading: , the concentration in ppm 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 ElectrochemicalGasSensorCreates the sensor object. It stores the settings only; nothing is sent to the board until begin().
ReturnsThe sensor object.
Parameters
| Type | Name | Description |
|---|---|---|
sensorType | _t | The cell preset. The example passes SENSOR_CO; this board's SGX-4CL2 cell has the preset SENSOR_CL2. |
uint8_t | _adcAddr | The I2C address, DEFAULT_ADC_ADDR (0x49) when left out, as in the example. For an address from 0x30 to 0x37, the range of this board's ADDRESS switch, begin() talks to the board's ATtiny404; for any other address it looks for an ADS1115 at that address and the LMP91000 directly on the bus. |
int | _configPin | A GPIO that begin() drives low while it configures the LMP91000 directly. -1 (none) when left out, as in the example. Unused for addresses 0x30 to 0x37. |
bool begin()returns boolStarts Wire, checks that the board answers, sets the preset's ADC gain and data rate, and configures the LMP91000 with the preset's settings. Call it once in setup() before reading.
Returnstrue when the board answered and both configuration steps succeeded, false otherwise.
double getPPM()returns doubleTakes one reading and converts it to ppm: it subtracts the internal zero voltage (the 2.5 V reference times the preset's internal zero percentage), adds the zero calibration, divides by the amplifier gain and by the preset's sensitivity in nA per ppm. A result below zero is returned as 0.
ReturnsThe gas concentration in ppm.
double getPPB()returns doubleSame reading as getPPM(), times 1000. The example shows it in a comment as the alternative to getPPM().
ReturnsThe gas concentration in ppb.
The example also uses Serial and delay(), which come with the Arduino core,
not with this library.
Putting it together
begin() does all the setup, so loop() only calls getPPM() and prints the
result. To read in ppb instead, swap the getPPM() line for the getPPB() line
in the comment below it.