Skip to main content

Read the CO concentration

What this example shows​

The bridgeBoard example starts the board at its default address, 0x30, with the SENSOR_CO preset for the SGX-4CO cell, then prints the carbon monoxide concentration in ppm every 2.5 seconds.

On this board the library talks to the ATtiny404 bridge, not to the front end and ADC directly: any address from 0x30 to 0x37 selects that mode. The bridge sets up the LMP91000 and the ADS1115 on the board's internal bus, so you do not wire LMPEN to anything.

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);
}

On this board, the address is set with the three-switch ADDRESS DIP switch on the top side, at the top left, not with jumpers on the back, and the default 0x30 needs no change. LMPEN is pulled to GND through 10 kΩ rather than wired straight to it; either way, you connect nothing to it.

Expected result in the Serial Monitor at 115200 baud, once the board answers:

Serial MonitorAutoscrollShow timestampNewline115200 baud
Message (Enter to send message to the board)
Sensor initialized successfully!
Sensor reading: 0.29711 PPM
Sensor reading: 0.29430 PPM
Sensor reading: 0.32846 PPM
Sensor reading: 0.29958 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 the board does not answer, the sketch 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 none

Creates the sensor object. The example passes SENSOR_CO and 0x30, the board's address as shipped; at an address from 0x30 to 0x37 the library talks to the board's ATtiny404 bridge.

ReturnsConstructor.

Parameters

ParameterTypeDescription
_tsensorTypeThe 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).
_adcAddruint8_tThe I2C address. Default DEFAULT_ADC_ADDR, 0x49. From 0x30 to 0x37 the library talks to the board's ATtiny404 bridge.
_configPinintA 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 bool

Starts I2C with Wire.begin(), then, at a bridge address, checks that the bridge answers, sends it the ADC gain and data rate from the preset, and sends the front end's configuration. Each step waits up to 500 ms for the bridge to reply.

Returnstrue if every step succeeded, false if the board did not answer or reported an error.

double getPPM()returns double

Has the bridge make 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 double

The 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 and the board's address, call begin() once in setup() and check its result, then call getPPM() whenever you want a reading. For a second board on the same bus, give it a different address with its address switches and create a second object with that address.