Skip to main content

Start a sensor at another address

What this example shows​

The customAddress example creates the sensor at an address you choose instead of the library's default, then prints a reading in ppm every 2.5 seconds. It passes ADC address 0x4B and LMPEN on GPIO 5. At that address the library talks to the ADS1115 and the LMP91000 directly, not through the board's ATtiny404 bridge. See the library overview for how the address decides this.

The sketch's comment names jumpers JP5, JP6 and JP7 for the addresses. On this board the ADS1115 address is set by JP2 (0x49, closed as it comes), JP3 (0x4A) and JP4 (0x4B), and JP5 is the SDA1/SCL1 pull-up jumper. To set 0x4B, as the example does, cut JP2 and then bridge JP4. For 0x4A, cut JP2 and bridge JP3. Keep exactly one of JP2, JP3 and JP4 closed. See Hardware Details for the jumper table and which combinations to avoid.

The example passes SENSOR_SO2, the library's preset for a sulphur dioxide cell. For a nitric oxide reading, use SENSOR_NO, the preset for this board's SGX-4NO-250 cell: 400 nA per ppm, a 120 kΩ amplifier gain and a positive 12% bias.

The example code​

The listing is the library's file at the pinned commit. Only comment lines are left out: the one that names the old connector, and the header lines that link other products' store pages.

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);
}

Expected result, with SENSOR_NO (readings in clean air; yours will differ):

Serial MonitorAutoscrollShow timestampNewline115200 baud
Message (Enter to send message to the board)
Sensor initialized successfully!
Sensor reading: 0.00629 PPM
Sensor reading: 0.00597 PPM
Sensor reading: 0.00626 PPM
Sensor reading: 0.00610 PPM

A Sensor reading: line comes every 2.5 seconds, with the concentration in ppm to five decimals and PPM at the end. If begin() fails, the sketch prints ERROR: Can't init the sensor! Check connections! and stops there.

Functions used​

ElectrochemicalGasSensor(sensorType _t, uint8_t _adcAddr = DEFAULT_ADC_ADDR, int _configPin = -1)returns ElectrochemicalGasSensor

Creates the sensor object for one board. Nothing is sent over I2C until you call begin().

ReturnsThe sensor object.

Parameters

TypeNameDescription
sensorType_tThe cell preset, SENSOR_SO2 in the example. SENSOR_NO is the one for this board's cell.
uint8_t_adcAddrThe I2C address, 0x4B in the example. For an address from 0x30 to 0x37 the library talks to the board's ATtiny404 bridge; for any other it talks to the ADS1115 at that address and to the LMP91000 directly.
int_configPinThe GPIO wired to LMPEN, 5 in the example. The library holds it low only while it configures the LMP91000. Ignored at addresses 0x30 to 0x37.
sensor.begin()returns bool

Starts I2C with Wire.begin(). For an address from 0x30 to 0x37 it checks that the board's bridge answers and sends the ADC and front-end settings through it, waiting up to 500 ms for each step. For any other address it starts the ADS1115 at that address with the preset's gain and the slowest data rate, then configures the LMP91000 (gain, load, reference, internal zero, bias and mode), holding the _configPin GPIO low while it does when one is given.

Returnstrue when every step succeeded, false otherwise.

sensor.getPPM()returns double

Takes one ADC reading and converts it: subtracts the internal zero (2.5 V times the preset's internal zero percentage), adds the zero calibration, divides by the amplifier gain to get the cell current, and divides that by the preset's nA per ppm.

ReturnsThe gas concentration in ppm. A negative result is returned as 0.

sensor.getPPB()returns double

The alternative the sketch's comment offers: takes the same reading as getPPM() and multiplies it by 1000.

ReturnsThe gas concentration in ppb.

Putting it together​

The address and the LMPEN GPIO go to the constructor, and begin() uses them when it starts the sensor. The rest is the same as a single reading: getPPM() in loop(), printed with five decimals every 2.5 seconds.