Read one sensor at the default address
What this example shows
The singleSensor example starts one sensor and prints a reading in ppm
every 2.5 seconds. It creates the sensor with only the cell preset, so the
library uses its default address, DEFAULT_ADC_ADDR (0x49). At that address
the library talks to the ADS1115 and the LMP91000 directly, not through the
board's ATtiny404 bridge, and the sketch's header asks for LMPEN connected to
GND or a GPIO. See the
library overview
for how the address decides this.
The example passes SENSOR_CO, the library's preset for a carbon monoxide
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.
/**
**************************************************
*
* @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);
}
Expected result, with SENSOR_NO (readings in clean air; yours will differ):
Sensor initialized successfully!
Sensor reading: 0.00632 PPM
Sensor reading: 0.00593 PPM
Sensor reading: 0.00615 PPM
Sensor reading: 0.00582 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 ElectrochemicalGasSensorCreates the sensor object for one board. Nothing is sent over I2C until you call begin().
ReturnsThe sensor object.
Parameters
| Type | Name | Description |
|---|---|---|
sensorType | _t | The cell preset, SENSOR_CO in the example. SENSOR_NO is the one for this board's cell. The library also has SENSOR_NO2, SENSOR_SO2, SENSOR_O3, SENSOR_H2S, SENSOR_NH3 and SENSOR_CL2. |
uint8_t | _adcAddr | The I2C address. Left out in the example, so it is DEFAULT_ADC_ADDR, 0x49. 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 | _configPin | The GPIO wired to LMPEN. Left out in the example (-1), so the library switches no pin. |
sensor.begin()returns boolStarts 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.getPPM()returns doubleTakes 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.
ReturnsThe gas concentration in ppm. A negative result is returned as 0.
sensor.getPPB()returns doubleThe alternative the sketch's comment offers: takes the same reading as getPPM() and multiplies it by 1000.
ReturnsThe gas concentration in ppb.
Putting it together
begin() runs once in setup(). If it returns false, the sketch prints the
error and stays in an endless loop. In loop(), each getPPM() makes one
fresh measurement and the sketch prints it with five decimals, then waits 2.5
seconds.