Sensor on a custom address
What this example shows
The library's customAddress example creates the sensor on an address you pass to the constructor, starts it, and prints the gas concentration in ppm every 2.5 seconds.
The example passes SENSOR_SO2, the library's sulfur dioxide preset, address 0x4B and GPIO 5 as the configuration pin. SENSOR_NH3 is the library's preset for this board's ammonia cell, and the ppm conversion uses the preset's sensitivity. As shipped, the example's readings are therefore not ammonia ppm. To read ammonia, change SENSOR_SO2 to SENSOR_NH3 in the line that creates the sensor.
The library talks to the board's ATtiny404 bridge when the address is from 0x30 to 0x37; with any other address, such as this example's 0x4B, it talks to an ADS1115 at that address directly, with the configuration pin set low while it configures the LMP91000. On this board the ADS1115 sits on the header's SDA2/SCL2 pins, which the Qwiic connectors do not reach, so this example does not run over the Qwiic cable. Wire the controller to the header instead: 3.3 V to 3V3, GND to GND, SDA to SDA2 and SCL to SCL2. The example then runs as written, with no change to the address; the ADS1115 answers on 0x4B when JP4 is the one closed of JP2 to JP4. The board's own ADDRESS switch is described in Hardware Details.
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);
}
The jumper names in the example's comments do not match this board. Here JP2 to JP4 set the ADS1115's address (JP2 0x49, closed when the board ships; JP3 0x4A; JP4 0x4B), JP5 and JP6 are the 10 kΩ I2C pull-up jumpers, and there is no JP7. Do not cut JP5 or JP6 to change an address. The board's own address is set with the ADDRESS switch, not with jumpers on the back. The comments also connect LMPEN to GND or a GPIO pin: on this board a 10 kΩ resistor already pulls it to GND, so leave it unconnected.
The Serial Monitor shows:
Sensor initialized successfully!
After that, a line starting with Sensor reading: follows every 2.5 seconds, with the concentration in ppm to five decimals.
If begin() returns false, the example prints ERROR: Can't init the sensor! Check connections! instead and stops.
Functions used
ElectrochemicalGasSensor(sensorType _t, uint8_t _adcAddr = DEFAULT_ADC_ADDR, int _configPin = -1)returns noneCreates the sensor object on the address you pass. begin() picks how to talk to the board from the address: from 0x30 to 0x37 it uses the ATtiny404 bridge; any other address is used as the ADS1115's own I2C address.
ReturnsConstructor.
Parameters
| Parameter | Type | Description |
|---|---|---|
_t | sensorType | The cell preset, here SENSOR_SO2. SENSOR_NH3 is the preset for this board's cell. |
_adcAddr | uint8_t | A bridge address from 0x30 to 0x37, or otherwise an ADS1115 address; here the ADS1115 address 0x4B. This board's own address is its bridge address, set with the ADDRESS switch from 0x30 to 0x37. |
_configPin | int | A GPIO the library sets low while it configures the LMP91000 when the address is outside 0x30 to 0x37, here 5. Not used from 0x30 to 0x37. |
bool begin()returns boolStarts I2C. With an address from 0x30 to 0x37 it checks that the bridge answers and sends it the ADC settings from the preset; with any other address it starts the ADS1115 on that address directly. Then it configures the LMP91000 analog front end. Call it once before any reading.
Returnstrue when every step succeeded. false when the bridge did not answer OK within 500 ms or reported an error, when starting the ADS1115 failed, or when configuring the analog front end failed.
double getPPM()returns doubleTakes one ADC reading, removes the front end's internal zero, adds the zero calibration, and turns the cell current into ppm with the preset's sensitivity. A result below zero 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 in a comment as an alternative.
ReturnsgetPPM() multiplied by 1000.
Putting it together
The address you pass to the constructor is the one begin() talks to, so it must match the address the board answers on. Then call begin() once in setup() and getPPM() whenever you want a reading.