forked from GeorgK/MQ135
-
Notifications
You must be signed in to change notification settings - Fork 12
/
MQ135.cpp
executable file
·131 lines (105 loc) · 4.07 KB
/
MQ135.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/**************************************************************************/
/*!
@file MQ135.cpp
@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
*/
/**************************************************************************/
#include "MQ135.h"
/**************************************************************************/
/*!
@brief Default constructor
@param[in] pin The analog input pin for the readout of the sensor
*/
/**************************************************************************/
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
@return The calculated correction factor
*/
/**************************************************************************/
float MQ135::getCorrectionFactor(float t, float h) {
// Linearization of the temperature dependency curve under and above 20 degree C
// below 20degC: fact = a * t * t - b * t - (h - 33) * d
// above 20degC: fact = a * t + b * h + c
// this assumes a linear dependency on humidity
if(t < 20){
return CORA * t * t - CORB * t + CORC - (h-33.)*CORD;
} else {
return CORE * t + CORF * h + CORG;
}
}
/**************************************************************************/
/*!
@brief Get the resistance of the sensor, ie. the measurement value
@return The sensor resistance in kOhm
*/
/**************************************************************************/
float MQ135::getResistance() {
int val = analogRead(_pin);
return ((1023./(float)val) - 1.)*RLOAD;
}
/**************************************************************************/
/*!
@brief Get the resistance of the sensor, ie. the measurement value corrected
for temp/hum
@param[in] t The ambient air temperature
@param[in] h The relative humidity
@return The corrected sensor resistance kOhm
*/
/**************************************************************************/
float MQ135::getCorrectedResistance(float t, float h) {
return getResistance()/getCorrectionFactor(t, h);
}
/**************************************************************************/
/*!
@brief Get the ppm of CO2 sensed (assuming only CO2 in the air)
@return The ppm of CO2 in the air
*/
/**************************************************************************/
float MQ135::getPPM() {
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] 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);
}
/**************************************************************************/
/*!
@brief Get the resistance RZero of the sensor for calibration purposes
@return The sensor resistance RZero in kOhm
*/
/**************************************************************************/
float MQ135::getRZero() {
return getResistance() * pow((ATMOCO2/PARA), (1./PARB));
}
/**************************************************************************/
/*!
@brief Get the corrected resistance RZero of the sensor for calibration
purposes
@param[in] t The ambient air temperature
@param[in] h The relative humidity
@return The corrected sensor resistance RZero in kOhm
*/
/**************************************************************************/
float MQ135::getCorrectedRZero(float t, float h) {
return getCorrectedResistance(t, h) * pow((ATMOCO2/PARA), (1./PARB));
}