From 3ae663163b013be05fe6df865f40d449e99953ec Mon Sep 17 00:00:00 2001 From: miguel5612 Date: Thu, 26 Mar 2020 11:54:18 -0500 Subject: [PATCH] Fixed example --- examples/MQ-3/MQ-3.ino | 83 ++++++++++++----------------------------- src/MQUnifiedsensor.cpp | 4 +- src/MQUnifiedsensor.h | 3 +- 3 files changed, 26 insertions(+), 64 deletions(-) diff --git a/examples/MQ-3/MQ-3.ino b/examples/MQ-3/MQ-3.ino index 0493e4a..bc22d48 100644 --- a/examples/MQ-3/MQ-3.ino +++ b/examples/MQ-3/MQ-3.ino @@ -9,6 +9,10 @@ modified 23 May 2019 by Miguel Califa + Updated library usage + modified 26 March 2020 + by Miguel Califa + This example code is in the public domain. */ @@ -31,79 +35,38 @@ MQUnifiedsensor MQ3(placa, Voltage_Resolution, ADC_Bit_Resolution, pin, type); float CH4, LPG, CO, Alcohol, Hexane, Benzine; void setup() { + //Init the serial port communication - to debug the library Serial.begin(9600); //Init serial port - //init the sensor - /***************************** MQInicializar**************************************** - Input: pin, type - Output: - Remarks: This function create the sensor object. - ************************************************************************************/ - MQ3.setRegressionMethod("Exponential"); + + //Set math model to calculate the PPM concentration and the value of constants + MQ3.setRegressionMethod("Exponential"); //_PPM = a*ratio^b MQ3.setA(4.8387); MQ3.setB(-2.68); // Configurate the ecuation values to get Benzene concentration + // Calibration setup + MQ3.setR0(3.86018237); + /* - //Si el valor de RL es diferente a 10K por favor asigna tu valor de RL con el siguiente metodo: + //If the RL value is different from 10K please assign your RL value with the following method: MQ3.setRL(10); */ + /***************************** MQ Init ********************************************/ + //Remarks: Configure the pin of arduino as input. + /************************************************************************************/ MQ3.init(); + /***************************** MQ Init ********************************************/ + //Input: setup flag, if this function are on setup will print the headers (Optional - Default value: False) + //Output: print on serial port the information about sensor and sensor readings + //Remarks: Configure the pin of arduino as input. + /************************************************************************************/ MQ3.serialDebug(true); + //pinMode(calibration_button, INPUT); } void loop() { MQ3.update(); // Update data, the arduino will be read the voltage on the analog pin - MQ3.readSensor(); // Sensor will read PPM concentration using the a and b values setted before or on setup + MQ3.readSensor(); // Sensor will read PPM concentration using the model and a and b values setted before or in the setup MQ3.serialDebug(); // Will print the table on the serial port - /* - //Rutina de calibracion - Uncomment if you need (setup too and header) - if(calibration_button) - { - float R0 = MQ3.calibrate(); - MQ3.setR0(R0); - } - */ - - /***************************** MQReadSensor **************************************** - Input: Gas - Serial print flag - Output: Value in PPM - Remarks: This function use readPPM to read the value in PPM the gas in the air. - ************************************************************************************/ - //Read the sensor and print in serial port - //Lecture will be saved in lecture variable - //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(); // Return CH4 concentration - - MQ3.setA(44771); MQ3.setB(-3.245); // Configurate the ecuation values - LPG = MQ3.readSensor(); // Return LPG concentration - - MQ3.setA(521853); MQ3.setB(-3.821); // Configurate the ecuation values - CO = MQ3.readSensor(); // Return CO concentration - - MQ3.setA(0.3934); MQ3.setB(-1.504); // Configurate the ecuation values - Alcohol = MQ3.readSensor(); // Return Alcohol concentration - - MQ3.setA(7585.3); MQ3.setB(-2.849); // Configurate the ecuation values - Hexane = MQ3.readSensor(); // Return Hexane concentration - - MQ3.setA(4.8387); MQ3.setB(-2.68); // Configurate the ecuation values - 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"); - Serial.print("R0: ");Serial.print(MQ3.getR0());Serial.println(" Ohm"); - Serial.print("CH4: ");Serial.print(CH4,2);Serial.println(" mg/L"); - Serial.print("LPG: ");Serial.print(LPG,2);Serial.println(" mg/L"); - Serial.print("CO: ");Serial.print(CO,2);Serial.println(" mg/L"); - Serial.print("Alcohol: ");Serial.print(Alcohol,2);Serial.println(" mg/L"); - Serial.print("Hexane: ");Serial.print(Hexane,2);Serial.println(" mg/L"); - Serial.print("Benzine: ");Serial.print(Benzine,2);Serial.println(" mg/L"); - Serial.println("***************************"); - */ + delay(500); //Sampling frequency } \ No newline at end of file diff --git a/src/MQUnifiedsensor.cpp b/src/MQUnifiedsensor.cpp index 24e7040..f698e11 100644 --- a/src/MQUnifiedsensor.cpp +++ b/src/MQUnifiedsensor.cpp @@ -99,7 +99,7 @@ float MQUnifiedsensor::readSensor() //if(_PPM > 10000) _PPM = 99999999; //No negative values accepted or upper datasheet recomendation. return _PPM; } -float MQUnifiedsensor::calibrate() { +float MQUnifiedsensor::calibrate(float ratioInCleanAir) { //More explained in: https://jayconsystems.com/blog/understanding-a-gas-sensor /* V = I x R @@ -116,7 +116,7 @@ float MQUnifiedsensor::calibrate() { float R0; //Define variable for R0 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 + R0 = RS_air/ratioInCleanAir; //Calculate R0 if(R0 < 0) R0 = 0; //No negative values accepted. return R0; } diff --git a/src/MQUnifiedsensor.h b/src/MQUnifiedsensor.h index ec35c78..95c1bed 100644 --- a/src/MQUnifiedsensor.h +++ b/src/MQUnifiedsensor.h @@ -27,7 +27,7 @@ class MQUnifiedsensor void serialDebug(bool onSetup = false); //Show on serial port information about sensor //user functions - float calibrate(); + float calibrate(float ratioInCleanAir); float readSensor(); //get function for info @@ -42,7 +42,6 @@ class MQUnifiedsensor byte _pin; byte _firstFlag = false; byte _VOLT_RESOLUTION = 5.0; // if 3.3v use 3.3 - byte _ratioInCleanAir; byte _RL = 10; //Value in KiloOhms byte _ADC_Bit_Resolution = 10;