Skip to main content

Read the SO2 concentration

What this example shows​

The library's bridgeBoard example starts the breakout at I2C address 0x30 and prints the gas concentration in ppm every 2.5 seconds. It is the same sketch the Quick Start uses.

The example creates the sensor with the CO cell preset, SENSOR_CO. SENSOR_SO2 is the library's preset for the SGX-4SO2 cell on this board: change SENSOR_CO to SENSOR_SO2 in the line that creates sensor to get SO2 values.

The listing is the library's file at the pinned commit, apart from a few comment-only lines, which name the connector by an older name or link to other Soldered products, and are left out. Where its comments say to set the address with jumpers, on this board the address is set by the ADDRESS switch (0x30 as shipped), and the address jumpers JP2 to JP4 stay as shipped. Where they say LMPEN is hardwired to GND, on this board LMPEN is pulled to GND by a 10 kΩ resistor and also runs to the on-board microcontroller (see Hardware Details).

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 line, a new Sensor reading: line follows every 2.5 seconds, with the concentration in ppm to five decimal places.

Functions used​

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

Creates the sensor object. The sensor type is a preset with the cell's sensitivity and the front end and ADC settings. The address decides how the library talks to the board: 0x30 to 0x37 is the bridge range.

ReturnsConstructor.

Parameters

ParameterTypeDescription
_tsensorTypeThe cell preset. SENSOR_SO2 is the preset for this board's cell.
_adcAddruint8_tThe address the library talks to. 0x30 to 0x37 is the bridge range, set by the board's ADDRESS switch; any other value talks to an ADS1115 directly at that address. Default 0x49. The example passes 0x30.
_configPinintGPIO for the front end's enable line. Ignored at addresses 0x30 to 0x37. Default -1.
bool begin()returns bool

Starts I2C, checks that the board answers, and sends the ADC and LMP91000 settings from the preset to the board. Call it once before any reading.

Returnstrue when the board answered and took every setting, false otherwise.

double getPPM()returns double

Has the board take one ADC reading and turns it into a gas concentration, using the 2.5 V reference, the front end gain and the cell's sensitivity from the preset. A result below zero is returned as 0.

ReturnsGas 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.

ReturnsGas concentration in ppb (getPPM() x 1000).

Putting it together​

begin() sends the cell's settings once, then every getPPM() call asks the board for a fresh conversion. Keep the address in the constructor equal to the ADDRESS switch setting, and give every board on the same bus its own address.