RumPi 1.0
A self-assembling C++ sensor & actuator library for the Raspberry Pi 5
RumPi::MQ135 Class Reference

MQ-135 gas sensor, typically used for detecting Carbon Dioxide (CO2) PPM. More...

#include <MQ135.h>

Inheritance diagram for RumPi::MQ135:
[legend]
Collaboration diagram for RumPi::MQ135:
[legend]

Public Member Functions

 MQ135 ()
 Builds a default MQ-135 (carbon dioxide) sensor and initializes its datasheet values. More...
 
 MQ135 (IAnalogDigitalConverterComponent *analogToDigitalConverter, const unsigned int &analogToDigitalConverterIndex, const unsigned int &digitalOutputPinNumber)
 Builds an MQ-135 that reads through an ADC and also exposes a digital threshold pin. More...
 
 MQ135 (IAnalogDigitalConverterComponent *analogToDigitalConverter, const unsigned int &analogToDigitalConverterIndex)
 Builds an analog-only MQ-135 that reads through an ADC. More...
 
 MQ135 (const unsigned int &digitalOutputPinNumber)
 Builds a digital-only MQ-135 that reports only its threshold-output pin. More...
 
virtual ~MQ135 ()
 Destroys the sensor, turning it off first. More...
 
void Initialize () override
 Initializes the MQ-135's datasheet values, its default temperature/humidity, and the pre-computed atmospheric CO2 constant used to correlate resistance to PPM. More...
 
double ReadGasValue () const override
 Reads the current CO2 PPM for the sensor's raw reading (assuming only CO2 in the air). More...
 
double CalculateR0 () override
 Gets the resistance R0 (RZero) of the sensor for calibration purposes. More...
 
double CalculateResistance () const
 Gets the resistance of the sensor (its measurement value). More...
 
double CalculatePPM () const
 Gets the PPM of CO2 sensed (assuming only CO2 in the air), from the sensor's resistance ratio. More...
 
double CalculateCorrectedR0 (double temperatureInC, double humidityPercent)
 Gets the corrected resistance R0 of the sensor for calibration purposes, adjusted for temperature and humidity. More...
 
double CalculateCorrectionFactor (double temperatureInC, double humidityPercent) const
 Gets the correction factor used to compensate the sensor's resistance for temperature and humidity. More...
 
double CalculateCorrectedResistance (double temperatureInC, double humidityPercent) const
 Gets the sensor's resistance with the measurement value corrected for temperature and humidity. More...
 
double CalculateCorrectedPPM (double temperatureInC, double humidityPercent) const
 Gets the PPM of CO2 sensed (assuming only CO2 in the air), corrected for temperature and humidity. More...
 
std::string ToString () const override
 Serializes the MQ-135's gas name, raw reading, raw and corrected PPM, temperature, and humidity to a string. More...
 
double GetCurrentTemperatureInC () const
 Gets the temperature (in Celsius) currently used to correct the sensor's readings. More...
 
double GetCurrentHumidityPercent () const
 Gets the relative humidity (percent) currently used to correct the sensor's readings. More...
 
void SetCurrentTemperatureInC (double currentTemperatureInC)
 Sets the temperature (in Celsius) used to correct the sensor's readings. More...
 
void SetCurrentHumidityPercent (double currentHumidityPercent)
 Sets the relative humidity (percent) used to correct the sensor's readings. More...
 
- Public Member Functions inherited from RumPi::GasSensor
virtual void CalculateInitialR0 ()
 Calibrates the sensor: computes and stores its clean-air resistance (R0). More...
 
 GasSensor ()
 Builds a default gas sensor with zeroed calibration values and no ADC or digital pin. More...
 
 GasSensor (IAnalogDigitalConverterComponent *analogToDigitalConverter, const EComponentType &gasSensorType, const unsigned int &analogToDigitalConverterIndex, const unsigned int &digitalOutputPinNumber)
 Builds an analog gas sensor that reads through an ADC and also exposes a digital threshold pin. More...
 
 GasSensor (IAnalogDigitalConverterComponent *analogToDigitalConverter, const EComponentType &gasSensorType, const unsigned int &analogToDigitalConverterIndex)
 Builds an analog-only gas sensor that reads through an ADC (no digital threshold pin). More...
 
 GasSensor (const EComponentType &gasSensorType, const unsigned int &digitalOutputPinNumber)
 Builds a digital-only gas sensor that reports only its threshold-output pin (no ADC). More...
 
