Custom address
What this example shows
The library's customAddress example creates the sensor at an address other than the default and prints the gas concentration in ppm every 2.5 seconds. It already uses SENSOR_SO2, the library's preset for this board's SGX-4SO2 cell, at address 0x4B with GPIO 5 as its enable pin.
The jumper instructions in this example do not apply to this board: neither the header's "Set the custom address using the jumpers on the back of the board" nor the comments above the constructor (JP5 for 0x49, JP6 for 0x4A, JP7 for 0x4B). Here JP5 and JP6 connect the I2C pull-up resistors, there is no JP7, and JP2 to JP4 stay as shipped. Do not cut or close a jumper because of those lines: the address the sketch talks to is the second argument of the constructor. This board's address switch and jumpers are described on Hardware Details.
On this board the LMP91000 and ADS1115 sit on the board's own bus 2, and your controller reaches them through the board's address on the Qwiic bus: 0x30 as shipped, 0x30 to 0x37 with the ADDRESS switch. The example's 0x4B talks to an ADS1115 directly. Pass the board's address as the second argument of the constructor instead, for example ElectrochemicalGasSensor sensor(SENSOR_SO2, 0x30);. Where the header says to connect LMPEN to GND or a GPIO pin, on this board LMPEN is already pulled to GND by a 10 kΩ resistor and also runs to the on-board microcontroller, and at the board's addresses the library ignores the enable-pin argument, here GPIO 5.
The listing is the library's file at the pinned commit, apart from a few comment-only lines in its header, which name the connector by an older name or link to other Soldered products, and are left out.
The example code
/**
**************************************************
*
* @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);
}
Expected result:
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 noneCreates the sensor object from a cell preset, an address and an optional enable pin. The example passes SENSOR_SO2, address 0x4B and GPIO 5.
ReturnsConstructor.
Parameters
| Parameter | Type | Description |
|---|---|---|
_t | sensorType | The cell preset. SENSOR_SO2 is the preset for this board's cell. |
_adcAddr | uint8_t | The 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. |
_configPin | int | GPIO for the front end's enable line. Ignored at addresses 0x30 to 0x37. Default -1. |
bool begin()returns boolStarts 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 doubleHas 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 doubleThe 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
The address is the second argument of the constructor and the enable pin the third. Every board on the same bus needs its own address.