SparkFun GM-X02B Arduino Library  v1.0.1
An Arduino library for the SparkFun Qwiic MEMS gas sensor breakouts built on the Winsen GM-x02B sensor family
Loading...
Searching...
No Matches
sfDevGMX02B.h File Reference

Header file for the SparkFun Winsen GM-X02B MEMS Gas Sensor Driver. More...

#include <stddef.h>
#include <stdint.h>
#include <sfTk/sfTkII2C.h>
#include <sfTk/sfToolkit.h>

Go to the source code of this file.

Classes

struct  sfe_gmx02b_part_t
 Per-part parameters for one GM-x02B variant and its board. More...
 
class  sfDevGMX02B
 Platform-independent driver for the SparkFun Qwiic MEMS gas sensor breakouts. More...
 

Typedefs

typedef enum sfe_gmx02b_sensor_t sfe_gmx02b_sensor_t
 The Winsen GM-x02B sensing elements carried by the SparkFun Qwiic gas sensor boards.
 
typedef enum sfe_gmx02b_cycle_time_t sfe_gmx02b_cycle_time_t
 Cycle time settings for the ADC101C021's automatic conversion mode.
 

Enumerations

enum  sfe_gmx02b_sensor_t : uint8_t {
  GM_102B = 0 , GM_202B , GM_302B , GM_402B ,
  GM_502B , GM_512B , GM_602B , GM_702B ,
  GM_802B , GM_PART_COUNT , GM_UNKNOWN = 0xFF
}
 The Winsen GM-x02B sensing elements carried by the SparkFun Qwiic gas sensor boards. More...
 
enum  sfe_gmx02b_cycle_time_t : uint8_t {
  GM_CYCLE_DISABLED = 0x00 , GM_CYCLE_TIME_32 = 0x01 , GM_CYCLE_TIME_64 = 0x02 , GM_CYCLE_TIME_128 = 0x03 ,
  GM_CYCLE_TIME_256 = 0x04 , GM_CYCLE_TIME_512 = 0x05 , GM_CYCLE_TIME_1024 = 0x06 , GM_CYCLE_TIME_2048 = 0x07
}
 Cycle time settings for the ADC101C021's automatic conversion mode. More...
 

Functions

const sfe_gmx02b_part_t * sfe_gmx02b_partInfo (sfe_gmx02b_sensor_t sensor)
 Get the descriptor for a family member.
 
const char * sfe_gmx02b_partName (sfe_gmx02b_sensor_t sensor)
 Get a printable part name.
 
const char * sfe_gmx02b_targetGas (sfe_gmx02b_sensor_t sensor)
 Get a printable target gas description.
 

Detailed Description

Header file for the SparkFun Winsen GM-X02B MEMS Gas Sensor Driver.

sfDevGMX02B is a comms-agnostic driver for the SparkFun Qwiic MEMS gas sensor breakouts, built on the SparkFun Toolkit. The boards share one design: a Winsen GM-x02B metal-oxide semiconductor (MOS) sensing element in series with a load resistor, read by a Texas Instruments ADC101C021 10-bit I2C ADC. Nine sensor variants are covered, one per target gas, each shipping at a unique default I2C address so multiple boards can share a bus.

The ADC101C021 also provides a programmable out-of-range alert function: with automatic conversion mode enabled, the ADC monitors the sensor continuously in hardware and flags (or signals on its ALERT pin) whenever the reading crosses a programmable high or low limit, with no polling required. The lowest and highest conversion results are tracked in hardware as well.

Note
SENSOR DIRECTION. Eight of the nine elements sense reducing gases: their resistance (Rs) FALLS as gas concentration rises. The GM-102B senses NO2, an oxidizing gas, and its resistance RISES with concentration. Because Rs sits on the high side of the divider, that inversion propagates to ADC counts, to which alert limit applies, and to which hardware min/max register holds the peak gas reading. Use getGasResponse(), setGasAlertResponse(), getGasAlert() and getPeakGasCounts() to stay direction-agnostic; the raw register accessors are direction-neutral by definition and leave the interpretation to the caller.
These are heated MOS elements and must be preheated before readings are meaningful: minutes for the output to settle after power-up, and 24 to 168 hours of powered aging after long unpowered storage (the exact schedule is per-part; see the datasheet). Out of the box these sensors are good for relative measurement and threshold alerts. Absolute ppm requires calibration against a known gas concentration (see setResponseCurve()).
Author
SparkFun Electronics
Date
September 2026

SPDX-License-Identifier: MIT

See also
https://github.com/sparkfun/SparkFun_GM-X02B_Arduino_Library

