RumPi 1.0
A self-assembling C++ sensor & actuator library for the Raspberry Pi 5
MQ135.h
Go to the documentation of this file.
1#pragma once
2#include "../Common.h"
3#include "GasSensor.h"
4
5namespace RumPi
6{
16 class MQ135 : public GasSensor
17 {
18 protected:
19 //For details about the parameters below, see:
20 //http://davidegironi.blogspot.com/2014/01/cheap-co2-meter-using-mq135-sensor-with.html
21 //https://hackaday.io/project/3475-sniffing-trinket/log/12363-mq135-arduino-library
22 //Parameters for calculating ppm of CO2 from sensor resistance
23 //These were obtained using power regression based on the graph data.
24 //Use constexpr here for double/float value compile time initialization (non integral types)
25 static constexpr double PARA = 116.6020682;
26 static constexpr double PARB = 2.769034857;
27
28 //Parameters to model temperature and humidity dependence.
29 //These are used to correct the PPM calculations based on the current temperature and humidity.
30 static constexpr double CORA = 0.00035;
31 static constexpr double CORB = 0.02718;
32 static constexpr double CORC = 1.39538;
33 static constexpr double CORD = 0.0018;
34 static constexpr double CORE = -0.003333333;
35 static constexpr double CORF = -0.001923077;
36 static constexpr double CORG = 1.130128205;
37
38 //Atmospheric CO2 level for calibration purposes,
39 //from "Globally averaged marine surface monthly mean data"
40 //available at https://gml.noaa.gov/ccgg/trends/gl_data.html
41 static constexpr double ATMOCO2 = 419.51; // Global CO2 June 2023
42
45
48
51
52 public:
63 MQ135();
64
79 MQ135(IAnalogDigitalConverterComponent* analogToDigitalConverter, const unsigned int& analogToDigitalConverterIndex, const unsigned int& digitalOutputPinNumber);
80
94 MQ135(IAnalogDigitalConverterComponent* analogToDigitalConverter, const unsigned int& analogToDigitalConverterIndex);
95
108 MQ135(const unsigned int& digitalOutputPinNumber);
109
121 virtual ~MQ135();
122
134 void Initialize() override;
135
148 double ReadGasValue() const override;
149
162 double CalculateR0() override;
163
176 double CalculateResistance() const;
177
190 double CalculatePPM() const;
191
207 double CalculateCorrectedR0(double temperatureInC, double humidityPercent);
208
227 double CalculateCorrectionFactor(double temperatureInC, double humidityPercent) const;
228
244 double CalculateCorrectedResistance(double temperatureInC, double humidityPercent) const;
245
261 double CalculateCorrectedPPM(double temperatureInC, double humidityPercent) const;
262
275 std::string ToString() const override;
276
289 double GetCurrentTemperatureInC() const;
290
303 double GetCurrentHumidityPercent() const;
304
317 void SetCurrentTemperatureInC(double currentTemperatureInC);
318
331 void SetCurrentHumidityPercent(double currentHumidityPercent);
332
333 };
334} // namespace RumPi
Base class for the MQ-series gas sensors (MQ2, MQ7, ...).
Definition: GasSensor.h:19
Interface for analog-to-digital converter components (e.g.
Definition: AnalogDigitalConverterComponent.h:14
MQ-135 gas sensor, typically used for detecting Carbon Dioxide (CO2) PPM.
Definition: MQ135.h:17
static constexpr double CORA
Definition: MQ135.h:30
void Initialize() override
Initializes the MQ-135's datasheet values, its default temperature/humidity, and the pre-computed atm...
Definition: MQ135.cpp:101
double CalculateCorrectedR0(double temperatureInC, double humidityPercent)
Gets the corrected resistance R0 of the sensor for calibration purposes, adjusted for temperature and...
Definition: MQ135.cpp:216
double CalculateResistance() const
Gets the resistance of the sensor (its measurement value).
Definition: MQ135.cpp:162
void SetCurrentHumidityPercent(double currentHumidityPercent)
Sets the relative humidity (percent) used to correct the sensor's readings.
Definition: MQ135.cpp:400
double CalculateCorrectedResistance(double temperatureInC, double humidityPercent) const
Gets the sensor's resistance with the measurement value corrected for temperature and humidity.
Definition: MQ135.cpp:270
double myCurrentTemperatureInC
The temperature (in Celsius) used to correct PPM readings for the sensor's temperature dependence.
Definition: MQ135.h:47
double CalculateCorrectionFactor(double temperatureInC, double humidityPercent) const
Gets the correction factor used to compensate the sensor's resistance for temperature and humidity.
Definition: MQ135.cpp:239
double CalculateR0() override
Gets the resistance R0 (RZero) of the sensor for calibration purposes.
Definition: MQ135.cpp:145
MQ135()
Builds a default MQ-135 (carbon dioxide) sensor and initializes its datasheet values.
Definition: MQ135.cpp:15
static constexpr double PARB
Definition: MQ135.h:26
virtual ~MQ135()
Destroys the sensor, turning it off first.
Definition: MQ135.cpp:85
static constexpr double CORG
Definition: MQ135.h:36
double GetCurrentHumidityPercent() const
Gets the relative humidity (percent) currently used to correct the sensor's readings.
Definition: MQ135.cpp:366
static constexpr double PARA
Definition: MQ135.h:25
static constexpr double CORB
Definition: MQ135.h:31
double CalculateCorrectedPPM(double temperatureInC, double humidityPercent) const
Gets the PPM of CO2 sensed (assuming only CO2 in the air), corrected for temperature and humidity.
Definition: MQ135.cpp:298
double ReadGasValue() const override
Reads the current CO2 PPM for the sensor's raw reading (assuming only CO2 in the air).
Definition: MQ135.cpp:128
static constexpr double CORE
Definition: MQ135.h:34
static constexpr double CORC
Definition: MQ135.h:32
double myAtmosphericCO2PPMConstant
Pre-calculated value that does not change; correlates CO2 and PPM based on the graph in the data shee...
Definition: MQ135.h:44
void SetCurrentTemperatureInC(double currentTemperatureInC)
Sets the temperature (in Celsius) used to correct the sensor's readings.
Definition: MQ135.cpp:383
static constexpr double CORF
Definition: MQ135.h:35
double CalculatePPM() const
Gets the PPM of CO2 sensed (assuming only CO2 in the air), from the sensor's resistance ratio.
Definition: MQ135.cpp:187
std::string ToString() const override
Serializes the MQ-135's gas name, raw reading, raw and corrected PPM, temperature,...
Definition: MQ135.cpp:323
double GetCurrentTemperatureInC() const
Gets the temperature (in Celsius) currently used to correct the sensor's readings.
Definition: MQ135.cpp:349
double myCurrentHumidityPercent
The relative humidity (percent) used to correct PPM readings for the sensor's humidity dependence.
Definition: MQ135.h:50
static constexpr double ATMOCO2
Definition: MQ135.h:41
static constexpr double CORD
Definition: MQ135.h:33
RumPi is the library's top-level facade.
Definition: AlertManager.h:6