Skip to main content

Single sensor reading

What this example shows​

The library's singleSensor example creates one sensor, starts it, and prints the gas concentration in ppm every 2.5 seconds.

The example creates the sensor with SENSOR_CO, the library's carbon monoxide preset, and no address, so the library's default address is used. SENSOR_NH3 is the library's preset for the SGX-4NH3 cell on this board, and the ppm conversion uses the preset's sensitivity: 70 nA/ppm for SENSOR_CO, 40 nA/ppm for SENSOR_NH3. As shipped, the example's readings are therefore not ammonia ppm. To read ammonia, change SENSOR_CO to SENSOR_NH3 in the line that creates the sensor.

The library talks to the board's ATtiny404 bridge when the address is from 0x30 to 0x37, and to an ADS1115 at that address directly for any other address, such as the default 0x49 this example uses. On this board the ADS1115 sits on the header's SDA2/SCL2 pins, which the Qwiic connectors do not reach, so this example does not run over the Qwiic cable. Wire the controller to the header instead: 3.3 V to 3V3, GND to GND, SDA to SDA2 and SCL to SCL2. The example then runs as written, with no change to the address.

The example's comments connect LMPEN to GND or a GPIO pin. On this board, leave LMPEN unconnected: a 10 kΩ resistor already pulls it to GND, and the ATtiny404 is connected to it too.

The example code​

Select Board
singleSensor.ino
/**
**************************************************
*
* @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 Serial Monitor shows:

Serial MonitorAutoscrollShow timestampNewline115200 baud
Message (Enter to send message to the board)
Sensor initialized successfully!

After that, a line starting with Sensor reading: follows every 2.5 seconds, with the concentration in ppm to five decimals.

If begin() returns false, the example prints ERROR: Can't init the sensor! Check connections! instead and stops.

Functions used​

ElectrochemicalGasSensor(sensorType _t, uint8_t _adcAddr = DEFAULT_ADC_ADDR, int _configPin = -1)returns none

Creates the sensor object. begin() picks how to talk to the board from the address: from 0x30 to 0x37 it uses the ATtiny404 bridge; any other address is used as the ADS1115's own I2C address. Leaving the address out, as this example does, uses DEFAULT_ADC_ADDR, 0x49.

ReturnsConstructor.

Parameters

ParameterTypeDescription
_tsensorTypeThe cell preset. SENSOR_NH3 is the preset for this board's cell.
_adcAddruint8_tThe address. Default DEFAULT_ADC_ADDR (0x49). On this board the ADDRESS switch sets the bridge's address from 0x30 to 0x37.
_configPinintA GPIO the library sets low while it configures the LMP91000 when the address is outside 0x30 to 0x37. Not used from 0x30 to 0x37. Default -1, no pin.
bool begin()returns bool

Starts I2C. With an address from 0x30 to 0x37 it checks that the bridge answers and sends it the ADC settings from the preset; with any other address it starts the ADS1115 on that address directly. Then it configures the LMP91000 analog front end. Call it once before any reading.

Returnstrue when every step succeeded. false when the bridge did not answer OK within 500 ms or reported an error, when starting the ADS1115 failed, or when configuring the analog front end failed.

double getPPM()returns double

Takes one ADC reading, removes the front end's internal zero, adds the zero calibration, and turns the cell current into ppm with the preset's sensitivity. A result below zero is returned as 0.

ReturnsThe gas concentration in ppm.

double getPPB()returns double

The same reading as getPPM(), in parts per billion. The example shows it in a comment as an alternative.

ReturnsgetPPM() multiplied by 1000.

Putting it together​

Create the sensor with a preset, call begin() once in setup(), then call getPPM() or getPPB() whenever you want a reading. If begin() returns false, the example prints an error and stops.