Skip to main content

Use a custom address

What this example shows​

The customAddress example creates the sensor at an address you choose, 0x4B, with configuration pin 5, then prints the gas concentration in PPM every 2.5 seconds. Give the constructor a second argument, the address, and a third, the configuration pin, to do the same in your own sketch. On this board, JP2 to JP4 set the ADS1115's address (JP2 for 0x49, JP3 for 0x4A, JP4 for 0x4B), and the ADDRESS switch sets the board's own address from 0x30 to 0x37. Both are described on Hardware details. JP2 is closed as shipped, so open JP2 before you close JP3 or JP4, and close only one of them: closing JP3 or JP4 with JP2 still closed ties SDA2 or SCL2 to 3V3 and stops the on-board bus.

The example is written with SENSOR_SO2, the library's sulphur dioxide preset, so its PPM values are not H2S values. Do not switch it to the library's SENSOR_H2S preset unchanged: that preset is written for the SGX-4H2S-100 cell, with a sensitivity of 1200 nA per ppm, while this board carries the SGX-4H2S-300 cell, whose datasheet gives an output signal of 460 ± 140 nA per ppm. With SENSOR_H2S the reading comes out about 2.6 times too low. Pass a custom sensor type with the SENSOR_H2S settings and 460 nA per ppm instead, as Quick Start shows.

The example code​

Select Board
customAddress.ino
// Include the required library
#include "Electrochemical-Gas-Sensor-SOLDERED.h"

// Create the sensor object with the according type
// The final parameter is the GPIO pin for LMPEN
ElectrochemicalGasSensor sensor(SENSOR_SO2, 0x4B, 5);

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

Expected result:

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

Then a line starting with Sensor reading: follows every 2.5 seconds, with the concentration in PPM to five decimals. If the sensor does not answer, the sketch prints ERROR: Can't init the sensor! Check connections! and stops.

Functions used​

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

Creates the sensor object. The sensor type holds the front-end and conversion settings for one gas cell. The address decides how begin() talks to the board: an address from 0x30 to 0x37 goes through the board's ATtiny404; any other address, such as the default 0x49, goes to an ADS1115 at that address and to the LMP91000 directly.

ReturnsConstructor.

Parameters

NameTypeDescription
_tsensorTypePreset from the library (SENSOR_CO, SENSOR_NO2, SENSOR_SO2, SENSOR_O3, SENSOR_NO, SENSOR_H2S, SENSOR_NH3, SENSOR_CL2) or a custom sensorType. For this board's cell, pass a custom one with 460 nA per ppm.
_adcAddruint8_tThe address begin() talks to. This example passes 0x4B.
_configPinintA GPIO the library holds high and pulls low while it configures the LMP91000 directly. Ignored at addresses from 0x30 to 0x37. This example passes 5.
bool begin()returns bool

Starts I2C and picks how to talk to the board from the address. With an address from 0x30 to 0x37 it checks that the board's ATtiny404 answers and sends it the ADC gain from the sensor type with the slowest data rate; each command waits up to 500 ms for an answer. With any other address it starts an ADS1115 at that address with the same gain and data rate, sets the configuration pin as an output held high, and pulls it low while it configures the LMP91000. Call it once before reading.

Returnstrue when every step succeeded, false otherwise.

double getPPM()returns double

Reads the ADC, subtracts the front end's internal zero (2.5 V times its percentage), adds the zero calibration, then divides by the amplifier gain and by the cell sensitivity in nA per ppm from the sensor type. A result below 0 is returned as 0.

ReturnsGas concentration in PPM.

double getPPB()returns double

The commented-out alternative in the example: takes a reading with getPPM() and multiplies it by 1000.

ReturnsGas concentration in PPB.

Putting it together​

Pass the address and the configuration pin as the constructor's second and third arguments, call begin() once in setup(), then read with getPPM() or getPPB().