virtual ~GasSensor ()
 Destroys the gas sensor, turning it off first. More...
 
virtual void TurnOn ()
 Turns the sensor on. More...
 
virtual void TurnOff ()
 Turns the sensor off. More...
 
virtual void Initialize ()
 Initializes the sensor's datasheet values. More...
 
int ReadRawAnalogValue () const
 Reads the raw analog value from the sensor's ADC channel. More...
 
int ReadDigitalValue ()
 Reads the sensor's digital threshold-output pin (1 when the configured gas threshold is exceeded). More...
 
virtual double ReadGasValue () const
 Gets the most recently processed gas value. More...
 
virtual void ProcessRawValues () override
 Reads the sensor and computes the current gas value (PPM) from the RS/R0 ratio using the datasheet's log-log curve. More...
 
virtual std::string ToString () const override
 Serializes the sensor's current gas value (PPM), a rough percentage, and the raw reading to a string. More...
 
unsigned int GetAnalogToDigitalConverterIndex () const
 Gets the ADC channel index this sensor reads from. More...
 
unsigned int GetDigitalOutputPinNumber () const
 Gets the digital threshold-output pin (UINT_MAX if unused). More...
 
double GetInitialValueR0 () const
 Gets the sensor's clean-air resistance (R0) from calibration. More...
 
double GetResistorValueRL () const
 Gets the load resistor value (RL) from the sensor's datasheet. More...
 
double GetCurrentValueRS () const
 Gets the sensor's current resistance (RS). More...
 
const char * GetDetectibleConcentration () const
 Gets a human-readable detectable-concentration range for this sensor. More...
 
EComponentType GetGasSensorType () const
 Gets this sensor's component type (which MQ sensor it is). More...
 
const char * GetGasSensorTypeAsString () const
 Gets this sensor's type as a display string (e.g. More...
 