Typedef Documentation

◆ sfe_gmx02b_cycle_time_t

Cycle time settings for the ADC101C021's automatic conversion mode.

In automatic conversion mode the ADC samples the sensor on its own at the selected interval (expressed as multiples of the ~1 us conversion time), keeps the conversion result register fresh, tracks the lowest / highest conversions, and evaluates the alert limits after every conversion. GM_CYCLE_DISABLED (the power-on default) returns the ADC to normal mode, where a new conversion only occurs when the conversion result register is read.

◆ sfe_gmx02b_sensor_t

The Winsen GM-x02B sensing elements carried by the SparkFun Qwiic gas sensor boards.

These are ordinals, not I2C addresses; they index the per-part descriptor table. Use sfe_gmx02b_partInfo() or getDefaultAddress() to obtain a part's factory address.

Note
The onboard ADC101C021 has no product ID register, so the sensing element cannot be read back from the hardware and must be declared by the sketch.

Enumeration Type Documentation

◆ sfe_gmx02b_cycle_time_t

enum sfe_gmx02b_cycle_time_t : uint8_t

Cycle time settings for the ADC101C021's automatic conversion mode.

In automatic conversion mode the ADC samples the sensor on its own at the selected interval (expressed as multiples of the ~1 us conversion time), keeps the conversion result register fresh, tracks the lowest / highest conversions, and evaluates the alert limits after every conversion. GM_CYCLE_DISABLED (the power-on default) returns the ADC to normal mode, where a new conversion only occurs when the conversion result register is read.

Enumerator
GM_CYCLE_DISABLED 

Automatic conversion mode off (normal mode, default).

GM_CYCLE_TIME_32 

Convert every Tconvert x 32 (~27 ksps).

GM_CYCLE_TIME_64 

Convert every Tconvert x 64 (~13.5 ksps).

GM_CYCLE_TIME_128 

Convert every Tconvert x 128 (~6.7 ksps).

GM_CYCLE_TIME_256 

Convert every Tconvert x 256 (~3.4 ksps).

GM_CYCLE_TIME_512 

Convert every Tconvert x 512 (~1.7 ksps).

GM_CYCLE_TIME_1024 

Convert every Tconvert x 1024 (~0.9 ksps).

GM_CYCLE_TIME_2048 

Convert every Tconvert x 2048 (~0.4 ksps).

◆ sfe_gmx02b_sensor_t

enum sfe_gmx02b_sensor_t : uint8_t

The Winsen GM-x02B sensing elements carried by the SparkFun Qwiic gas sensor boards.

These are ordinals, not I2C addresses; they index the per-part descriptor table. Use sfe_gmx02b_partInfo() or getDefaultAddress() to obtain a part's factory address.

Note
The onboard ADC101C021 has no product ID register, so the sensing element cannot be read back from the hardware and must be declared by the sketch.
Enumerator
GM_102B 

GM-102B - nitrogen dioxide (NO2). Rs RISES with gas.

GM_202B 

GM-202B - ethanol and smoke.

GM_302B 

GM-302B - alcohol and ethanol vapor.

GM_402B 

GM-402B - methane and propane (combustibles).

GM_502B 

GM-502B - VOCs (ethanol, formaldehyde, toluene).

GM_512B 

GM-512B - breath odor (H2S, ethanol, acetone).

GM_602B 

GM-602B - hydrogen sulfide (H2S) and benzene.

GM_702B 

GM-702B - carbon monoxide (CO) and hydrogen.

GM_802B 

GM-802B - ammonia (NH3).

GM_PART_COUNT 

Number of parts in the family. Not a valid sensor.

GM_UNKNOWN 

No sensor declared or detected.

Function Documentation

◆ sfe_gmx02b_partInfo()

const sfe_gmx02b_part_t * sfe_gmx02b_partInfo ( sfe_gmx02b_sensor_t sensor)

Get the descriptor for a family member.

Parameters
sensorThe sensor variant.
Returns
Pointer to the const descriptor, or nullptr if sensor is not a valid variant.

◆ sfe_gmx02b_partName()

const char * sfe_gmx02b_partName ( sfe_gmx02b_sensor_t sensor)

Get a printable part name.

Parameters
sensorThe sensor variant.
Returns
A string such as "GM-602B", or "Unknown".

◆ sfe_gmx02b_targetGas()

const char * sfe_gmx02b_targetGas ( sfe_gmx02b_sensor_t sensor)

Get a printable target gas description.

Parameters
sensorThe sensor variant.
Returns
A string such as "H2S", or "Unknown".