Read one sensor
What this example shows
The singleSensor example creates one sensor with the SENSOR_CO preset, the
library's preset for this board's SGX-4CO cell, and prints the carbon monoxide
concentration in ppm every 2.5 seconds. It leaves the address and the config
pin at the constructor's defaults.
What it needs on this board
The constructor's default address is 0x49. At an address outside 0x30 to 0x37
the library does not go through the board's bridge: begin() starts the
ADS1115 at that address and writes the LMP91000's configuration itself, over
the controller's I2C. On this board those two chips sit on the internal bus,
which the top header brings out as SDA2 and SCL2. The Qwiic connectors and
SDA1/SCL1 reach only the ATtiny404 bridge, and an I2C scan over Qwiic finds
only the bridge, at 0x30 as shipped. The example passes no config pin, and
LMPEN is pulled to GND on the board; see
Hardware Details.
So wire the top header for this example, not the Qwiic cable: connect your
controller's SDA to SDA2, its SCL to SCL2, its 3.3 V pin to 3V3 and its
GND to GND. Over Qwiic only the bridge answers.
The bridge's address, 0x30 to 0x37, is the one the Read the CO concentration example passes.
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 lines below are from a run of this example on this board, with the
controller wired to the header's SDA2, SCL2, 3V3 and GND pins as
described above.
Expected result in the Serial Monitor at 115200 baud:
Sensor initialized successfully!
Sensor reading: 0.32536 PPM
Sensor reading: 0.31509 PPM
Sensor reading: 0.32570 PPM
Sensor reading: 0.29218 PPM
Then a line every 2.5 seconds: Sensor reading: , the concentration in ppm
with five decimals, and PPM. A result below zero is printed as 0.00000.
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. The example passes SENSOR_CO and leaves the address and the config pin at their defaults.
ReturnsConstructor.
Parameters
| Parameter | Type | Description |
|---|---|---|
_t | sensorType | The cell's preset. For this board's SGX-4CO cell use SENSOR_CO (70 nA/ppm, ADC range 4.096 V, 14 kΩ amplifier gain, 20% internal zero). |
_adcAddr | uint8_t | The I2C address. Default DEFAULT_ADC_ADDR, 0x49. From 0x30 to 0x37 the library talks to the board's ATtiny404 bridge. |
_configPin | int | A GPIO the library drives low while it sends the front end's configuration and high otherwise. Not used at a 0x30 to 0x37 address. Default -1, none. |
bool begin()returns boolStarts I2C with Wire.begin(). At an address from 0x30 to 0x37 it checks that the bridge answers, sends it the ADC gain and data rate from the preset and the front end's configuration, waiting up to 500 ms for each reply. At any other address it starts the ADC at that address, sets its gain and its slowest data rate, then writes the front end's configuration.
Returnstrue if every step succeeded, false otherwise.
double getPPM()returns doubleMakes one ADC conversion of the front end's output, takes off the internal zero (20% of the 2.5 V reference for SENSOR_CO), adds the zero calibration, and converts the cell current to ppm with the preset's sensitivity.
ReturnsThe gas concentration in ppm. A negative result is returned as 0.
double getPPB()returns doubleThe same measurement as getPPM(), in parts per billion. The example shows it as a commented-out alternative.
ReturnsgetPPM() multiplied by 1000.
Putting it together
Create the object with SENSOR_CO, call begin() once in setup() and check
its result, then call getPPM() (or getPPB()) whenever you want a reading.