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

MQ-7 gas sensor, typically used for detecting carbon monoxide. More...

#include <MQ7.h>

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

Public Member Functions

 MQ7 ()
 Builds a default MQ-7 (carbon monoxide) sensor and initializes its datasheet values. More...
 
 MQ7 (IAnalogDigitalConverterComponent *analogToDigitalConverter, const unsigned int &analogToDigitalConverterIndex, const unsigned int &digitalOutputPinNumber)
 Builds an MQ-7 that reads through an ADC and also exposes a digital threshold pin. More...
 
 MQ7 (IAnalogDigitalConverterComponent *analogToDigitalConverter, const unsigned int &analogToDigitalConverterIndex)
 Builds an analog-only MQ-7 that reads through an ADC. More...
 
 MQ7 (const unsigned int &digitalOutputPinNumber)
 Builds a digital-only MQ-7 that reports only its threshold-output pin. More...
 
virtual ~MQ7 ()
 Destroys the sensor, turning it off first. More...
 
void Initialize () override
 Initializes the MQ-7's datasheet values (its carbon monoxide gas curve) and precomputes a PPM value for every possible raw analog reading, since the PPM formula is expensive but the mapping never changes. More...
 
double ReadGasValue () const override
 Reads the current carbon monoxide PPM for the sensor's raw reading (from the precomputed cache). More...
 
double CalculateCarbonMonoxidePPM (const double &sensorGasValueInVolts) const
 Computes the carbon monoxide concentration (PPM) from the sensor's output voltage using an exponential fit of the sensor's response curve. More...
 
std::string ToString () const override
 Serializes the MQ-7's gas name, raw reading, and carbon monoxide PPM to a string. 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...
 

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...
 
- 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...
 

Detailed Description

MQ-7 gas sensor, typically used for detecting carbon monoxide.

Author
Eddie O'Hagan
Date
8/13/2026

Constructor & Destructor Documentation

◆ MQ7() [1/4]

RumPi::MQ7::MQ7 ( )

Builds a default MQ-7 (carbon monoxide) sensor and initializes its datasheet values.

Author
Eddie O'Hagan
Date
8/13/2026
MQ7 carbonMonoxideSensor;
MQ7()
Builds a default MQ-7 (carbon monoxide) sensor and initializes its datasheet values.
Definition: MQ7.cpp:15

◆ MQ7() [2/4]

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

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

Author
Eddie O'Hagan
Date
8/13/2026
MQ7 carbonMonoxideSensor(adc, 5, 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.

◆ MQ7() [3/4]

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

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

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

◆ MQ7() [4/4]

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

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

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

◆ ~MQ7()

RumPi::MQ7::~MQ7 ( )
virtual

Destroys the sensor, turning it off first.

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

Member Function Documentation

◆ CalculateCarbonMonoxidePPM()

double RumPi::MQ7::CalculateCarbonMonoxidePPM ( const double &  sensorGasValueInVolts) const

Computes the carbon monoxide concentration (PPM) from the sensor's output voltage using an exponential fit of the sensor's response curve.

Author
Eddie O'Hagan
Date
8/13/2026
double ppm = carbonMonoxideSensor.CalculateCarbonMonoxidePPM(sensorGasValueInVolts);
Parameters
[in]sensorGasValueInVoltsThe sensor's output voltage.
Returns
The carbon monoxide concentration, in PPM.

◆ Initialize()

void RumPi::MQ7::Initialize ( )
overridevirtual

Initializes the MQ-7's datasheet values (its carbon monoxide gas curve) and precomputes a PPM value for every possible raw analog reading, since the PPM formula is expensive but the mapping never changes.

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

Reimplemented from RumPi::GasSensor.

◆ ReadGasValue()

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

Reads the current carbon monoxide PPM for the sensor's raw reading (from the precomputed cache).

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

Reimplemented from RumPi::GasSensor.

◆ ToString()

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

Serializes the MQ-7's gas name, raw reading, and carbon monoxide PPM to a string.

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

Reimplemented from RumPi::GasSensor.


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