mirror of
				https://github.com/miguel5612/MQSensorsLib.git
				synced 2025-10-29 14:14:15 +03:00 
			
		
		
		
	Fixed mq-3 example
This commit is contained in:
		| @@ -35,7 +35,7 @@ void setup() { | ||||
|   Output:   | ||||
|   Remarks: This function create the sensor object. | ||||
|   ************************************************************************************/  | ||||
|   MQ3.inicializar();  | ||||
|   MQ3.init();  | ||||
|   //pinMode(calibration_button, INPUT); | ||||
| } | ||||
|  | ||||
| @@ -62,12 +62,23 @@ void loop() { | ||||
|   //Lecture will be saved in lecture variable | ||||
|   //float lecture =  MQ3.readSensor("", true); // Return Alcohol concentration | ||||
|   // Options, uncomment where you need | ||||
|   CH4 =  MQ3.readSensor("CH4"); // Return CH4 concentration | ||||
|   LPG =  MQ3.readSensor("LPG"); // Return LPG concentration | ||||
|   CO =  MQ3.readSensor("CO"); // Return CO concentration | ||||
|   Alcohol =  MQ3.readSensor("Alcohol"); // Return Alcohol concentration | ||||
|   Hexane =  MQ3.readSensor("Hexane"); // Return Hexane concentration | ||||
|   Benzine =  MQ3.readSensor("Benzene"); // Return Benzene concentration | ||||
|   MQ3.setA(2*10^31); MQ3.setB(19.01); // Configurate the ecuation values | ||||
|   CH4 =  MQ3.readSensor("Exponential"); // Return CH4 concentration | ||||
|  | ||||
|   MQ3.setA(44771); MQ3.setB(-3.245); // Configurate the ecuation values | ||||
|   LPG =  MQ3.readSensor("Exponential"); // Return LPG concentration | ||||
|  | ||||
|   MQ3.setA(521853); MQ3.setB(-3.821); // Configurate the ecuation values | ||||
|   CO =  MQ3.readSensor("Exponential"); // Return CO concentration | ||||
|    | ||||
|   MQ3.setA(0.3934); MQ3.setB(-1.504); // Configurate the ecuation values | ||||
|   Alcohol =  MQ3.readSensor("Exponential"); // Return Alcohol concentration | ||||
|    | ||||
|   MQ3.setA(7585.3); MQ3.setB(-2.849); // Configurate the ecuation values | ||||
|   Hexane =  MQ3.readSensor("Exponential"); // Return Hexane concentration | ||||
|  | ||||
|   MQ3.setA(4.8387); MQ3.setB(-2.68); // Configurate the ecuation values | ||||
|   Benzine =  MQ3.readSensor("Exponential"); // Return Benzene concentration | ||||
|  | ||||
|   Serial.println("***************************"); | ||||
|   Serial.println("Lectures for MQ-3"); | ||||
|   | ||||
| @@ -6,7 +6,13 @@ MQUnifiedsensor::MQUnifiedsensor(String Placa, int Voltage_Resolution, int pin, | ||||
|   this->_placa = Placa; | ||||
|   this-> _VOLT_RESOLUTION = Voltage_Resolution; | ||||
| } | ||||
| MQUnifiedsensor::serialDebug(boolean onSetup) | ||||
| MQUnifiedsensor::setA(double a) { | ||||
|   this->_a = a; | ||||
| } | ||||
| MQUnifiedsensor::setB(double b) { | ||||
|   this->_b = b; | ||||
| } | ||||
| MQUnifiedsensor::serialDebug(boolean onSetup, String regressionMethod) | ||||
| { | ||||
|   if(onSetup) | ||||
|   { | ||||
| @@ -40,6 +46,7 @@ MQUnifiedsensor::serialDebug(boolean onSetup) | ||||
|     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); | ||||
|     } | ||||
| @@ -57,15 +64,15 @@ void MQUnifiedsensor::init() | ||||
| { | ||||
|   pinMode(_pin, INPUT); | ||||
| } | ||||
| float MQUnifiedsensor::readSensor(String nameLectureRequeired) | ||||
| float MQUnifiedsensor::readSensor(String regressionMethod) | ||||
| { | ||||
|   setSensorCharacteristics(nameLectureRequeired, print); //In this function update _a and _b
 | ||||
|   //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
 | ||||
|   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.
 | ||||
|   _PPM= _a*pow(_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; | ||||
|   | ||||
| @@ -20,12 +20,14 @@ class MQUnifiedsensor | ||||
|     void update(); | ||||
|     void setR0(double R0 = 10); | ||||
|     void setRL(double RL = 10); | ||||
|     void setA(double a); | ||||
|     void setB(double b); | ||||
|     void setVoltResolution(double voltage_resolution =  5); | ||||
|     void serialDebug(boolean onSetup = false); //Show on serial port information about sensor
 | ||||
|      | ||||
|     //user functions
 | ||||
|     float calibrate(boolean print = false); | ||||
|     float readSensor(<String regressionMethod = "Exponential", float _a, float _b); | ||||
|     float calibrate(boolean print = false, String regressionMethod = "Exponential"); | ||||
|     float readSensor(String regressionMethod = "Exponential", float _a, float _b); | ||||
|      | ||||
|     //get function for info
 | ||||
|     double getR0(); | ||||
| @@ -43,7 +45,7 @@ class MQUnifiedsensor | ||||
|     byte _VOLT_RESOLUTION  = 5.0; // if 3.3v use 3.3
 | ||||
|     byte _ratioInCleanAir, _sensor_volt; | ||||
|     byte _RLValue = 10; //Value in KiloOhms
 | ||||
|     double _adc; | ||||
|     double _adc, _a, _b; | ||||
|     float  _R0, RS_air, _ratio, _PPM, _RS_Calc;   | ||||
| }; | ||||
| 
 | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 miguel5612
					miguel5612