Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions MQ135.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ the datasheet but the information there seems to be wrong.
@section HISTORY

v1.0 - First release
v1.1 - Add rzero param in constructor,
correcting atmospheric CO2 level for 2019 year
*/
/**************************************************************************/

Expand All @@ -20,20 +22,20 @@ v1.0 - First release
/*!
@brief Default constructor

@param[in] pin The analog input pin for the readout of the sensor
@param[in] pin The analog input pin for the readout of the sensor
@param[in] rzero Calibration resistance at atmospheric CO2 level
*/
/**************************************************************************/

MQ135::MQ135(uint8_t pin) {
MQ135::MQ135(uint8_t pin, float rzero) {
_rzero = rzero;
_pin = pin;
}


/**************************************************************************/
/*!
@brief Get the correction factor to correct for temperature and humidity

@param[in] t The ambient air temperature
@param[in] t The ambient air temperature in Celsius
@param[in] h The relative humidity

@return The calculated correction factor
Expand All @@ -60,7 +62,7 @@ float MQ135::getResistance() {
@brief Get the resistance of the sensor, ie. the measurement value corrected
for temp/hum

@param[in] t The ambient air temperature
@param[in] t The ambient air temperature in Celsius
@param[in] h The relative humidity

@return The corrected sensor resistance kOhm
Expand All @@ -78,22 +80,22 @@ float MQ135::getCorrectedResistance(float t, float h) {
*/
/**************************************************************************/
float MQ135::getPPM() {
return PARA * pow((getResistance()/RZERO), -PARB);
return PARA * pow((getResistance()/_rzero), -PARB);
}

/**************************************************************************/
/*!
@brief Get the ppm of CO2 sensed (assuming only CO2 in the air), corrected
for temp/hum

@param[in] t The ambient air temperature
@param[in] t The ambient air temperature in Celsius
@param[in] h The relative humidity

@return The ppm of CO2 in the air
*/
/**************************************************************************/
float MQ135::getCorrectedPPM(float t, float h) {
return PARA * pow((getCorrectedResistance(t, h)/RZERO), -PARB);
return PARA * pow((getCorrectedResistance(t, h)/_rzero), -PARB);
}

/**************************************************************************/
Expand All @@ -112,7 +114,7 @@ float MQ135::getRZero() {
@brief Get the corrected resistance RZero of the sensor for calibration
purposes

@param[in] t The ambient air temperature
@param[in] t The ambient air temperature in Celsius
@param[in] h The relative humidity

@return The corrected sensor resistance RZero in kOhm
Expand Down
11 changes: 7 additions & 4 deletions MQ135.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ the datasheet but the information there seems to be wrong.
@section HISTORY

v1.0 - First release
v1.1 - Add rzero param in constructor,
correcting atmospheric CO2 level for 2019 year
*/
/**************************************************************************/
#ifndef MQ135_H
Expand All @@ -23,8 +25,7 @@ v1.0 - First release

/// The load resistance on the board
#define RLOAD 10.0
/// Calibration resistance at atmospheric CO2 level
#define RZERO 76.63

/// Parameters for calculating ppm of CO2 from sensor resistance
#define PARA 116.6020682
#define PARB 2.769034857
Expand All @@ -36,14 +37,16 @@ v1.0 - First release
#define CORD 0.0018

/// Atmospheric CO2 level for calibration purposes
#define ATMOCO2 397.13
/// On august 2019 from https://www.co2.earth
#define ATMOCO2 409.95

class MQ135 {
private:
uint8_t _pin;
float _rzero;

public:
MQ135(uint8_t pin);
MQ135(uint8_t pin, float rzero=76.63);
float getCorrectionFactor(float t, float h);
float getResistance();
float getCorrectedResistance(float t, float h);
Expand Down