Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 29 additions & 8 deletions MQ135.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}


Expand All @@ -40,7 +50,12 @@ MQ135::MQ135(uint8_t pin) {
*/
/**************************************************************************/
float MQ135::getCorrectionFactor(float t, float h) {
return CORA * t * t - CORB * t + CORC - (h-33.)*CORD;
//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));
}

/**************************************************************************/
Expand All @@ -52,7 +67,13 @@ float MQ135::getCorrectionFactor(float t, float h) {
/**************************************************************************/
float MQ135::getResistance() {
int val = analogRead(_pin);
return ((1023./(float)val) * 5. - 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;
}

/**************************************************************************/
Expand All @@ -78,7 +99,7 @@ float MQ135::getCorrectedResistance(float t, float h) {
*/
/**************************************************************************/
float MQ135::getPPM() {
return PARA * pow((getResistance()/RZERO), -PARB);
return PARA * pow((getResistance()/_rzero), PARB);
}

/**************************************************************************/
Expand All @@ -93,7 +114,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);
}

/**************************************************************************/
Expand All @@ -104,7 +125,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));
}

/**************************************************************************/
Expand All @@ -119,5 +140,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 changes: 32 additions & 9 deletions MQ135.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,52 @@ 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)
/// 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)

/*
/// 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

/// Atmospheric CO2 level for calibration purposes
#define ATMOCO2 397.13
/// 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);
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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).