Read the NO2 concentration
What this example shows
The library's bridgeBoard example creates the sensor at the board's I2C
address, starts it, and prints the gas concentration in ppm every 2.5 seconds.
An address from 0x30 to 0x37 tells the library that it is talking to the
board's ATtiny404, which runs the LMP91000 and the ADS1115 for you.
The example creates the sensor with the carbon monoxide preset, SENSOR_CO.
Change SENSOR_CO to SENSOR_NO2 before you upload it: SENSOR_NO2 is
the library's preset for the SGX-4NO2 cell on this board. If you moved the
ADDRESS switches, change 0x30 to the address they set.
The example code
/**
**************************************************
*
* @file bridgeBoard.ino
* @brief See how to init a sensor on the new ATtiny404 bridge board revision,
*
* To successfully run the sketch:
* - Set the desired address using the jumpers on the back of the board
* (address = 0x30 + jumper offset, giving a range of 0x30-0x37)
* - Run the sketch and open serial monitor at 115200 baud!
*
* Note: on this board revision, LMPEN is hardwired to GND, so there's
* no configPin argument to pass - the constructor's third argument
* is ignored whenever the address falls in the bridge range (0x30-0x37).
*
*
* @authors @ soldered.com
***************************************************/
// Include the required library
#include "Electrochemical-Gas-Sensor-SOLDERED.h"
// Any additional boards on the same bus just need a different jumper setting
// (0x30-0x37), same as twoSensors.ino does for legacy boards with 0x49/0x4A/0x4B.
ElectrochemicalGasSensor sensor(SENSOR_CO, 0x30);
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 and jumper address!");
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 comment at the top of the example says LMPEN is hardwired to GND. On this
board LMPEN is the LMP91000's enable input and is also wired to the ATtiny404,
as Hardware Details
shows. Either way you connect nothing to it: at addresses 0x30 to 0x37 the
library does not use the constructor's third argument.
Expected result:
Sensor initialized successfully!
Sensor reading: 0.01453 PPM
Sensor reading: 0.01357 PPM
Sensor reading: 0.01379 PPM
Sensor reading: 0.01435 PPM
A new Sensor reading: line follows every 2.5 seconds, with the NO2
concentration in ppm to five decimals. If the board does not answer, it prints
ERROR: Can't init the sensor! Check connections and jumper address! and stops.
Functions used
ElectrochemicalGasSensor(sensorType _t, uint8_t _adcAddr = DEFAULT_ADC_ADDR, int _configPin = -1)returns ElectrochemicalGasSensorCreates the sensor object. Nothing is sent to the board until you call begin(). The address decides how begin() talks to the board: from 0x30 to 0x37 it sends its commands to the ATtiny404, and from 0x48 to 0x4B (DEFAULT_ADC_ADDR is 0x49) it addresses the ADS1115 at that address and the LMP91000 directly.
ReturnsThe sensor object.
Parameters
| Type | Name | Description |
|---|---|---|
sensorType | _t | The cell's preset. Use SENSOR_NO2 for this board's SGX-4NO2 cell. |
uint8_t | _adcAddr | The board's I2C address. Use 0x30 to 0x37, as set by the ADDRESS switches; 0x30 as shipped. |
int | _configPin | GPIO for the LMP91000 enable pin. Not used at addresses 0x30 to 0x37; leave it out. |
sensor.begin()returns boolStarts Wire, checks that the board answers, then sends it the ADC gain and data rate and the LMP91000 configuration from the preset. Each step waits up to 500 ms for the board's answer. Call it once in setup().
Returnstrue when every step succeeded, false otherwise.
sensor.getPPM()returns doubleAsks the board for a new ADC reading and turns it into a gas concentration: the voltage minus the internal zero, plus the calibration offset, divided by the TIA gain and by the cell's sensitivity in the preset. A negative result is returned as 0.
ReturnsThe gas concentration in ppm.
sensor.getPPB()returns doubleThe same reading as getPPM(), multiplied by 1000. The example mentions it in a comment as the line to use when parts per billion suit your sensor better.
ReturnsThe gas concentration in ppb.
Putting it together
begin() runs once in setup(). If it fails, the sketch prints the error and
stays in an endless loop, so nothing else runs until you fix the connection or
the address and reset the board. In loop(), each getPPM() call reads the
cell again, and the sketch prints the value with five decimals.