Compare commits

..

No commits in common. "feature/improve-calculations" and "master" have entirely different histories.

3 changed files with 18 additions and 65 deletions

View File

@ -20,22 +20,12 @@ v1.0 - First release
/*!
@brief Default constructor
@param[in] pin The analog input pin for the readout of the sensor
@param[in] rload Load resistance on board, in KOhms
@param[in] rzero Calibration sensor resistance at atmospheric CO2 level, in KOhms
@param[in] atmoco2 Atmospheric CO2 level for calibration purposes, in PPM
@param[in] vref analogRead() reference voltage, in volts
@param[in] vc Test voltage applied to the A pins of MQ-135, in volts
@param[in] pin The analog input pin for the readout of the sensor
*/
/**************************************************************************/
MQ135::MQ135(uint8_t pin, float rload, float rzero, float atmoco2, float vref, float vc) {
MQ135::MQ135(uint8_t pin) {
_pin = pin;
_rload = rload;
_rzero = rzero;
_atmoco2 = atmoco2;
_vref = vref;
_vc = vc;
}
@ -50,12 +40,7 @@ MQ135::MQ135(uint8_t pin, float rload, float rzero, float atmoco2, float vref, f
*/
/**************************************************************************/
float MQ135::getCorrectionFactor(float t, float h) {
//return CORA * t * t - CORB * t + CORC - (h-33.)*CORD;
// This formula is reduced from temperature and humidity dependency graph,
// found in this datasheet:
// http://china-total.com/Product/meter/gas-sensor/MQ135.pdf
return (1.30732 - 0.0116044 * t) * (2.20591 - 0.296456 * log(h));
return CORA * t * t - CORB * t + CORC - (h-33.)*CORD;
}
/**************************************************************************/
@ -67,13 +52,7 @@ float MQ135::getCorrectionFactor(float t, float h) {
/**************************************************************************/
float MQ135::getResistance() {
int val = analogRead(_pin);
float r;
//r = ((1023./(float)val) - 1.)*_rload;
// or taking Vref and Vc into account:
r = ((1023. * _rload * _vc) / ((float)val * _vref)) - _rload;
return r;
return ((1023./(float)val) - 1.)*RLOAD;
}
/**************************************************************************/
@ -99,7 +78,7 @@ float MQ135::getCorrectedResistance(float t, float h) {
*/
/**************************************************************************/
float MQ135::getPPM() {
return PARA * pow((getResistance()/_rzero), PARB);
return PARA * pow((getResistance()/RZERO), -PARB);
}
/**************************************************************************/
@ -114,7 +93,7 @@ float MQ135::getPPM() {
*/
/**************************************************************************/
float MQ135::getCorrectedPPM(float t, float h) {
return PARA * pow((getCorrectedResistance(t, h)/_rzero), PARB);
return PARA * pow((getCorrectedResistance(t, h)/RZERO), -PARB);
}
/**************************************************************************/
@ -125,7 +104,7 @@ float MQ135::getCorrectedPPM(float t, float h) {
*/
/**************************************************************************/
float MQ135::getRZero() {
return getResistance() * pow((_atmoco2/PARA), (1./-PARB));
return getResistance() * pow((ATMOCO2/PARA), (1./PARB));
}
/**************************************************************************/
@ -140,5 +119,5 @@ float MQ135::getRZero() {
*/
/**************************************************************************/
float MQ135::getCorrectedRZero(float t, float h) {
return getCorrectedResistance(t, h) * pow((_atmoco2/PARA), (1./-PARB));
return getCorrectedResistance(t, h) * pow((ATMOCO2/PARA), (1./PARB));
}

41
MQ135.h
View File

@ -21,52 +21,29 @@ v1.0 - First release
#include "WProgram.h"
#endif
/// The load resistance on the board, in KOhms
#define MQ135_RLOAD 10.0
/// Calibration sensor resistance at atmospheric CO2 level, in KOhms
#define MQ135_RZERO 76.63
/// 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)
/// Correlation parameters from Davide Gironi <http://davidegironi.blogspot.ru/2014/01/cheap-co2-meter-using-mq135-sensor-with.html>
#define PARA 56.0820
#define PARB (-5.9603)
#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, in PPM.
/// See http://co2now.org/
#define MQ135_ATMOCO2 397.16
/// Test voltage applied to the A pins of MQ-135, in volts. Usually, it's equal to power supply voltage.
#define MQ135_VC 5.0
/// analogRead reference voltage, in volts. By default it's a power supply voltage on Arduino. Can be changed with analogReference().
#define MQ135_VREF 5.0
/// Atmospheric CO2 level for calibration purposes
#define ATMOCO2 397.13
class MQ135 {
private:
uint8_t _pin;
// Calibration sensor resistance at atmospheric CO2 level, in KOhms
float _rzero;
// Atmospheric CO2 level for calibration purposes, in PPM, see http://co2now.org/
float _atmoco2;
// analogRead reference voltage, in volts
float _vref;
// Load resistance on board, in KOhms
float _rload;
// Test voltage applied to the A pins of MQ-135, in volts
float _vc;
public:
MQ135(uint8_t pin, float rload = MQ135_RLOAD, float rzero = MQ135_RZERO,
float atmoco2 = MQ135_ATMOCO2, float vref = MQ135_VREF,
float vc = MQ135_VC);
MQ135(uint8_t pin);
float getCorrectionFactor(float t, float h);
float getResistance();
float getCorrectedResistance(float t, float h);

View File

@ -1,7 +1,4 @@
MQ135
=====
Arduino library for the MQ135 Air Quality sensor. Read about using this library
[here](https://hackaday.io/project/3475-sniffing-trinket/log/12363-mq135-arduino-library).
A project using this library: [Sniffing
Trinket](https://hackaday.io/project/3475-sniffing-trinket).
Arduino library for the MQ135