This repository has been archived by the owner on May 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
MQ135.cpp
281 lines (254 loc) · 10.3 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/**************************************************************************/
/*!
@file MQ135.cpp
@author G.Krocker (Mad Frog Labs)
@license GNU GPLv3
First version of an Arduino Library for the MQ135 gas sensor
TODO (completed): 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
v2.0 - New Corrected version by Zesteros
v2.1 - Correct atmospheric co2 level for October 2022
*/
/**************************************************************************/
#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 Set up the pins!
*/
/**************************************************************************/
void MQ135::begin()
{
pinMode(_pin, INPUT);
}
/**************************************************************************/
/*!
@brief Get the resistance of the sensor, ie. the measurement value
@return The sensor resistance in Ohms
*/
/**************************************************************************/
float MQ135::getResistance() const {
int val = analogRead(_pin);
/*
* Values from @lorf
* r = ((1023. * _rload * _vc) / ((float)val * _vref)) - _rload;
*
*/
return ((1023. * RLOAD * 5.)/((float)val * 5.)) - RLOAD;
}
/**************************************************************************/
/*!
@brief Get the ppm of CO sensed
@return ppm value of CO in air
*/
/**************************************************************************/
float MQ135::getCOPPM() const {
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() const {
return scaleFactorCO2 * pow((getResistance()/r0CO2), -exponentCO2);
}
/**************************************************************************/
/*!
@brief Get the ppm of Ethanol sensed
@return The ppm of Ethanol in the air
*/
/**************************************************************************/
float MQ135::getEthanolPPM() const {
return scaleFactorEthanol * pow((getResistance()/r0Ethanol), -exponentEthanol);
}
/**************************************************************************/
/*!
@brief Get the ppm of NH4 sensed
@return The ppm of NH4 in the air
*/
/**************************************************************************/
float MQ135::getNH4PPM() const {
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() const {
return scaleFactorToluene * pow((getResistance()/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::getAcetonePPM() const {
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() const {
return getResistance() * pow((atmCO/scaleFactorCO), (1./exponentCO));
}
/**************************************************************************/
/*!
@brief Get the resistance RZero of the sensor for calibration purposes
@return The sensor resistance RZero in kOhm
*/
/**************************************************************************/
float MQ135::getRZeroCO2() const {
return getResistance() * pow((atmCO2/scaleFactorCO2), (1./exponentCO2));
}
/**************************************************************************/
/*!
@brief Get the resistance RZero of the sensor for calibration purposes
@return The sensor resistance RZero in kOhm
*/
/**************************************************************************/
float MQ135::getRZeroEthanol() const {
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::getRZeroNH4() const {
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() const {
return getResistance() * pow((atmToluene/scaleFactorToluene), (1./exponentToluene));
}
/**************************************************************************/
/*!
@brief Get the resistance RZero of the sensor for calibration purposes
@return The sensor resistance RZero in kOhm
*/
/**************************************************************************/
float MQ135::getRZeroAcetone() const {
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) const {
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) const {
return scaleFactorCO2 * pow((res/r0CO2), -exponentCO2);
}
/**************************************************************************/
/*!
@brief Get the ppm of Ethanol sensed
@return The ppm of Ethanol in the air
*/
/**************************************************************************/
float MQ135::getEthanol(float res) const {
return scaleFactorEthanol * pow((res/r0Ethanol), -exponentEthanol);
}
/**************************************************************************/
/*!
@brief Get the ppm of NH4 sensed
@return The ppm of NH4 in the air
*/
/**************************************************************************/
float MQ135::getNH4(float res) const {
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) const {
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) const {
return scaleFactorAcetone * pow((res/r0Acetone), -exponentAcetone);
}
/* RETURN THE RZERO WITH A PARAMETER OF RESISTANCE*/
float MQ135::getCorrectedRZero(float r) const {
return r * pow((atmCO2/scaleFactorCO2), (1./exponentCO2));
}
float MQ135::getCorrectedRZeroCO(float r) const {
return r * pow((atmCO/scaleFactorCO), (1./exponentCO));
}
float MQ135::getCorrectedRZeroEthanol(float r) const {
return r * pow((atmEthanol/scaleFactorEthanol), (1./exponentEthanol));
}
float MQ135::getCorrectedRZeroNH4(float r) const {
return r * pow((atmNH4/scaleFactorNH4), (1./exponentNH4));
}
float MQ135::getCorrectedRZeroToluene(float r) const {
return r * pow((atmToluene/scaleFactorToluene), (1./exponentToluene));
}
float MQ135::getCorrectedRZeroAcetone(float r) const {
return r * pow((atmAcetone/scaleFactorAcetone), (1./exponentAcetone));
}
/*CORRECTED RESISTANCE*/
float MQ135::getCorrectedResistance(float t, float h) const {
return getResistance()/getCorrectionFactor(t, h);
}
/*CORRECTION FACTOR*/
float MQ135::getCorrectionFactor(float t, float h) const {
return CORA * t * t - CORB * t + CORC - (h-33.)*CORD;
}
float MQ135::getCalibratedCO2(float t, float h) const {
return scaleFactorCO2 * pow((getCorrectedResistance(t, h)/getCorrectedRZero(getCorrectedResistance(t, h))), -exponentCO2);
}
float MQ135::getCalibratedCO(float t, float h) const {
return scaleFactorCO * pow((getCorrectedResistance(t, h)/getCorrectedRZeroCO(getCorrectedResistance(t, h))), -exponentCO);
}
float MQ135::getCalibratedEthanol(float t, float h) const {
return scaleFactorEthanol * pow((getCorrectedResistance(t, h)/getCorrectedRZeroEthanol(getCorrectedResistance(t, h))), -exponentEthanol);
}
float MQ135::getCalibratedNH4(float t, float h) const {
return scaleFactorNH4 * pow((getCorrectedResistance(t, h)/getCorrectedRZeroNH4(getCorrectedResistance(t, h))), -exponentNH4);
}
float MQ135::getCalibratedToluene(float t, float h) const {
return scaleFactorToluene * pow((getCorrectedResistance(t, h)/getCorrectedRZeroToluene(getCorrectedResistance(t, h))), -exponentToluene);
}
float MQ135::getCalibratedAcetone(float t, float h) const {
return scaleFactorAcetone * pow((getCorrectedResistance(t, h)/getCorrectedRZeroAcetone(getCorrectedResistance(t, h))), -exponentAcetone);
}