const char * GetGasSensorTypeAsString (const EComponentType &gasSensorType) const
 Maps a gas-sensor component type to its display string (e.g. More...
 
IAnalogDigitalConverterComponentGetAnalogDigitalConverter () const
 Gets the ADC this sensor reads its analog value through. More...
 
const GasDataGetGasData () const
 Gets the per-gas curve data (slope/intercept and concentration points) for this sensor. More...
 
- Public Member Functions inherited from RumPi::BaseComponent
 BaseComponent ()
 Builds a base component with an unknown type and an empty name. More...
 
virtual ~BaseComponent ()
 Virtual destructor so deleting a component through a BaseComponent* also cleans up the concrete subclass. More...
 
virtual void TurnOn ()=0
 Turns the component on (e.g. More...
 
virtual void TurnOff ()=0
 Turns the component off. More...
 
virtual std::string ToString () const =0
 Serializes the component's current readings to a string (usually JSON). More...
 
virtual void ProcessRawValues ()=0
 Reads and processes the component's latest raw values (called once per poll). More...
 
virtual EComponentType GetComponentType () const
 Gets the component's type. More...
 
const std::string & GetName () const
 Gets the component's unique per-instance name. More...
 
void SetName (const std::string &name)
 Sets the component's unique per-instance name. More...
 

Protected Attributes

double myAtmosphericCO2PPMConstant
 Pre-calculated value that does not change; correlates CO2 and PPM based on the graph in the data sheet. More...
 
double myCurrentTemperatureInC
 The temperature (in Celsius) used to correct PPM readings for the sensor's temperature dependence. More...
 
double myCurrentHumidityPercent
 The relative humidity (percent) used to correct PPM readings for the sensor's humidity dependence. More...
 
- Protected Attributes inherited from RumPi::GasSensor
unsigned int myAnalogToDigitalConverterIndex
 The ADC channel index this sensor reads its analog value from. More...
 
unsigned int myDigitalOutputPinNumber
 The digital output (threshold) pin, or UINT_MAX if unused. More...
 
double myInitialValueR0
 The sensor's resistance in clean air (R0), from calibration. More...
 
double myResistorValueRL
 The load resistor value (RL) from the sensor's datasheet. More...
 
double myCurrentValueRS
 The sensor's current resistance (RS). More...
 
double myCleanAirFactor
 The clean-air factor (RS/R0 in clean air) from the datasheet. More...
 
double myRawGasSensorValue
 The most recent raw analog reading. More...
 
double myCurrentGasValue
 The most recent computed gas value (PPM). More...
 
const char * myDetectibleConcentration
 A human-readable detectable-concentration range for this sensor. More...
 
IAnalogDigitalConverterComponentmyAnalogToDigitalConverter
 The ADC this sensor reads its analog value through. More...
 
GasData myGasData
 Per-gas curve data (slope/intercept) used to convert the RS/R0 ratio to PPM. More...
 
- Protected Attributes inherited from RumPi::BaseComponent
EComponentType myComponentType
 The kind of component this is (set by each concrete subclass). More...
 
std::string myName
 A unique per-instance name, assigned when the component is added to the manager. More...
 

Static Protected Attributes

static constexpr double PARA = 116.6020682
 
static constexpr double PARB = 2.769034857
 
static constexpr double CORA = 0.00035
 
static constexpr double CORB = 0.02718
 
static constexpr double CORC = 1.39538
 
static constexpr double CORD = 0.0018
 
static constexpr double CORE = -0.003333333
 
static constexpr double CORF = -0.001923077
 
static constexpr double CORG = 1.130128205
 
static constexpr double ATMOCO2 = 419.51
 

Additional Inherited Members

- Static Public Member Functions inherited from RumPi::BaseComponent
static std::string ComponentTypeToString (const EComponentType theComponentTypeToConvert)
 Maps an EComponentType to its string name. More...
 
static EComponentType StringToComponentType (const std::string &theComponentTypeName)
 Maps a type name back to its EComponentType (the inverse of ComponentTypeToString), so the client can turn the type carried on the wire back into a value it can look an icon up with. More...
 
- Static Public Attributes inherited from RumPi::GasSensor
static constexpr double GAS_SENSOR_VOLTAGE = 5.0
 The supply voltage the gas sensor runs on. More...
 
static const int CALIBRATION_SAMPLE_TIMES = 200
 The number of samples taken during the calibration phase. More...
 
static const int CALIBRATION_SAMPLE_INTERVAL = 250
 Milliseconds between samples during the calibration phase. More...
 
static const int READ_SAMPLE_INTERVAL = 250
 Milliseconds between samples during normal operation. More...
 
- Protected Member Functions inherited from RumPi::GasSensor
virtual double CalculateR0 ()
 Samples the sensor many times in clean air, averages the reading, converts it to resistance (RS), and divides by the clean-air factor to get R0. More...
 
virtual double CalculateRS (const double &sensorValueInVolts) const
 Computes the sensor's resistance (RS) from its output voltage, using the load resistor RL and supply voltage. More...
 
virtual double CalculateRSR0Ratio (const double &sensorValueInVolts) const
 Computes the RS/R0 ratio (used to look up PPM on the datasheet curve) from the sensor's output voltage. More...
 
double ConvertRawSensorValueToVolts (const double &rawGasSensorValue) const
 Converts a raw analog reading to its equivalent voltage (using the sensor's supply voltage and the ADC's maximum value). More...
 
double ConvertVoltsToRawSensorValue (const double &volts) const
 Converts a voltage back to its equivalent raw analog value (the inverse of ConvertRawSensorValueToVolts). More...
 
double GetAnalogToDigitalConverterMaxRawValue () const
 Gets the ADC's full-scale raw count for scaling raw readings to volts (PCF8591 = 255 8-bit, MCP3008 = 1023 10-bit), falling back to the 10-bit convention when no ADC is wired. More...
 
double GetAnalogToDigitalConverterVoltage () const
 Gets the ADC's reference voltage for scaling raw readings to volts (defined by the wired ADC, e.g. More...
 

Detailed Description

MQ-135 gas sensor, typically used for detecting Carbon Dioxide (CO2) PPM.

Original algorithm for CO2 detection on the MQ-135 is from G. Krocker (Mad Frog Labs), ViliusKraujutis, and NuclearPhoenixx. Source: https://www.arduino.cc/reference/en/libraries/mq135/

Author
Eddie O'Hagan
Date
8/13/2026

Constructor & Destructor Documentation

◆ MQ135() [1/4]

RumPi::MQ135::MQ135 ( )

Builds a default MQ-135 (carbon dioxide) sensor and initializes its datasheet values.

Author
Eddie O'Hagan
Date
8/13/2026
MQ135 carbonDioxideSensor;
MQ135()
Builds a default MQ-135 (carbon dioxide) sensor and initializes its datasheet values.
Definition: MQ135.cpp:15

◆ MQ135() [2/4]

RumPi::MQ135::MQ135 ( IAnalogDigitalConverterComponent analogToDigitalConverter,
const unsigned int &  analogToDigitalConverterIndex,
const unsigned int &  digitalOutputPinNumber 
)

Builds an MQ-135 that reads through an ADC and also exposes a digital threshold pin.

Author
Eddie O'Hagan
Date
8/13/2026
MQ135 carbonDioxideSensor(adc, 0, PIN_GPIO_27);
constexpr unsigned int PIN_GPIO_27
BCM GPIO pin 27.
Definition: Common.h:53
Parameters
[in]analogToDigitalConverterThe ADC the sensor reads through.
[in]analogToDigitalConverterIndexThe ADC channel index.
[in]digitalOutputPinNumberThe digital threshold-output pin.

◆ MQ135() [3/4]

RumPi::MQ135::MQ135 ( IAnalogDigitalConverterComponent analogToDigitalConverter,
const unsigned int &  analogToDigitalConverterIndex 
)

Builds an analog-only MQ-135 that reads through an ADC.

Author
Eddie O'Hagan
Date
8/13/2026
MQ135 carbonDioxideSensor(adc, 0);
Parameters
[in]analogToDigitalConverterThe ADC the sensor reads through.
[in]analogToDigitalConverterIndexThe ADC channel index.

◆ MQ135() [4/4]

RumPi::MQ135::MQ135 ( const unsigned int &  digitalOutputPinNumber)

Builds a digital-only MQ-135 that reports only its threshold-output pin.

Author
Eddie O'Hagan
Date
8/13/2026
MQ135 carbonDioxideSensor(PIN_GPIO_27);
Parameters
[in]digitalOutputPinNumberThe digital threshold-output pin.

◆ ~MQ135()

RumPi::MQ135::~MQ135 ( )
virtual

Destroys the sensor, turning it off first.

Author
Eddie O'Hagan
Date
8/13/2026
MQ135* sensor = new MQ135();
delete sensor;

Member Function Documentation

◆ CalculateCorrectedPPM()

double RumPi::MQ135::CalculateCorrectedPPM ( double  temperatureInC,
double  humidityPercent 
) const

Gets the PPM of CO2 sensed (assuming only CO2 in the air), corrected for temperature and humidity.

Author
Eddie O'Hagan
Date
8/13/2026
double ppm = carbonDioxideSensor.CalculateCorrectedPPM(21.0, 25.0);
Parameters
[in]temperatureInCThe current temperature, in Celsius.
[in]humidityPercentThe current relative humidity, as a percent.
Returns
The temperature/humidity-corrected CO2 concentration, in PPM.

◆ CalculateCorrectedR0()

double RumPi::MQ135::CalculateCorrectedR0 ( double  temperatureInC,
double  humidityPercent 
)

Gets the corrected resistance R0 of the sensor for calibration purposes, adjusted for temperature and humidity.

Author
Eddie O'Hagan
Date
8/13/2026
double correctedR0 = carbonDioxideSensor.CalculateCorrectedR0(21.0, 25.0);
Parameters
[in]temperatureInCThe current temperature, in Celsius.
[in]humidityPercentThe current relative humidity, as a percent.
Returns
The corrected calibration resistance R0, in kOhm.

◆ CalculateCorrectedResistance()

double RumPi::MQ135::CalculateCorrectedResistance ( double  temperatureInC,
double  humidityPercent 
) const

Gets the sensor's resistance with the measurement value corrected for temperature and humidity.

Author
Eddie O'Hagan
Date
8/13/2026
double corrected = carbonDioxideSensor.CalculateCorrectedResistance(21.0, 25.0);
Parameters
[in]temperatureInCThe current temperature, in Celsius.
[in]humidityPercentThe current relative humidity, as a percent.
Returns
The corrected sensor resistance, in kOhm.

◆ CalculateCorrectionFactor()

double RumPi::MQ135::CalculateCorrectionFactor ( double  temperatureInC,
double  humidityPercent 
) const

Gets the correction factor used to compensate the sensor's resistance for temperature and humidity.

The temperature dependency curve is linearized under and above 20 degrees C, assuming a linear dependency on humidity: below 20C fact = a*t*t - b*t + c - (h - 33)*d; above 20C fact = e*t + f*h + g.

Author
Eddie O'Hagan
Date
8/13/2026
double factor = carbonDioxideSensor.CalculateCorrectionFactor(21.0, 25.0);
Parameters
[in]temperatureInCThe current temperature, in Celsius.
[in]humidityPercentThe current relative humidity, as a percent.
Returns
The temperature/humidity correction factor.

◆ CalculatePPM()

double RumPi::MQ135::CalculatePPM ( ) const

Gets the PPM of CO2 sensed (assuming only CO2 in the air), from the sensor's resistance ratio.

Author
Eddie O'Hagan
Date
8/13/2026
double ppm = carbonDioxideSensor.CalculatePPM();
Returns
The CO2 concentration, in PPM.

◆ CalculateR0()

double RumPi::MQ135::CalculateR0 ( )
overridevirtual

Gets the resistance R0 (RZero) of the sensor for calibration purposes.

Author
Eddie O'Hagan
Date
8/13/2026
double r0 = carbonDioxideSensor.CalculateR0();
Returns
The calibration resistance R0, in kOhm.

Reimplemented from RumPi::GasSensor.

◆ CalculateResistance()

double RumPi::MQ135::CalculateResistance ( ) const

Gets the resistance of the sensor (its measurement value).

Author
Eddie O'Hagan
Date
8/13/2026
double resistance = carbonDioxideSensor.CalculateResistance();
Returns
The sensor resistance, in kOhm.

◆ GetCurrentHumidityPercent()

double RumPi::MQ135::GetCurrentHumidityPercent ( ) const

Gets the relative humidity (percent) currently used to correct the sensor's readings.

Author
Eddie O'Hagan
Date
8/13/2026
double humidity = carbonDioxideSensor.GetCurrentHumidityPercent();
Returns
The current relative humidity, as a percent.

◆ GetCurrentTemperatureInC()

double RumPi::MQ135::GetCurrentTemperatureInC ( ) const

Gets the temperature (in Celsius) currently used to correct the sensor's readings.

Author
Eddie O'Hagan
Date
8/13/2026
double temperature = carbonDioxideSensor.GetCurrentTemperatureInC();
Returns
The current temperature, in Celsius.

◆ Initialize()

void RumPi::MQ135::Initialize ( )
overridevirtual

Initializes the MQ-135's datasheet values, its default temperature/humidity, and the pre-computed atmospheric CO2 constant used to correlate resistance to PPM.

Author
Eddie O'Hagan
Date
8/13/2026
//Called by every MQ135 constructor.

Reimplemented from RumPi::GasSensor.

◆ ReadGasValue()

double RumPi::MQ135::ReadGasValue ( ) const
overridevirtual

Reads the current CO2 PPM for the sensor's raw reading (assuming only CO2 in the air).

Author
Eddie O'Hagan
Date
8/13/2026
double ppm = carbonDioxideSensor.ReadGasValue();
Returns
The current CO2 concentration, in PPM.

Reimplemented from RumPi::GasSensor.

◆ SetCurrentHumidityPercent()

void RumPi::MQ135::SetCurrentHumidityPercent ( double  currentHumidityPercent)

Sets the relative humidity (percent) used to correct the sensor's readings.

Author
Eddie O'Hagan
Date
8/13/2026
carbonDioxideSensor.SetCurrentHumidityPercent(40.0);
Parameters
[in]currentHumidityPercentThe relative humidity to use, as a percent.

◆ SetCurrentTemperatureInC()

void RumPi::MQ135::SetCurrentTemperatureInC ( double  currentTemperatureInC)

Sets the temperature (in Celsius) used to correct the sensor's readings.

Author
Eddie O'Hagan
Date
8/13/2026
carbonDioxideSensor.SetCurrentTemperatureInC(23.5);
Parameters
[in]currentTemperatureInCThe temperature to use, in Celsius.

◆ ToString()

std::string RumPi::MQ135::ToString ( ) const
overridevirtual

Serializes the MQ-135's gas name, raw reading, raw and corrected PPM, temperature, and humidity to a string.

Author
Eddie O'Hagan
Date
8/13/2026
std::string data = carbonDioxideSensor.ToString();
Returns
A multi-line string describing the sensor's reading.

Reimplemented from RumPi::GasSensor.

Member Data Documentation

◆ ATMOCO2

constexpr double RumPi::MQ135::ATMOCO2 = 419.51
staticconstexprprotected

◆ CORA

constexpr double RumPi::MQ135::CORA = 0.00035
staticconstexprprotected

◆ CORB

constexpr double RumPi::MQ135::CORB = 0.02718
staticconstexprprotected

◆ CORC

constexpr double RumPi::MQ135::CORC = 1.39538
staticconstexprprotected

◆ CORD

constexpr double RumPi::MQ135::CORD = 0.0018
staticconstexprprotected

◆ CORE

constexpr double RumPi::MQ135::CORE = -0.003333333
staticconstexprprotected

◆ CORF

constexpr double RumPi::MQ135::CORF = -0.001923077
staticconstexprprotected

◆ CORG

constexpr double RumPi::MQ135::CORG = 1.130128205
staticconstexprprotected

◆ myAtmosphericCO2PPMConstant

double RumPi::MQ135::myAtmosphericCO2PPMConstant
protected

Pre-calculated value that does not change; correlates CO2 and PPM based on the graph in the data sheet.

◆ myCurrentHumidityPercent

double RumPi::MQ135::myCurrentHumidityPercent
protected

The relative humidity (percent) used to correct PPM readings for the sensor's humidity dependence.

◆ myCurrentTemperatureInC

double RumPi::MQ135::myCurrentTemperatureInC
protected

The temperature (in Celsius) used to correct PPM readings for the sensor's temperature dependence.

◆ PARA

constexpr double RumPi::MQ135::PARA = 116.6020682
staticconstexprprotected

◆ PARB

constexpr double RumPi::MQ135::PARB = 2.769034857
staticconstexprprotected

The documentation for this class was generated from the following files: