Library for gas sensors

Before starting

The Gases PRO Sensor Board for Waspmote has its own library which contains the set of necessary instructions to easily configure and read each one of the sensors which can be connected to the board. Next, each one of the functions is described and the process of configuration detailed for each sensor. The specific configuration which must be applied to each one of the sensors is explained in the specific sensor's section.

When using the Gases PRO Sensor Board on Waspmote, remember it is mandatory to include the WaspSensorGas_Pro library by introducing the next line at the beginning of the code:

#include <WaspSensorGas_PRO.h>

Each gas sensor needs its own object. So, between the include of the library and the global variables declaration, the object must be created following the next structure:

  • Name of the class: Gas

  • Name of the object. We recommend to use the formula of the gas sensor. In this case CO.

  • In brackets the socket selected for the sensor. In this case SOCKET_1.

See the example below:

Gas CO(SOCKET_1);

Power and configuration

Initializing the sensor

Before reading the concentration values, the sensor must be powered using the function ON(). Unlike with other sensor boards, the user does not need to power on the board. The API library performs the next tasks in the power on process from each sensor:

  • Powers on the board (if necessary)

  • Powers on the sensor

  • Reads the parameters from the EEPROM

  • Sets the AFE module to work with the sensor

gas_sensor.ON();
gas_sensor.ON(R_gain);

Where R_gain is the gain associated to the transimpedance amplifier. Allowed values for sensors that uses the 3-electrode AFE are:

  • LMP91000_GAIN_1

  • LMP91000_GAIN_2

  • LMP91000_GAIN_3

  • LMP91000_GAIN_4

  • LMP91000_GAIN_5

  • LMP91000_GAIN_6

  • LMP91000_GAIN_7

In all cases the function returns:

  • 1 if OK

  • -1 no communication with AFE module

The Methane and Combustible Gases Sensor and the CO2 Gas Sensor and the CO low concentration Gas Sensor do not have a transimpedance stage so they do not admit a gain value. If R_gain is not selected, a default value will be loaded.

The BME280 (Temperature, Humidity and Pressure Sensor) is automatically powered on when any gas sensor is powered on.

Switching sensor off

In order to save battery, the sensor could be powered off (Methane and Combustible Gases and CO2) using the function OFF(). Unlike other sensor boards, the Gases PRO Sensor Board does not need to power off the board. The API library powers off the board automatically when the last sensor is turned off.

gas_sensor.OFF();

If the sensor needs to be powered off for a long time (weeks or months) is recommendable to power off with the parameter 1 in the off function.

gas_sensor.OFF(1);

In all cases the function returns the pwrGasPRORegister. Each bit of this register represents the status of a sensor. '1' for the powered on sensors and '0' for the powered off.

  • 7: not used

  • 6: gas sensor in socket 6

  • 5: gas sensor in socket 5 or socket B in Plug & Sense!

  • 4: gas sensor in socket 4 or socket A in Plug & Sense!

  • 3: gas sensor in socket 3 or socket F in Plug & Sense!

  • 2: gas sensor in socket 2

  • 1: gas sensor in socket 1 or socket C in Plug & Sense!

  • 0: particles-dust sensor (socket D in Plug & Sense!)

Setting gain resistor for transimpedance stage

The transimpedance stage converts a current into a proportional voltage. The conversion factor is fixed by the resistor of the stage (V = R·I). The resistor, and therefore the gain, can be selected by the user according to the gas concentration to measure. High gains should be used to measure at low concentrations. On the other hand, low gains should be used at high concentrations. Using high gains with high concentrations, the voltage input for the ADC will be out of its range, so the measured concentration will be wrong.

gas_sensor.configureAFE(float R_gain);

Allowed values for R_gain in 3-electrode sensors:

  • LMP91000_GAIN_1

  • LMP91000_GAIN_2

  • LMP91000_GAIN_3

  • LMP91000_GAIN_4

  • LMP91000_GAIN_5

  • LMP91000_GAIN_6

  • LMP91000_GAIN_7

In all cases the function returns:

  • 1 if OK

  • -1 no communication with AFE module

How to choose the right gain resistor

As has been annotated in other chapter, electrochemical sensors generate a current proportional to the gas concentration. The transimpedance stage converts the current (using a multiplication factor) in a voltage for the ADC, but it has a limit. For 3-electrode gas sensors, it can only generate ±1024 mV. So, the multiplication factor (gain resistor) must be selected according the current generated by the sensor.

To know the current generated, the expected concentration to measure and the approximated sensitivity of the sensor must be applied into the next equation:

The tables below show the maximum currents for each R_gain setting.

Table: Maximum current by gain setting for a 3-electrode AFE

  • An example with a H2 Gas Sensor (3-electrode) is shown below:

  • expected H2 concentration: 524 ppm

  • H2 Gas Sensor sensitivity: 20 nA/ppm

The current is 25.56 µA, very high for LMP91000_GAIN_6 and LMP91000_GAIN_7, but not for LMP91000_GAIN_5. So, the right gain should be LMP91000_GAIN_5 or less.

Power modes

The 3-electrode and 4-electrode AFEs can change the power mode of the circuits that manages the electrochemical sensor. The power modes allowed are below:

  • AFE_DEEP_SLEEP: AFE module changes to deep sleep mode. ADC changes to low power standby mode (0.1 µA typical consumption), bias stage and transimpedance amplifier are powered off (0.6 µA typical consumption).

  • AFE_STAND_BY: AFE module changes to standby mode. ADC changes to low power standby mode (0.1 µA typical consumption), bias stage is active and transimpedance amplifier is off (6.5 µA typical consumption).

  • AFE_AMPERIOMETRIC: AFE module changes to amperometric mode. ADC changes to low power standby mode (0.1 µA typical consumption), bias stage and transimpedance amplifier are on (10 µA typical consumption).

In all cases, the ADC only exits of low power standby mode to performs the conversion. When the conversion finishes, the ADC changes automatically to low power standby mode.

gas_sensor.setPowerMode(power_mode);

The CH4 and combustible gases module, and the CO2 module have only 2 states: on and off. So, this function cannot be used.

The function returns:

  • 1 if OK

Reading the sensors

Temperature

The developer can read two temperature sensors. One of them is inside the AFE module for the 3- sensors. The other one is the BME280 sensor. If no parameter is passed, getTemp() function will read the BME280 sensor and, if the BME280 sensor fails, it will read the temperature sensor installed in the AFE.

float temperature;
// Reads the temperature sensor
temperature = gas_sensor.getTemp();

The sensor to read can be selected with '0' to read the sensor of the AFE and '1' to read the BME280.

float temperature;
// Reads the temperature sensor from the AFE module
temperature = gas_sensor.getTemp(0);
// Reads the BME280 sensor
temperature = gas_sensor.getTemp(1);

The CH4 and combustible gases module, the CO2 module and all the 4 electrode sensors can only read the BME280 sensor.

In all cases the function returns:

  • the temperature in ºC

  • -1000 if I2C bus communication error

Humidity

Humidity sensor from BME280 environmental sensor can be read with the function getHumidity(). This function does not need any parameter.

float humidity;
// Reads the environmetal humidity from BME280 sensor
humidity = gas_sensor.getHumidity();

The function returns:

  • the percentage of relative humidity (% RH)

  • -1000 if I2C bus communication error

Pressure

Pressure sensor from BME280 environmental sensor can be read with the function getPressure(). This function does not need any parameter.

float pressure;
// Reads the environmetal pressure from BME280 sensor
pressure = gas_sensor.getPressure();

The function returns:

  • the pressure in Pascals (Pa)

  • -1000 if I2C bus communication error

Gas concentration

There are 4 functions to read the gas concentration, according to the parameters passed to the function. The first one is getConc(). This function performs a high resolution A-D conversion, reads the temperature sensor to compensate baseline and sensitivity and converts to ppm automatically. In the case of a 4-electrode AFE, it will read the both electrodes and compensate them.

The second one, getConc(resolution), performs the A-D conversion with the resolution passed as parameter, reads the temperature sensor to compensate baseline and sensitivity and converts to ppm automatically. In the case of a 4-electrode AFE, it will read both electrodes and compensate them. Allowed resolution parameters are:

  • MCP3421_LOW_RES: 12 bits conversion

  • MCP3421_MEDIUM_RES: 14 bits conversion

  • MCP3421_HIGH_RES: 16 bits conversion

  • MCP3421_ULTRA_HIGH_RES: 18 bits conversion

The third one, getConc(resolution, NO2_conc), must only be used with a ozone sensor. In this case, the function performs the A-D conversion with the resolution passed as parameter, reads the temperature sensor to compensate the sensitivity and converts to ppm automatically. Also, it compensates the NO2 cross-sensitivity with the NO2 concentration, passed as parameter. Allowed resolution values are:

  • MCP3421_LOW_RES: 12 bits conversion

  • MCP3421_MEDIUM_RES: 14 bits conversion

  • MCP3421_HIGH_RES: 16 bits conversion

  • MCP3421_ULTRA_HIGH_RES: 18 bits conversion

The last one is getConc(resolution, temperature, NO2_conc). In this case, the function performs the A-D conversion with the resolution passed as parameter, reads the temperature sensor to compensate the sensitivity and converts to ppm automatically. Also, it compensates the NO2 cross-sensitivity with the NO2 concentration, passed as parameter. The function only will be used with ozone sensors. Allowed resolution values are:

  • MCP3421_LOW_RES: 12 bits conversion

  • MCP3421_MEDIUM_RES: 14 bits conversion

  • MCP3421_HIGH_RES: 16 bits conversion

  • MCP3421_ULTRA_HIGH_RES: 18 bits conversion

Temperature must be in Celsius degrees. If the value is -1000 ºC (means error in the temperature sample), the function does not compensate the baseline and sensitivity.

float gas_concentration;
// Read gas concentration
gas_concentration = gas_sensor.getConc();
// Read gas concentration with ultra high resolution (18 bits)
gas_concentration = gas_sensor.getConc(MCP3421_ULTRA_HIGH_RES);
// Read gas concentration from O3 sensor with low res and compensate NO2 gas
gas_concentration = gas_sensor.getConc(MCP3421_LOW_RES, 0.125);
// Read gas concentration
gas_concentration = gas_sensor.getConc(MCP3421_MEDIUM_RES, 24.5, 1.25);

In all cases the function returns:

  • concentration value in ppm or %LEL

  • - 1000 if I2C bus communication error

Autogain process

The function autoGain() has been developed to fix the voltage generated in the transimpedance stage out of the limits of the ADC. It must only be used with 3-electrode sensors. The function will change internally the values of the gain resistor and the voltage reference until the voltage measured by the ADC is out of the limits or more changes cannot be done.

autoGain() should be used when the sensor has completed the warm up time.

// Performs autoGain process
gas_sensor.autoGain();

The function returns the changes done:

  • high nibble (4 MSB): steps in voltage reference (0..15)

  • low nibble (4 LSB): steps in gain resistor (0..15)

Showing sensor information

The function showSensorInfo() shows the sensor data via USB. This function does not need any parameters and returns nothing.

// Show sensor data
gas_sensor.showSensorInfo();

The files of the sensor board itself are: WaspSensorGas_Pro.cpp, WaspSensorGas_Pro.h

They can be downloaded from:

Last updated