Skip to main content

Start the sensor at a custom address

What this example shows​

The library's customAddress example creates the sensor at an address you choose, starts it, and prints the gas concentration in ppm every 2.5 seconds.

The example creates the sensor with the sulfur dioxide preset, SENSOR_SO2, at address 0x4B with LMPEN on GPIO 5: the second and third arguments of the constructor. SENSOR_NO2 is the library's preset for this board's SGX-4NO2 cell; change it in the line that creates the sensor.

The example code​

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

The comment above the line that creates the sensor names JP5, JP6 and JP7 for the addresses 0x49, 0x4A and 0x4B. That does not match this board: here JP5 and JP6 disconnect the I2C pull-ups and there is no JP7. The ADS1115 address jumpers on this board are JP2 to JP4, and the board's own I2C address (0x30 to 0x37) is set with the ADDRESS switches. Check the jumper table on the Hardware details page before you cut or close a jumper.

Expected result:

Serial MonitorAutoscrollShow timestampNewline115200 baud
Message (Enter to send message to the board)
Sensor initialized successfully!

Then every 2.5 seconds the sketch prints a line that starts with Sensor reading:, followed by the concentration in ppm with five decimals. If begin() fails, it 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 ElectrochemicalGasSensor

Creates the sensor object. Nothing is sent to the board until you call begin(). The address decides how begin() talks to the board: from 0x30 to 0x37 it sends its commands to the ATtiny404, and from 0x48 to 0x4B (DEFAULT_ADC_ADDR is 0x49) it addresses the ADS1115 at that address and the LMP91000 directly.

ReturnsThe sensor object.

Parameters

TypeNameDescription
sensorType_tThe cell's preset. SENSOR_NO2 is the preset for this board's SGX-4NO2 cell; the example passes SENSOR_SO2.
uint8_t_adcAddrThe address begin() talks to, 0x49 by default; the example passes 0x4B.
int_configPinGPIO for the LMP91000 enable pin (LMPEN), used only at 0x48 to 0x4B. -1 (none) by default; the example passes 5.
sensor.begin()returns bool

Starts Wire and writes the preset to the board. From 0x30 to 0x37 it checks that the ATtiny404 answers, then sends it the ADC gain and data rate and the LMP91000 configuration, each step waiting up to 500 ms for the answer. From 0x48 to 0x4B it starts the ADS1115 at that address, sets the preset's gain and the slowest data rate, and writes the LMP91000 configuration, pulling _configPin low while it does when one is given. Call it once in setup().

Returnstrue when every step succeeded, false otherwise.

sensor.getPPM()returns double

Takes a new ADC reading and turns it into a gas concentration: the voltage minus the internal zero, plus the calibration offset, divided by the TIA gain and by the cell's sensitivity in the preset. A negative result is returned as 0.

ReturnsThe gas concentration in ppm.

sensor.getPPB()returns double

The same reading as getPPM(), multiplied by 1000. The example mentions it in a comment as the line to use when parts per billion suit your sensor better.

ReturnsThe gas concentration in ppb.

Putting it together​

The address and the LMPEN pin are fixed when the sensor object is created, before setup() runs. begin() then runs once; if it fails, the sketch prints the error and stays in an endless loop. In loop(), each getPPM() call reads the cell again, and the sketch prints the value with five decimals and waits 2.5 seconds.