diff --git a/examples/Alcoholimeter/Alcoholimeter.ino b/examples/Alcoholimeter/Alcoholimeter.ino index 4ec6786..99efdf2 100644 --- a/examples/Alcoholimeter/Alcoholimeter.ino +++ b/examples/Alcoholimeter/Alcoholimeter.ino @@ -17,7 +17,7 @@ #include //Definitions -#define pin A0 //Analog input 0 of your arduino +#define pin A3 //Analog input 0 of your arduino #define type 3 //MQ3 //Declare Sensor @@ -25,6 +25,7 @@ MQUnifiedsensor MQ3(pin, type); void setup() { + Serial.begin(9600); //init the sensor /***************************** MQInicializar**************************************** Input: pin, type @@ -32,6 +33,14 @@ void setup() { Remarks: This function create the sensor object. ************************************************************************************/ MQ3.inicializar(); + + float RoCleanAir = MQ3.calibrate(); + + Serial.print("R0 preconfigurado: "); + Serial.println(MQ3.getR0()); + Serial.print("R0 clean air: "); + Serial.println(RoCleanAir); + MQ3.setR0(RoCleanAir); } void loop() { @@ -42,7 +51,8 @@ void loop() { ************************************************************************************/ //Read the sensor and print in serial port //Lecture will be saved in lecture variable - int lecture = MQ3.readSensor(""); // Return Alcohol concentration + int lecture = MQ3.readSensor("", true); // Return Alcohol concentration + /* int gL = lecture/1000; Serial.print("Alcohol ppm measured: "); Serial.print(lecture); @@ -51,4 +61,5 @@ void loop() { Serial.print("Alcohol gL measured: "); Serial.print(gL); Serial.println("g/L"); + */ } diff --git a/src/MQUnifiedsensor.cpp b/src/MQUnifiedsensor.cpp index e687e69..3bde33c 100644 --- a/src/MQUnifiedsensor.cpp +++ b/src/MQUnifiedsensor.cpp @@ -106,8 +106,11 @@ int MQUnifiedsensor::readSensor(String nameLectureRequeired, bool print) { String nameLecture = getnameLecture(); Serial.println("**********************"); + Serial.println("* PPM_log = (log10(Rs/R0) - b)/m"); + Serial.println("* PPM = pow(10, PPM_Log)"); Serial.println("* Sensor: MQ-" + String(_type)); Serial.println("* m =" + String(_m) + " ,b =" + String(_b) + ", R0 = " + _R0); + Serial.println("* Vcc: " + String(_VOLT_RESOLUTION) + ", RS: " + String(_RS_Calc)); Serial.println("* RS/R0 = " + String(_ratio) + " ,Voltaje leido(ADC): " + String(_sensor_volt)); Serial.println("* Lectura(" + nameLecture + ")=" + String(_PPM) + " PPM"); Serial.println("**********************"); @@ -512,16 +515,17 @@ void MQUnifiedsensor::setSensorCharacteristics(String nameLectureRequeired, bool } //Serial debugging } -int MQUnifiedsensor::readPPM(int m, int b) { +float MQUnifiedsensor::readPPM(int m, int b) { /** * Returns the PPM concentration */ + //More explained in: https://jayconsystems.com/blog/understanding-a-gas-sensor _sensor_volt = this->getVoltage(); - double RS_gas; //Define variable for sensor resistance + //_RS_Calc; //Define variable for sensor resistance - RS_gas = ((_VOLT_RESOLUTION*_RLValue)/_sensor_volt)-_RLValue; //Get value of RS in a gas + _RS_Calc = ((_VOLT_RESOLUTION*_RLValue)/_sensor_volt)-_RLValue; //Get value of RS in a gas - _ratio = RS_gas / this->_R0; // Get ratio RS_gas/RS_air + _ratio = _RS_Calc / this->_R0; // Get ratio RS_gas/RS_air double ppm_log = (log10(_ratio) - b) / m; //Get ppm value in linear scale according to the the ratio value double ppm = pow(10, ppm_log); //Convert ppm value to log scale @@ -541,18 +545,18 @@ long MQUnifiedsensor::calibrate(boolean print) { RS = [(VC x RL) / VRL] - RL */ _sensor_volt; //Define variable for sensor voltage - long RS_air; //Define variable for sensor resistance - long R0; //Define variable for R0 + float RS_air; //Define variable for sensor resistance + float R0; //Define variable for R0 _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 if(print) { Serial.println("*******Calibrating*********"); + Serial.println("* Vcc: " + String(_VOLT_RESOLUTION) + "*"); Serial.println("* Sensor: MQ-" + String(_type) + "*"); Serial.println("* _sensor_volt: " + String(_sensor_volt) + "*"); Serial.println("* _RLValue: " + String(_RLValue) + "*"); - Serial.println("* _VOLT_RESOLUTION: " + String(_VOLT_RESOLUTION) + "*"); Serial.println("* _ratioInCleanAir: " + String(_ratioInCleanAir) + "*"); Serial.println("* R0: " + String(R0) + "*"); Serial.println("*******Calibrating*********"); @@ -572,9 +576,15 @@ double MQUnifiedsensor::getVoltage(int read) { } return voltage; } + + void MQUnifiedsensor::setR0(double R0) { this->_R0 = R0; } + +double MQUnifiedsensor::getR0() { + return _R0; +} void MQUnifiedsensor::setDefaultGas() { if(_type == 2) diff --git a/src/MQUnifiedsensor.h b/src/MQUnifiedsensor.h index c98ab2c..2c41d40 100644 --- a/src/MQUnifiedsensor.h +++ b/src/MQUnifiedsensor.h @@ -274,11 +274,12 @@ class MQUnifiedsensor void setDefaultGas(); int readSensor(String nameLectureRequeired = "", bool print = false); - int readPPM(int m, int b); long calibrate(boolean print = false); double getVoltage(int read = true); double stringToDouble(String & str); + double getR0(); + float readPPM(int m, int b); String getnameLecture(); @@ -286,11 +287,12 @@ class MQUnifiedsensor private: /************************Private vars************************************/ - int _pin, _type, _R0, _lecturePosInArray; + int _pin, _type, _lecturePosInArray; + double _R0; String _nameLectureRequeired; int _VOLT_RESOLUTION = 5.0; // if 3.3v use 3.3 int _RLValue = 10; //Value in KiloOhms - int _PPM; + float _PPM, _RS_Calc; float _ratioInCleanAir, _sensor_volt, RS_air, _m, _b, _ratio; };