forked from NuclearPhoenixx/MQ135
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adjustments to use the library with the ESP32
The original Arduino library for the MQ135 cannot be used with the ESP32 for several reasons. This fork is changed, to work with the ESP32.
- Loading branch information
Showing
11 changed files
with
434 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
|
||
MQ135_Simulation.raw | ||
MQ135_Simulation.net | ||
MQ135_Simulation.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
Version 4 | ||
SHEET 1 992 680 | ||
WIRE 0 48 -32 48 | ||
WIRE 96 48 80 48 | ||
WIRE 224 48 96 48 | ||
WIRE 432 48 224 48 | ||
WIRE 352 80 64 80 | ||
WIRE 96 112 96 48 | ||
WIRE 224 112 224 48 | ||
WIRE -32 128 -32 48 | ||
WIRE 432 128 432 48 | ||
WIRE 64 208 64 80 | ||
WIRE 352 208 352 80 | ||
WIRE 352 208 64 208 | ||
WIRE 224 240 224 192 | ||
WIRE 304 240 224 240 | ||
WIRE 432 240 432 208 | ||
WIRE 480 240 432 240 | ||
WIRE 224 256 224 240 | ||
WIRE 96 272 96 192 | ||
WIRE 432 272 432 240 | ||
WIRE 16 352 16 272 | ||
WIRE 224 352 224 336 | ||
FLAG -32 208 0 | ||
FLAG 224 352 0 | ||
FLAG 432 352 0 | ||
FLAG 304 240 VSense | ||
FLAG 480 240 VRef | ||
FLAG 16 352 0 | ||
SYMBOL voltage -32 112 R0 | ||
SYMATTR InstName V1 | ||
SYMATTR Value 5 | ||
SYMBOL res 208 96 R0 | ||
SYMATTR InstName RSense | ||
SYMATTR Value {Rsense} | ||
SYMBOL res 80 96 R0 | ||
SYMATTR InstName RHeat | ||
SYMATTR Value 33 | ||
SYMBOL res 96 32 R90 | ||
WINDOW 0 0 56 VBottom 2 | ||
WINDOW 3 32 56 VTop 2 | ||
SYMATTR InstName RPar+ | ||
SYMATTR Value 0.5 | ||
SYMBOL res 112 256 R90 | ||
WINDOW 0 0 56 VBottom 2 | ||
WINDOW 3 32 56 VTop 2 | ||
SYMATTR InstName Rpar- | ||
SYMATTR Value 0.5 | ||
SYMBOL res 208 240 R0 | ||
SYMATTR InstName RL | ||
SYMATTR Value 44k | ||
SYMBOL res 416 112 R0 | ||
SYMATTR InstName R1 | ||
SYMATTR Value 100k | ||
SYMBOL res 416 256 R0 | ||
SYMATTR InstName R2 | ||
SYMATTR Value 100k | ||
TEXT 584 56 Left 2 !.step param Rsense 24k 90k 1k | ||
TEXT 582 96 Left 2 !.op | ||
TEXT 136 96 Left 2 ;MQ135 | ||
TEXT 104 232 Left 2 ;H | ||
TEXT 104 64 Left 2 ;H | ||
TEXT 232 64 Left 2 ;A | ||
TEXT 232 224 Left 2 ;B |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
/**************************************************************************/ | ||
/*! | ||
@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 | ||
@param[in] refvpin The analog input pin for the reference voltage measurement | ||
@param[in] rzero Calibration resistance at atmospheric CO2 level | ||
@param[in] rload The load resistance on the board in kOhm | ||
*/ | ||
/**************************************************************************/ | ||
|
||
MQ135::MQ135(uint8_t pin, uint8_t refvpin, float rzero, float rload) { | ||
_pin = pin; | ||
_rzero = rzero; | ||
_rload = rload; | ||
_refvpin = refvpin; | ||
} | ||
|
||
/**************************************************************************/ | ||
/*! | ||
@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 | ||
Known issue: If the ADC resolution is not 10-bits, this will give | ||
back garbage values! | ||
@return The sensor resistance in kOhm | ||
*/ | ||
/**************************************************************************/ | ||
float MQ135::getResistance() { | ||
int val = analogRead(_pin); | ||
int refv = analogRead(_refvpin); | ||
return _rload * ( refv * 0.00080586 * 2 - val * 0.00080586 ) / ( val * 0.00080586 ); | ||
} | ||
|
||
/**************************************************************************/ | ||
/*! | ||
@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)); | ||
} |
Oops, something went wrong.