mirror of
				https://github.com/miguel5612/MQSensorsLib.git
				synced 2025-10-29 14:14:15 +03:00 
			
		
		
		
	Added debug on MQ-3
This commit is contained in:
		| @@ -17,12 +17,14 @@ | ||||
| #include <MQUnifiedsensor.h> | ||||
|  | ||||
| //Definitions | ||||
| #define placa "Arduino UNO" | ||||
| #define Voltage_Resolution 5 | ||||
| #define pin A0 //Analog input 0 of your arduino | ||||
| #define type 3 //MQ3 | ||||
| #define type "MQ-3" //MQ3 | ||||
| //#define calibration_button 13 //Pin to calibrate your sensor | ||||
|  | ||||
| //Declare Sensor | ||||
| MQUnifiedsensor MQ3(pin, type); | ||||
| MQUnifiedsensor MQ3(placa, Voltage_Resolution, pin, type); | ||||
|  | ||||
| //Variables | ||||
| float CH4, LPG, CO, Alcohol, Hexane, Benzine; | ||||
| @@ -35,7 +37,9 @@ void setup() { | ||||
|   Output:   | ||||
|   Remarks: This function create the sensor object. | ||||
|   ************************************************************************************/  | ||||
|   MQ3.setRegressionMethod("Exponential"); | ||||
|   MQ3.init();  | ||||
|   MQ3.serialDebug(true); | ||||
|   //pinMode(calibration_button, INPUT); | ||||
| } | ||||
|  | ||||
| @@ -63,23 +67,24 @@ void loop() { | ||||
|   //float lecture =  MQ3.readSensor("", true); // Return Alcohol concentration | ||||
|   // Options, uncomment where you need | ||||
|   MQ3.setA(2*10^31); MQ3.setB(19.01); // Configurate the ecuation values | ||||
|   CH4 =  MQ3.readSensor("Exponential"); // Return CH4 concentration | ||||
|   CH4 =  MQ3.readSensor(); // Return CH4 concentration | ||||
|  | ||||
|   MQ3.setA(44771); MQ3.setB(-3.245); // Configurate the ecuation values | ||||
|   LPG =  MQ3.readSensor("Exponential"); // Return LPG concentration | ||||
|   LPG =  MQ3.readSensor(); // Return LPG concentration | ||||
|  | ||||
|   MQ3.setA(521853); MQ3.setB(-3.821); // Configurate the ecuation values | ||||
|   CO =  MQ3.readSensor("Exponential"); // Return CO concentration | ||||
|   CO =  MQ3.readSensor(); // Return CO concentration | ||||
|    | ||||
|   MQ3.setA(0.3934); MQ3.setB(-1.504); // Configurate the ecuation values | ||||
|   Alcohol =  MQ3.readSensor("Exponential"); // Return Alcohol concentration | ||||
|   Alcohol =  MQ3.readSensor(); // Return Alcohol concentration | ||||
|    | ||||
|   MQ3.setA(7585.3); MQ3.setB(-2.849); // Configurate the ecuation values | ||||
|   Hexane =  MQ3.readSensor("Exponential"); // Return Hexane concentration | ||||
|   Hexane =  MQ3.readSensor(); // Return Hexane concentration | ||||
|  | ||||
|   MQ3.setA(4.8387); MQ3.setB(-2.68); // Configurate the ecuation values | ||||
|   Benzine =  MQ3.readSensor("Exponential"); // Return Benzene concentration | ||||
|   Benzine =  MQ3.readSensor(); // Return Benzene concentration | ||||
|  | ||||
|   /* | ||||
|   Serial.println("***************************"); | ||||
|   Serial.println("Lectures for MQ-3"); | ||||
|   Serial.print("Volt: ");Serial.print(MQ3.getVoltage(false));Serial.println(" V");  | ||||
| @@ -91,5 +96,6 @@ void loop() { | ||||
|   Serial.print("Hexane: ");Serial.print(Hexane,2);Serial.println(" mg/L"); | ||||
|   Serial.print("Benzine: ");Serial.print(Benzine,2);Serial.println(" mg/L"); | ||||
|   Serial.println("***************************"); | ||||
|    | ||||
|   */ | ||||
|   MQ3.serialDebug(); | ||||
| } | ||||
| @@ -1,18 +1,43 @@ | ||||
| #include "MQUnifiedsensor.h" | ||||
| 
 | ||||
