Skip to main content

Read the sensor

What this example shows​

The bridgeBoard example starts the board through its ATtiny404 at 0x30, sends the analog front end its settings, and prints a reading every 2.5 seconds. Your controller only ever talks to the ATtiny404; it forwards the settings to the LMP91000 and reads the ADS1115 for you.

Connect the board with a Qwiic cable, or wire 3V3, GND, SDA1 and SCL1 from the top header. You do not need to wire LMPEN: the board holds it low itself. Where the sketch's comments mention address jumpers on the back of the board, this board uses the three-position ADDRESS switch on its top side instead.

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 it, a line Sensor reading: followed by a value with five decimals and PPM arrives every 2.5 seconds. 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 ElectrochemicalGasSensor

Creates the sensor object with a sensor configuration and an address. Nothing is sent to the board until you call begin(). An address from 0x30 to 0x37 tells the library this is a board with the ATtiny404 bridge, and the third argument is then ignored.

ReturnsThe sensor object.

Parameters

TypeNameDescription
sensorType_tThe front end and conversion settings, such as SENSOR_CO. The presets live in the library's src/sensorConfigData.h.
uint8_t_adcAddrThe board's address. Use 0x30 to 0x37 for this board, matching the ADDRESS switch. For any other address, such as the default DEFAULT_ADC_ADDR (0x49), begin() talks to the ADS1115 at that address directly instead of to the bridge.
int_configPinGPIO for LMPEN on the direct-wired board. Unused on this board.
sensor.begin()returns bool

Starts I2C with Wire.begin(), checks that the bridge answers, sends it the ADC gain and the slowest ADC data rate, then sends the LMP91000 settings from the sensor configuration. Each command to the bridge waits up to 500 ms for an answer.

Returnstrue when every step succeeded, false when the bridge did not answer or reported an error.

sensor.getPPM()returns double

Asks the bridge for one ADC conversion and turns the voltage into a concentration: it subtracts the internal zero (a share of the board's 2.5 V reference), adds the configuration's zero calibration, divides by the TIA gain to get the cell current, then divides by the configuration's nanoamperes per PPM. A negative result is returned as 0.

ReturnsThe concentration in PPM, calculated with the sensor configuration you passed to the constructor.

sensor.getPPB()returns double

The commented-out alternative in the example: the same reading as getPPM(), multiplied by 1000.

ReturnsThe concentration in PPB.

Putting it together​

SENSOR_CO is the preset for an SGX-4CO carbon monoxide cell. The libraries have no preset for this board's SGX-4OX-ROHS oxygen cell yet, so the value the example prints is calculated with the CO cell's settings. You set the front end's bias and gain yourself:

  • Copy one of the presets in src/sensorConfigData.h into your sketch as your own sensorType and pass it to the constructor with address 0x30.
  • The cell needs a −600 mV bias. With the board's 2.5 V reference, that is BIAS_SIGN_NEGATIVE with BIAS_24_PERCENT (24% of 2.5 V is 0.6 V).
  • Keep REF_EXTERNAL: the board has a 2.5 V external reference.
  • The cell's output in air is 0.1 ± 0.03 mA, and its output is logarithmic, S = K ln(1/(1-C)), so the library's linear PPM calculation does not convert it to % vol directly.

After power-up the cell can take up to 15 minutes to settle, so let it run before you trust a reading.

Up to eight boards share one bus: give each one its own address with the ADDRESS switch and create one ElectrochemicalGasSensor per address, from 0x30 to 0x37.