Make almost all variable parameters settable via optional constructor arguments

This commit is contained in:
Dmitry Frolov 2015-07-08 19:12:35 +06:00
parent 88e1e8abc2
commit d39b92dba0
3 changed files with 56 additions and 17 deletions

View File

@ -20,12 +20,22 @@ 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] 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
*/
/**************************************************************************/
MQ135::MQ135(uint8_t pin) {
MQ135::MQ135(uint8_t pin, float rload, float rzero, float atmoco2, float vref, float vc) {
_pin = pin;
_rload = rload;
_rzero = rzero;
_atmoco2 = atmoco2;
_vref = vref;
_vc = vc;
}
@ -52,7 +62,13 @@ float MQ135::getCorrectionFactor(float t, float h) {
/**************************************************************************/
float MQ135::getResistance() {
int val = analogRead(_pin);
return ((1023./(float)val) - 1.)*RLOAD;
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;
}
/**************************************************************************/
@ -78,7 +94,7 @@ float MQ135::getCorrectedResistance(float t, float h) {
*/
/**************************************************************************/
float MQ135::getPPM() {
return PARA * pow((getResistance()/RZERO), -PARB);
return PARA * pow((getResistance()/_rzero), -PARB);
}
/**************************************************************************/
@ -93,7 +109,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);
}
/**************************************************************************/
@ -104,7 +120,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));
}
/**************************************************************************/
@ -119,5 +135,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));
}

38
MQ135.h
View File

@ -21,13 +21,15 @@ v1.0 - First release
#include "WProgram.h"
#endif
/// The load resistance on the board
#define RLOAD 10.0
/// Calibration resistance at atmospheric CO2 level
#define RZERO 76.63
/// 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
/// Parameters for calculating ppm of CO2 from sensor resistance
#define PARA 116.6020682
#define PARB 2.769034857
//#define PARA 116.6020682
//#define PARB 2.769034857
#define PARA 117.185
#define PARB 2.65797
/// Parameters to model temperature and humidity dependence
#define CORA 0.00035
@ -35,15 +37,33 @@ v1.0 - First release
#define CORC 1.39538
#define CORD 0.0018
/// Atmospheric CO2 level for calibration purposes
#define ATMOCO2 397.13
/// 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
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);
MQ135(uint8_t pin, float rload = MQ135_RLOAD, float rzero = MQ135_RZERO,
float atmoco2 = MQ135_ATMOCO2, float vref = MQ135_VREF,
float vc = MQ135_VC);
float getCorrectionFactor(float t, float h);
float getResistance();
float getCorrectedResistance(float t, float h);

View File

@ -1,4 +1,7 @@
MQ135
=====
Arduino library for the 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).