| MQUnifiedsensor::MQUnifiedsensor(String Placa, int Voltage_Resolution, int pin, String type) { | ||||
| MQUnifiedsensor::MQUnifiedsensor(String Placa, double Voltage_Resolution, int pin, String type) { | ||||
|   this->_pin = pin; | ||||
|   this->_type = type; //MQ-2, MQ-3 ... MQ-309A
 | ||||
|   this->_placa = Placa; | ||||
|   this-> _VOLT_RESOLUTION = Voltage_Resolution; | ||||
| } | ||||
| MQUnifiedsensor::setA(double a) { | ||||
| void MQUnifiedsensor::init() | ||||
| { | ||||
|   pinMode(_pin, INPUT); | ||||
| } | ||||
| void MQUnifiedsensor::setA(double a) { | ||||
|   this->_a = a; | ||||
| } | ||||
| MQUnifiedsensor::setB(double b) { | ||||
| void MQUnifiedsensor::setB(double b) { | ||||
|   this->_b = b; | ||||
| } | ||||
| MQUnifiedsensor::serialDebug(boolean onSetup, String regressionMethod) | ||||
| void MQUnifiedsensor::setR0(double R0) { | ||||
|   this->_R0 = R0; | ||||
| } | ||||
| void MQUnifiedsensor::setRL(double RL) { | ||||
|   this->_RL = RL; | ||||
| } | ||||
| void MQUnifiedsensor::setVoltResolution(double voltage_resolution) | ||||
| { | ||||
|   _VOLT_RESOLUTION = voltage_resolution; | ||||
| } | ||||
| void MQUnifiedsensor::setRegressionMethod(String regressionMethod) | ||||
| { | ||||
|   this->_regressionMethod = regressionMethod; | ||||
| } | ||||
| 
 | ||||
| double MQUnifiedsensor::getR0() { | ||||
|   return _R0; | ||||
| } | ||||
| double MQUnifiedsensor::getRL() { | ||||
|   return _RL; | ||||
| } | ||||
| void MQUnifiedsensor::serialDebug(bool onSetup) | ||||
| { | ||||
|   if(onSetup) | ||||
|   { | ||||
| @@ -26,12 +51,12 @@ MQUnifiedsensor::serialDebug(boolean onSetup, String regressionMethod) | ||||
|     Serial.println("Contributors: Andres A. Martinez - Juan A. Rodríguez - Mario A. Rodríguez O "); | ||||
| 
 | ||||
|     Serial.println("Sensor:" + _type); | ||||
|     Serial.println("Supply voltage:" + _VOLT_RESOLUTION); | ||||
|     Serial.println("R0: " + _R0); | ||||
|     Serial.println("RL: " + _RL); | ||||
|     Serial.print("Supply voltage:"); Serial.println(_VOLT_RESOLUTION); | ||||
|     Serial.print("R0: "); Serial.println(_R0); | ||||
|     Serial.print("RL: "); Serial.println(_RL); | ||||
| 
 | ||||
|     Serial.println("Model: Logarithmic regression with parameters."); | ||||
|     Serial.println(_type + ":" + "a:" + _a + " | b:" + _b); | ||||
|     Serial.print(_type + ":" + "a:"); Serial.print(_a); Serial.print(" | b:"); Serial.println(_b); | ||||
| 
 | ||||
|     Serial.println("Development board: " + _placa); | ||||
|   } | ||||
| @@ -46,9 +71,11 @@ MQUnifiedsensor::serialDebug(boolean onSetup, String regressionMethod) | ||||
|     else | ||||
|     { | ||||
|       String eq = ""; | ||||
|       if(regression == "Linear") eq = "ratio*a + b" | ||||
|       if(regression == "Exponential") eq = "a*ratio^b" | ||||
|       Serial.println("|" + _adc + "|" + "v = ADC*" + _VOLT_RESOLUTION + "/1024" + "|" + _sensor_volt + "|" + "RS = ((" + _VOLT_RESOLUTION + "*RL)/Voltage) - RL" + "|" + _RS_Calc + "|" + "Ratio = RS/R0" + "|" + _ratio + "|" + eq + "|" + _PPM); | ||||
|       if(_regressionMethod == "Linear") eq = "ratio*a + b"; | ||||
|       if(_regressionMethod == "Exponential") eq = "a*ratio^b"; | ||||
|       Serial.print("|"); Serial.print(_adc);  Serial.print("| v = ADC*"); Serial.print(_VOLT_RESOLUTION); Serial.print("/1024 |"); Serial.print(_sensor_volt); | ||||
|       Serial.print("| RS = ((" ); Serial.print(_VOLT_RESOLUTION ); Serial.print("*RL)/Voltage) - RL |"); Serial.print(_RS_Calc); Serial.print("| Ratio = RS/R0 |"); | ||||
|       Serial.print(_ratio);  Serial.print( "|" + eq + "|"); Serial.print(_PPM); Serial.println("|"); | ||||
|     } | ||||
|   } | ||||
| } | ||||
| @@ -56,31 +83,19 @@ void MQUnifiedsensor::update() | ||||
| { | ||||
|   _sensor_volt = this->getVoltage(); | ||||
| } | ||||
| void MQUnifiedsensor::setVoltResolution(float voltaje) | ||||
| { | ||||
|   _VOLT_RESOLUTION = voltaje; | ||||
| } | ||||
| void MQUnifiedsensor::init() | ||||
| { | ||||
|   pinMode(_pin, INPUT); | ||||
| } | ||||
| float MQUnifiedsensor::readSensor(String regressionMethod) | ||||
| float MQUnifiedsensor::readSensor() | ||||
| { | ||||
|   //More explained in: https://jayconsystems.com/blog/understanding-a-gas-sensor
 | ||||
|   _RS_Calc = ((_VOLT_RESOLUTION*_RLValue)/_sensor_volt)-_RLValue; //Get value of RS in a gas
 | ||||
|   _RS_Calc = ((_VOLT_RESOLUTION*_RL)/_sensor_volt)-_RL; //Get value of RS in a gas
 | ||||
|   if(_RS_Calc < 0)  _RS_Calc = 0; //No negative values accepted.
 | ||||
|   _ratio = _RS_Calc / this->_R0;   // Get ratio RS_gas/RS_air
 | ||||
|   if(_ratio <= 0 || _ratio>100)  _ratio = 0.01; //No negative values accepted or upper datasheet recomendation.
 | ||||
|   if(regressionMethod == "Exponential") _PPM= _a*pow(_ratio, _b); | ||||
|   if(regressionMethod == "Linear") _PPM= _a*_ratio + _b); | ||||
|   if(_regressionMethod == "Exponential") _PPM= _a*pow(_ratio, _b); | ||||
|   if(_regressionMethod == "Linear") _PPM= _a*_ratio + _b; | ||||
|   if(_PPM < 0)  _PPM = 0; //No negative values accepted or upper datasheet recomendation.
 | ||||
|   if(_PPM > 10000) _PPM = 9999; //No negative values accepted or upper datasheet recomendation.
 | ||||
|   return _PPM; | ||||
| } | ||||
| String MQUnifiedsensor::getnameLecture() | ||||
| { | ||||
|   return _nameLectureRequeired; | ||||
| } | ||||
| float MQUnifiedsensor::calibrate() { | ||||
|   //More explained in: https://jayconsystems.com/blog/understanding-a-gas-sensor
 | ||||
|   /*
 | ||||
| @@ -96,7 +111,7 @@ float MQUnifiedsensor::calibrate() { | ||||
|   */ | ||||
|   float RS_air; //Define variable for sensor resistance
 | ||||
|   float R0; //Define variable for R0
 | ||||
|   RS_air = ((_VOLT_RESOLUTION*_RLValue)/_sensor_volt)-_RLValue; //Calculate RS in fresh air
 | ||||
|   RS_air = ((_VOLT_RESOLUTION*_RL)/_sensor_volt)-_RL; //Calculate RS in fresh air
 | ||||
|   if(RS_air < 0)  RS_air = 0; //No negative values accepted.
 | ||||
|   R0 = RS_air/_ratioInCleanAir; //Calculate R0 
 | ||||
|   if(R0 < 0)  R0 = 0; //No negative values accepted.
 | ||||
| @@ -120,23 +135,6 @@ double MQUnifiedsensor::getVoltage(int read) { | ||||
|   } | ||||
|   return voltage; | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| void MQUnifiedsensor::setR0(double R0) { | ||||
|   this->_R0 = R0; | ||||
| } | ||||
| 
 | ||||
| double MQUnifiedsensor::getR0() { | ||||
|   return _R0; | ||||
| } | ||||
| void MQUnifiedsensor::setRL(double RL) { | ||||
|   this->_RLValue = RL; | ||||
| } | ||||
| 
 | ||||
| double MQUnifiedsensor::getRL() { | ||||
|   return _RLValue; | ||||
| } | ||||
| 
 | ||||
| double MQUnifiedsensor::stringToDouble(String & str) | ||||
| { | ||||
|   return atof( str.c_str() ); | ||||
|   | ||||
| @@ -13,7 +13,7 @@ | ||||
| class MQUnifiedsensor | ||||
| { | ||||
|   public: | ||||
|     MQUnifiedsensor(String Placa = "Arduino", int Voltage_Resolution =  5, int pin, String type); | ||||
|     MQUnifiedsensor(String Placa = "Arduino", double Voltage_Resolution =  5, int pin = 1, String type = "CUSTOM MQ"); | ||||
|      | ||||
|     //Functions to set values
 | ||||
|     void init(); | ||||
| @@ -22,12 +22,13 @@ class MQUnifiedsensor | ||||
|     void setRL(double RL = 10); | ||||
|     void setA(double a); | ||||
|     void setB(double b); | ||||
|     void setRegressionMethod(String regressionMethod); | ||||
|     void setVoltResolution(double voltage_resolution =  5); | ||||
|     void serialDebug(boolean onSetup = false); //Show on serial port information about sensor
 | ||||
|     void serialDebug(bool onSetup = false); //Show on serial port information about sensor
 | ||||
|      | ||||
|     //user functions
 | ||||
|     float calibrate(boolean print = false, String regressionMethod = "Exponential"); | ||||
|     float readSensor(String regressionMethod = "Exponential", float _a, float _b); | ||||
|     float calibrate(); | ||||
|     float readSensor(); | ||||
|      | ||||
|     //get function for info
 | ||||
|     double getR0(); | ||||
| @@ -40,13 +41,16 @@ class MQUnifiedsensor | ||||
|     /************************Private vars************************************/ | ||||
|     byte _pin; | ||||
|     byte _firstFlag = false; | ||||
|     String _type; | ||||
|     String _placa;  | ||||
|     byte _VOLT_RESOLUTION  = 5.0; // if 3.3v use 3.3
 | ||||
|     byte _ratioInCleanAir, _sensor_volt; | ||||
|     byte _RLValue = 10; //Value in KiloOhms
 | ||||
|     byte _RL = 10; //Value in KiloOhms
 | ||||
|      | ||||
|     double _adc, _a, _b; | ||||
|     float  _R0, RS_air, _ratio, _PPM, _RS_Calc;   | ||||
|      | ||||
|     String _type; | ||||
|     String _placa;  | ||||
|     String _regressionMethod; | ||||
| }; | ||||
| 
 | ||||
| #endif //MQUnifiedsensor_H
 | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 miguel5612
					miguel5612