Custom address
What this example shows
The customAddress example creates the sensor at an address other than the
default, passing the address and a config pin to the constructor, and prints
the gas concentration in ppm every 2.5 seconds. It uses the address 0x4B and
GPIO 5.
What it needs on this board
0x4B is an ADS1115 address, not the board's bridge address. At an address
outside 0x30 to 0x37 the library does not go through the board's bridge:
begin() starts the ADS1115 at that address and writes the LMP91000's
configuration itself, over the controller's I2C, with GPIO 5 held low while it
writes. On this board those two chips sit on the internal bus, which the top
header brings out as SDA2 and SCL2. The Qwiic connectors and
SDA1/SCL1 reach only the ATtiny404 bridge, and an I2C scan over Qwiic finds
only the bridge, at 0x30 as shipped. LMPEN is on the bottom header, pulled to
GND through 10 kΩ and also wired to the ATtiny404.
So wire the top header for this example, not the Qwiic cable: connect your
controller's SDA to SDA2, its SCL to SCL2, its 3.3 V pin to 3V3 and its
GND to GND. Over Qwiic only the bridge answers.
The listing's comment names JP5 for 0x49, JP6 for 0x4A and JP7 for 0x4B.
Those names do not match this board's jumpers. Here JP5 sets the 10 kΩ
pull-ups on SDA1/SCL1, JP6 the 10 kΩ pull-ups on the internal
SDA2/SCL2 bus, and there is no JP7: do not cut JP5 or JP6 to set an
address. On this board JP2 to JP4 set the ADS1115's address on the internal
bus (JP2 closed for 0x49 as shipped, JP3 for 0x4A, JP4 for 0x4B), and the
three-switch ADDRESS DIP switch sets the board's own address on your bus, from
0x30 to 0x37. The switch and the jumpers are described on
Hardware Details.
Before you upload it, change SENSOR_SO2 to SENSOR_CO in the line that
creates the sensor. SENSOR_SO2 is the library's preset for the sulphur
dioxide cell. This board carries the SGX-4CO carbon monoxide cell, and the
library's preset for it is SENSOR_CO.
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 lines below are from a run of this example on this board, with the
controller wired to the header's SDA2, SCL2, 3V3 and GND pins as
described above.
Expected result in the Serial Monitor at 115200 baud:
Sensor initialized successfully!
Sensor reading: 0.29917 PPM
Sensor reading: 0.31240 PPM
Sensor reading: 0.29335 PPM
Sensor reading: 0.32002 PPM
Then a line every 2.5 seconds: Sensor reading: , the concentration in ppm
with five decimals, and PPM. A result below zero is printed as 0.00000.
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. The example passes a preset, the address 0x4B and GPIO 5 as the config pin.
ReturnsConstructor.
Parameters
| Parameter | Type | Description |
|---|---|---|
_t | sensorType | The cell's preset. For this board's SGX-4CO cell use SENSOR_CO (70 nA/ppm, ADC range 4.096 V, 14 kΩ amplifier gain, 20% internal zero). |
_adcAddr | uint8_t | The I2C address. Default DEFAULT_ADC_ADDR, 0x49. From 0x30 to 0x37 the library talks to the board's ATtiny404 bridge. |
_configPin | int | A GPIO the library drives low while it sends the front end's configuration and high otherwise. Not used at a 0x30 to 0x37 address. Default -1, none. |
bool begin()returns boolStarts I2C with Wire.begin(). At an address from 0x30 to 0x37 it checks that the bridge answers, sends it the ADC gain and data rate from the preset and the front end's configuration, waiting up to 500 ms for each reply. At any other address it starts the ADC at that address, sets its gain and its slowest data rate, then writes the front end's configuration with the config pin held low.
Returnstrue if every step succeeded, false otherwise.
double getPPM()returns doubleMakes one ADC conversion of the front end's output, takes off the internal zero, adds the zero calibration, and converts the cell current to ppm with the preset's sensitivity.
ReturnsThe gas concentration in ppm. A negative result is returned as 0.
double getPPB()returns doubleThe same measurement as getPPM(), in parts per billion. The example shows it as a commented-out alternative.
ReturnsgetPPM() multiplied by 1000.
Putting it together
Pass the address as the constructor's second argument, with SENSOR_CO as the
preset, call begin() once in setup() and check its result, then call
getPPM() whenever you want a reading. The example passes 0x4B, an ADS1115
address, with GPIO 5 as the config pin. Through the board's bridge, the address
is the one on its ADDRESS switch, from 0x30 to 0x37 (0x30 as shipped), and the
config pin is not used.