Skip to main content

Custom address

What this example shows​

The library's customAddress example creates the sensor at an I2C address other than the default, passing the address and a pin as the constructor's second and third arguments, and prints the concentration in ppm every 2.5 s. It passes the address 0x4B and pin 5.

The example creates the sensor with SENSOR_SO2, the sulphur dioxide preset. The library calculates ppm from the preset's sensitivity and gain settings; the preset for this board's chlorine cell is SENSOR_CL2.

This board's own address is set with its ADDRESS switch, from 0x30 to 0x37 (0x30 as shipped); see Hardware details.

The example code​

Select Board
customAddress.ino
/**
**************************************************
*
* @file customAddress.ino
* @brief See how to init a sensor on a custom address
*
* To successfully run the sketch:
* - Set the custom address using the jumpers on the back of the board
* - Connect LMPEN pin to GND or a GPIO pin so the breakout can be configured
* - Run the sketch and open serial monitor at 115200 baud!
*
*
* @authors Robert @ soldered.com
***************************************************/

// Include the required library
#include "Electrochemical-Gas-Sensor-SOLDERED.h"

// Create the sensor object with the according type
// To set a custom address, connect one of the following jumpers:
// JP5 for 0x49
// JP6 for 0x4A
// JP7 for 0x4B
// 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);
}

The example's comments set the address with jumpers on the back of the board, name jumpers for 0x49 to 0x4B, and ask for LMPEN to be connected. On this board all jumpers are on the top side, JP5 and JP6 are pull-up jumpers, and the board's own address is set with the ADDRESS switch. A 10 kΩ resistor holds LMPEN low and it is wired to the ATtiny404, so you leave it unconnected (see Hardware details).

Expected result:

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

Then a line every 2.5 s: Sensor reading: , the concentration in ppm with five decimals, and PPM. If begin() fails, 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 ElectrochemicalGasSensor

Creates the sensor object. It stores the settings only; nothing is sent to the board until begin().

ReturnsThe sensor object.

Parameters

TypeNameDescription
sensorType_tThe cell preset. The example passes SENSOR_SO2; this board's SGX-4CL2 cell has the preset SENSOR_CL2.
uint8_t_adcAddrThe I2C address, DEFAULT_ADC_ADDR (0x49) when left out; the example passes 0x4B. For an address from 0x30 to 0x37, the range of this board's ADDRESS switch, begin() talks to the board's ATtiny404; for any other address it looks for an ADS1115 at that address and the LMP91000 directly on the bus.
int_configPinA GPIO that begin() drives low while it configures the LMP91000 directly; the example passes 5. -1 (none) when left out. Unused for addresses 0x30 to 0x37.
bool begin()returns bool

Starts Wire, checks that the board answers, sets the preset's ADC gain and data rate, and configures the LMP91000 with the preset's settings. Call it once in setup() before reading.

Returnstrue when the board answered and both configuration steps succeeded, false otherwise.

double getPPM()returns double

Takes one reading and converts it to ppm: it subtracts the internal zero voltage (the 2.5 V reference times the preset's internal zero percentage), adds the zero calibration, divides by the amplifier gain and by the preset's sensitivity in nA per ppm. A result below zero is returned as 0.

ReturnsThe gas concentration in ppm.

double getPPB()returns double

Same reading as getPPM(), times 1000. The example shows it in a comment as the alternative to getPPM().

ReturnsThe gas concentration in ppb.

The example also uses Serial and delay(), which come with the Arduino core, not with this library.

Putting it together​

The address and the pin are the only differences from a sensor created with the defaults: begin() reads the address to decide how to reach the board, and the rest of the sketch is the same single reading every 2.5 s.