From 6d1229977e2eceba220afb311ff4f2b524286682 Mon Sep 17 00:00:00 2001 From: miguel5612 Date: Wed, 29 May 2019 20:58:22 -0500 Subject: [PATCH] fixed calibration --- src/MQUnifiedsensor.cpp | 27 ++++++++++++++++++++------- src/MQUnifiedsensor.h | 11 ++++++++--- 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/src/MQUnifiedsensor.cpp b/src/MQUnifiedsensor.cpp index 670406c..ca0868c 100644 --- a/src/MQUnifiedsensor.cpp +++ b/src/MQUnifiedsensor.cpp @@ -136,13 +136,26 @@ int MQUnifiedsensor::readPPM(int m, int b) { return floor(ppm); } double MQUnifiedsensor::calibrate() { - float sensor_volt; //Define variable for sensor voltage - float RS_air; //Define variable for sensor resistance - float R0; //Define variable for R0 - float sensorValue; //Define variable for analog readings - sensor_volt = this->getVoltage(); //Convert average to voltage - R0 = sensor_volt / _ratioInCleanAir; //Calculate R0 - return R0; + //More explained in: https://jayconsystems.com/blog/understanding-a-gas-sensor + /* + V = I x R + VRL = [VC / (RS + RL)] x RL + VRL = (VC x RL) / (RS + RL) + Así que ahora resolvemos para RS: + VRL x (RS + RL) = VC x RL + (VRL x RS) + (VRL x RL) = VC x RL + (VRL x RS) = (VC x RL) - (VRL x RL) + RS = [(VC x RL) - (VRL x RL)] / VRL + RS = [(VC x RL) / VRL] - RL + */ + float sensor_volt; //Define variable for sensor voltage + float RS_air; //Define variable for sensor resistance + float R0; //Define variable for R0 + float sensorValue; //Define variable for analog readings + sensor_volt = this->getVoltage(); //Convert average to voltage + RS_air = ((VOLT_RESOLUTION*RLValue)/sensor_volt)-RLValue; //Calculate RS in fresh air + R0 = RS_air/_ratioInCleanAir; //Calculate R0 + return R0; } double MQUnifiedsensor::getVoltage() { double avg = 0.0; diff --git a/src/MQUnifiedsensor.h b/src/MQUnifiedsensor.h index b034cf8..17380c3 100644 --- a/src/MQUnifiedsensor.h +++ b/src/MQUnifiedsensor.h @@ -4,9 +4,16 @@ #include #include + //Count of posible lectures #define lecturesAvailable 19 +/************************Global vars************************************/ +int VOLT_RESOLUTION = 5.0; // if 3.3v use 3.3 +int RLValue = 10; //Value in KiloOhms +int _ratioInCleanAir, _PPM; +double RS_air,_R0, _m, _b, _ratio; + /************************Hardware Related Macros************************************/ //Index in the nameLecture vector @@ -82,10 +89,8 @@ class MQUnifiedsensor private: - int _pin, _type, _PPM, _lecturePosInArray, _ratioInCleanAir; - double RS_air,_R0 = 11.820, _m, _b, _ratio; + int _pin, _type; String _MQ[19], _nameLectureRequeired; - int VOLT_RESOLUTION = 5.0; // if 3.3v use 3.3 }; #endif //MQUnifiedsensor_H \ No newline at end of file