diff --git a/MQ135.cpp b/MQ135.cpp old mode 100755 new mode 100644 index 676ed31..70d5393 --- a/MQ135.cpp +++ b/MQ135.cpp @@ -27,49 +27,66 @@ v1.0 - First release MQ135::MQ135(uint8_t pin) { _pin = pin; } - - /**************************************************************************/ /*! -@brief Get the correction factor to correct for temperature and humidity - -@param[in] t The ambient air temperature -@param[in] h The relative humidity +@brief Get the resistance of the sensor, ie. the measurement value -@return The calculated correction factor +@return The sensor resistance in Ohms */ /**************************************************************************/ -float MQ135::getCorrectionFactor(float t, float h) { - return CORA * t * t - CORB * t + CORC - (h-33.)*CORD; +float MQ135::getResistance() { + int val = analogRead(_pin); + return ((1024./(float)val) * 5. - 1.)*RLOAD; } - /**************************************************************************/ /*! -@brief Get the resistance of the sensor, ie. the measurement value - -@return The sensor resistance in kOhm +@brief Get the ppm of CO sensed +@return ppm value of CO in air */ /**************************************************************************/ -float MQ135::getResistance() { - int val = analogRead(_pin); - return ((1023./(float)val) * 5. - 1.)*RLOAD; +float MQ135::getCOPPM() { + return scaleFactorCO * pow((getResistance()/r0CO), -exponentCO); } +/**************************************************************************/ +/*! +@brief Get the ppm of CO2 sensed (assuming only CO2 in the air) +@return The ppm of CO2 in the air +*/ +/**************************************************************************/ +float MQ135::getCO2PPM() { + return scaleFactorCO2 * pow((getResistance()/r0CO2), -exponentCO2); +} /**************************************************************************/ /*! -@brief Get the resistance of the sensor, ie. the measurement value corrected - for temp/hum +@brief Get the ppm of Ethanol sensed -@param[in] t The ambient air temperature -@param[in] h The relative humidity +@return The ppm of Ethanol in the air +*/ +/**************************************************************************/ +float MQ135::getEthanolPPM() { + return scaleFactorEthanol * pow((getResistance()/r0Ethanol), -exponentEthanol); +} +/**************************************************************************/ +/*! +@brief Get the ppm of NH4 sensed -@return The corrected sensor resistance kOhm +@return The ppm of NH4 in the air */ /**************************************************************************/ -float MQ135::getCorrectedResistance(float t, float h) { - return getResistance()/getCorrectionFactor(t, h); +float MQ135::getNH4PPM() { + return scaleFactorNH4 * pow((getResistance()/r0NH4), -exponentNH4); } +/**************************************************************************/ +/*! +@brief Get the ppm of CO2 sensed (assuming only CO2 in the air) +@return The ppm of CO2 in the air +*/ +/**************************************************************************/ +float MQ135::getToluenePPM() { + return scaleFactorToluene * pow((getResistance()/r0Toluene), -exponentToluene); +} /**************************************************************************/ /*! @brief Get the ppm of CO2 sensed (assuming only CO2 in the air) @@ -77,25 +94,59 @@ float MQ135::getCorrectedResistance(float t, float h) { @return The ppm of CO2 in the air */ /**************************************************************************/ -float MQ135::getPPM() { - return PARA * pow((getResistance()/RZERO), -PARB); +float MQ135::getAcetonePPM() { + return scaleFactorAcetone * pow((getResistance()/r0Acetone), -exponentAcetone); } +/**************************************************************************/ +/*! +@brief Get the resistance RZero of the sensor for calibration purposes +@return The sensor resistance RZero in kOhm +*/ +/**************************************************************************/ +float MQ135::getRZeroCO() { + return getResistance() * pow((atmCO/scaleFactorCO), (1./exponentCO)); +} /**************************************************************************/ /*! -@brief Get the ppm of CO2 sensed (assuming only CO2 in the air), corrected - for temp/hum +@brief Get the resistance RZero of the sensor for calibration purposes -@param[in] t The ambient air temperature -@param[in] h The relative humidity +@return The sensor resistance RZero in kOhm +*/ +/**************************************************************************/ +float MQ135::getRZeroCO2() { + return getResistance() * pow((atmCO2/scaleFactorCO2), (1./exponentCO2)); +} +/**************************************************************************/ +/*! +@brief Get the resistance RZero of the sensor for calibration purposes -@return The ppm of CO2 in the air +@return The sensor resistance RZero in kOhm +*/ +/**************************************************************************/ +float MQ135::getRZeroEthanol() { + return getResistance() * pow((atmEthanol/scaleFactorEthanol), (1./exponentEthanol)); +} +/**************************************************************************/ +/*! +@brief Get the resistance RZero of the sensor for calibration purposes + +@return The sensor resistance RZero in kOhm */ /**************************************************************************/ -float MQ135::getCorrectedPPM(float t, float h) { - return PARA * pow((getCorrectedResistance(t, h)/RZERO), -PARB); +float MQ135::getRZeroNH4() { + return getResistance() * pow((atmNH4/scaleFactorNH4), (1./exponentNH4)); } +/**************************************************************************/ +/*! +@brief Get the resistance RZero of the sensor for calibration purposes +@return The sensor resistance RZero in kOhm +*/ +/**************************************************************************/ +float MQ135::getRZeroToluene() { + return getResistance() * pow((atmToluene/scaleFactorToluene), (1./exponentToluene)); +} /**************************************************************************/ /*! @brief Get the resistance RZero of the sensor for calibration purposes @@ -103,21 +154,111 @@ float MQ135::getCorrectedPPM(float t, float h) { @return The sensor resistance RZero in kOhm */ /**************************************************************************/ -float MQ135::getRZero() { - return getResistance() * pow((ATMOCO2/PARA), (1./PARB)); +float MQ135::getRZeroAcetone() { + return getResistance() * pow((atmAcetone/scaleFactorAcetone), (1./exponentAcetone)); } +/**************************************************************************/ +/*! +@brief Get the ppm of CO sensed +@return ppm value of CO in air +*/ +/**************************************************************************/ +float MQ135::getCO(float res) { + return scaleFactorCO * pow((res/r0CO), -exponentCO); +} +/**************************************************************************/ +/*! +@brief Get the ppm of CO2 sensed (assuming only CO2 in the air) +@return The ppm of CO2 in the air +*/ +/**************************************************************************/ +float MQ135::getCO2(float res) { + return scaleFactorCO2 * pow((res/r0CO2), -exponentCO2); +} /**************************************************************************/ /*! -@brief Get the corrected resistance RZero of the sensor for calibration - purposes +@brief Get the ppm of Ethanol sensed -@param[in] t The ambient air temperature -@param[in] h The relative humidity +@return The ppm of Ethanol in the air +*/ +/**************************************************************************/ +float MQ135::getEthanol(float res) { + return scaleFactorEthanol * pow((res/r0Ethanol), -exponentEthanol); +} +/**************************************************************************/ +/*! +@brief Get the ppm of NH4 sensed -@return The corrected sensor resistance RZero in kOhm +@return The ppm of NH4 in the air */ /**************************************************************************/ -float MQ135::getCorrectedRZero(float t, float h) { - return getCorrectedResistance(t, h) * pow((ATMOCO2/PARA), (1./PARB)); +float MQ135::getNH4(float res) { + return scaleFactorNH4 * pow((res/r0NH4), -exponentNH4); +} +/**************************************************************************/ +/*! +@brief Get the ppm of CO2 sensed (assuming only CO2 in the air) + +@return The ppm of CO2 in the air +*/ +/**************************************************************************/ +float MQ135::getToluene(float res) { + return scaleFactorToluene * pow((res/r0Toluene), -exponentToluene); +} +/**************************************************************************/ +/*! +@brief Get the ppm of CO2 sensed (assuming only CO2 in the air) + +@return The ppm of CO2 in the air +*/ +/**************************************************************************/ +float MQ135::getAcetone(float res) { + return scaleFactorAcetone * pow((res/r0Acetone), -exponentAcetone); +} +/* RETURN THE RZERO WITH A PARAMETER OF RESISTANCE*/ +float MQ135::getCorrectedRZero(float r) { + return r * pow((atmCO2/scaleFactorCO2), (1./exponentCO2)); +} +float MQ135::getCorrectedRZeroCO(float r) { + return r * pow((atmCO/scaleFactorCO), (1./exponentCO)); +} +float MQ135::getCorrectedRZeroEthanol(float r) { + return r * pow((atmEthanol/scaleFactorEthanol), (1./exponentEthanol)); +} +float MQ135::getCorrectedRZeroNH4(float r) { + return r * pow((atmNH4/scaleFactorNH4), (1./exponentNH4)); +} +float MQ135::getCorrectedRZeroToluene(float r) { + return r * pow((atmToluene/scaleFactorToluene), (1./exponentToluene)); +} +float MQ135::getCorrectedRZeroAcetone(float r) { + return r * pow((atmAcetone/scaleFactorAcetone), (1./exponentAcetone)); +} +/*CORRECTED RESISTANCE*/ +float MQ135::getCorrectedResistance(float t, float h) { + return getResistance()/getCorrectionFactor(t, h); +} +/*CORRECTION FACTOR*/ +float MQ135::getCorrectionFactor(float t, float h) { + return CORA * t * t - CORB * t + CORC - (h-33.)*CORD; +} + +float MQ135::getCalibratedCO2(float t, float h){ + return scaleFactorCO2 * pow((getCorrectedResistance(t, h)/getCorrectedRZero(getCorrectedResistance(t, h))), -exponentCO2); +} +float MQ135::getCalibratedCO(float t, float h) { + return scaleFactorCO * pow((getCorrectedResistance(t, h)/getCorrectedRZeroCO(getCorrectedResistance(t, h))), -exponentCO); +} +float MQ135::getCalibratedEthanol(float t, float h) { + return scaleFactorEthanol * pow((getCorrectedResistance(t, h)/getCorrectedRZeroEthanol(getCorrectedResistance(t, h))), -exponentEthanol); +} +float MQ135::getCalibratedNH4(float t, float h) { + return scaleFactorNH4 * pow((getCorrectedResistance(t, h)/getCorrectedRZeroNH4(getCorrectedResistance(t, h))), -exponentNH4); +} +float MQ135::getCalibratedToluene(float t, float h) { + return scaleFactorToluene * pow((getCorrectedResistance(t, h)/getCorrectedRZeroToluene(getCorrectedResistance(t, h))), -exponentToluene); +} +float MQ135::getCalibratedAcetone(float t, float h) { + return scaleFactorAcetone * pow((getCorrectedResistance(t, h)/getCorrectedRZeroAcetone(getCorrectedResistance(t, h))), -exponentAcetone); } diff --git a/MQ135.h b/MQ135.h old mode 100755 new mode 100644 index 22cefec..6304556 --- a/MQ135.h +++ b/MQ135.h @@ -1,55 +1,125 @@ -/**************************************************************************/ -/*! -@file MQ135.h -@author G.Krocker (Mad Frog Labs) -@license GNU GPLv3 - -First version of an Arduino Library for the MQ135 gas sensor -TODO: Review the correction factor calculation. This currently relies on -the datasheet but the information there seems to be wrong. - -@section HISTORY - -v1.0 - First release -*/ -/**************************************************************************/ -#ifndef MQ135_H -#define MQ135_H -#if ARDUINO >= 100 - #include "Arduino.h" -#else - #include "WProgram.h" -#endif - -/// 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 - -/// Parameters to model temperature and humidity dependence -#define CORA 0.00035 -#define CORB 0.02718 -#define CORC 1.39538 -#define CORD 0.0018 - -/// Atmospheric CO2 level for calibration purposes -#define ATMOCO2 397.13 - -class MQ135 { - private: - uint8_t _pin; - - public: - MQ135(uint8_t pin); - float getCorrectionFactor(float t, float h); - float getResistance(); - float getCorrectedResistance(float t, float h); - float getPPM(); - float getCorrectedPPM(float t, float h); - float getRZero(); - float getCorrectedRZero(float t, float h); -}; -#endif +/**************************************************************************/ +/*! +@file MQ135.h +@author G.Krocker (Mad Frog Labs) +@license GNU GPLv3 + +First version of an Arduino Library for the MQ135 gas sensor +TODO: Review the correction factor calculation. This currently relies on +the datasheet but the information there seems to be wrong. + +@section HISTORY + +v1.0 - First release +*/ +/**************************************************************************/ +#ifndef MQ135_H +#define MQ135_H +#if ARDUINO >= 100 + #include "Arduino.h" +#else + #include "WProgram.h" +#endif + +/// To convert readed resistance into ohms +#define RLOAD 10.0 +/// R0 for AIR +#define r0Air 1 +/// R0 for CO **measured with 24hrs of exposure** +#define r0CO 69.65 +/// R0 for CO2 **realized 24 hrs of exposure** +#define r0CO2 553.232 +/// R0 for Ethanol **measured with 24hrs of exposure** +#define r0Ethanol 240.293 +/// R0 for Ammonium **measured with 24hrs of exposure** +#define r0NH4 164.8282 +/// R0 for Toluene **measured with 24hrs of exposure** +#define r0Toluene 130.726 +/// R0 for Acetone **measured with 24hrs of exposure** +#define r0Acetone 224.6261 +/// Parameters Equation for CO +#define scaleFactorCO 662.9382 +#define exponentCO 4.0241 +/// Parameters Equation for CO2 +#define scaleFactorCO2 116.6020682 +#define exponentCO2 2.769034857 +/// Paremeters Equation for Ethanol +#define scaleFactorEthanol 75.3103 +#define exponentEthanol 3.1459 +/// Parameters Equation for NH4 +#define scaleFactorNH4 102.694 +#define exponentNH4 2.48818 +/// Parameters Equation for Toluene +#define scaleFactorToluene 43.7748 +#define exponentToluene 3.42936 +/// Parameters Equation for Acetone +#define scaleFactorAcetone 33.1197 +#define exponentAcetone 3.36587 +/// Parameters to model temperature and humidity dependence +#define CORA 0.00035 +#define CORB 0.02718 +#define CORC 1.39538 +#define CORD 0.0018 +/// Atmospheric CO Level for calibration purposes +#define atmCO 1 +/// Atmospheric CO2 level for calibration purposes +#define atmCO2 407.57 +/// Atmospheric Ethanol Level for calibration purposes https://www.mathesongas.com/pdfs/msds/00224106.pdf +#define atmEthanol 22.5 +/// Atmospheric NH4 level for calibration purposes +#define atmNH4 15 +/// Atmospheric Toluene level for calibration purposes +#define atmToluene 2.9 +/// Atmospheric Acetone level for calibration purposes +#define atmAcetone 16 + +class MQ135 { + private: + uint8_t _pin; + + public: + MQ135(uint8_t pin); + float getResistance(); + + float getCOPPM(); + float getCO2PPM(); + float getEthanolPPM(); + float getNH4PPM(); + float getToluenePPM(); + float getAcetonePPM(); + + float getRZeroCO(); + float getRZeroCO2(); + float getRZeroEthanol(); + float getRZeroNH4(); + float getRZeroToluene(); + float getRZeroAcetone(); + + float getCO(float res); + float getCO2(float res); + float getEthanol(float res); + float getNH4(float res); + float getToluene(float res); + float getAcetone(float res); + + + float getCorrectedRZero(float r); + float getCorrectedRZeroCO(float r); + float getCorrectedRZeroEthanol(float r); + float getCorrectedRZeroNH4(float r); + float getCorrectedRZeroToluene(float r); + float getCorrectedRZeroAcetone(float r); + + float getCorrectedResistance(float t, float h); + float getCorrectionFactor(float t, float h); + + float getCalibratedCO2(float t, float h); + float getCalibratedCO(float t, float h); + float getCalibratedEthanol(float t, float h); + float getCalibratedNH4(float t, float h); + float getCalibratedToluene(float t, float h); + float getCalibratedAcetone(float t, float h); + + +}; +#endif