Use a custom address
What this example shows
The customAddress example creates the sensor at an address other than the
library's default and prints the gas concentration in parts per million (PPM)
every 2.5 seconds.
Wire your controller to the top header for this example, not to a Qwiic
connector: the controller's SDA to SDA2, its SCL to SCL2, its 3.3 V supply
to 3V3 and its ground to GND. At this address the library talks to the
ADS1115 and the LMP91000 directly on SDA2 and SCL2; the board's ATtiny404 bridge at 0x30
is not used. Over
Qwiic, only the bridge example in the
Quick Start
works.
The example code
The sketch uses the sulfur dioxide preset, SENSOR_SO2, and creates the
sensor at address 0x4B with GPIO 5 as _configPin, the sketch's "GPIO pin for
LMPEN". Wire the board's LMPEN pin on the header to that GPIO. On this
board, LMPEN also goes to the board's ATtiny404.
The jumper names in the sketch's comment (JP5, JP6, JP7) are not this board's
address jumpers. On this board, JP5 and JP6 connect the I2C pull-ups, and the
ADS1115's address is set with JP2 to JP4. JP2 is closed as shipped (0x49).
Open JP2 before you close JP3 (0x4A) or JP4 (0x4B), and close only one of
them: closing JP3 or JP4 with JP2 still closed ties SDA2 or SCL2 to 3V3
and stops the bus. For this sketch's 0x4B, open JP2 and close JP4.
Hardware details
lists every jumper and the combinations to avoid.
/**
**************************************************
*
* @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);
}
🔗Open the complete example
examples/customAddress at commit c4df54b
Open the Serial Monitor at 115200 baud. The sketch prints:
Sensor initialized successfully!
Then a line every 2.5 seconds: Sensor reading: , the concentration 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 noneCreates the sensor object for one board. The address decides how begin() talks to it: for an address from 0x30 to 0x37 it uses the protocol of the ATtiny404 bridge; for any other address it talks to an ADS1115 at that address and to the LMP91000 itself. The default is DEFAULT_ADC_ADDR (0x49).
ReturnsConstructor.
Parameters
| Name | Type | Description |
|---|---|---|
_t | sensorType | The preset for your cell: SENSOR_CO, SENSOR_NO2, SENSOR_SO2, SENSOR_O3, SENSOR_NO, SENSOR_H2S, SENSOR_NH3, SENSOR_CL2, or your own sensorType. |
_adcAddr | uint8_t | The address begin() uses; here 0x4B. 0x30 to 0x37 selects the bridge protocol; this board answers at 0x30 out of the box, or at the address its switch sets. Any other value is taken as an ADS1115 address. 0x49 by default. |
_configPin | int | A GPIO for the LMP91000 enable line; here 5. The library holds it high and pulls it low only while it writes the LMP91000's registers itself; for an address from 0x30 to 0x37 it is not used. -1 (none) by default. |
bool begin()returns boolStarts I2C with Wire.begin() and picks the protocol from the address. For 0x30 to 0x37 it checks that the bridge answers, sends the ADC gain from the preset and has the bridge write the LMP91000's registers, each step giving up after 500 ms. For any other address it checks that an ADS1115 answers at that address, sets its gain, and writes the LMP91000's registers when the LMP91000 reports ready, pulling _configPin low around the writes when one is given. Call it once before reading.
Returnstrue when every step succeeded, false otherwise.
double getPPM()returns doubleTakes one reading of the ADS1115's channel 0 (through the bridge for 0x30 to 0x37) and converts it to a concentration with the preset: it removes the internal zero offset, adds the zero calibration, divides by the amplifier gain and by the cell's sensitivity in nA per PPM. A negative result is returned as 0.
ReturnsThe gas concentration in PPM.
double getPPB()returns doubleThe same reading as getPPM(), in parts per billion. The example shows it as a commented-out alternative.
ReturnsThe gas concentration in PPB, getPPM() x 1000.
Putting it together
Pass the address and, when you use one, the GPIO for the enable line as the
second and third arguments of ElectrochemicalGasSensor. Then call begin()
once and read with getPPM() or getPPB().