Skip to main content

Read the gas concentration

What this example shows​

The library's bridgeBoard example starts the breakout through its ATtiny404 bridge and prints the gas concentration in ppm every 2.5 seconds. It creates the sensor on address 0x30, where this board answers until you move its ADDRESS switch.

The example creates the sensor with SENSOR_CO, the library's carbon monoxide preset. SENSOR_NH3 is the library's preset for this board's SGX-4NH3 cell: 40 nA per ppm, a 35 kΩ amplifier gain and the ADC's 4.096 V range. 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 example's comments say to set the address with jumpers on the back of the board. On this board the address is set with the three-position ADDRESS switch. The 0x49/0x4A/0x4B the comments mention are ADS1115 addresses, not addresses this board answers on. Where the comments say LMPEN is hardwired to GND, on this board a 10 kΩ resistor pulls it to GND: leave it unconnected.

The example code​

Select Board
bridgeBoard.ino
/**
**************************************************
*
* @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);
}

Expected result:

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.

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 uses DEFAULT_ADC_ADDR, 0x49.

ReturnsConstructor.

Parameters

ParameterTypeDescription
_tsensorTypeThe cell preset. SENSOR_NH3 is the preset for this board's cell.
_adcAddruint8_tThe address. This example passes 0x30, the board's address as shipped; the ADDRESS switch sets it 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 and the switch's address, call begin() once in setup(), then call getPPM() whenever you want a reading. If begin() returns false, check the cable and the address first.