Fixed example

This commit is contained in:
miguel5612 2020-03-26 11:54:18 -05:00
parent 1177d5827d
commit 3ae663163b
3 changed files with 26 additions and 64 deletions

View File

@ -9,6 +9,10 @@
modified 23 May 2019 modified 23 May 2019
by Miguel Califa by Miguel Califa
Updated library usage
modified 26 March 2020
by Miguel Califa
This example code is in the public domain. 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; float CH4, LPG, CO, Alcohol, Hexane, Benzine;
void setup() { void setup() {
//Init the serial port communication - to debug the library
Serial.begin(9600); //Init serial port Serial.begin(9600); //Init serial port
//init the sensor
/***************************** MQInicializar**************************************** //Set math model to calculate the PPM concentration and the value of constants
Input: pin, type MQ3.setRegressionMethod("Exponential"); //_PPM = a*ratio^b
Output:
Remarks: This function create the sensor object.
************************************************************************************/
MQ3.setRegressionMethod("Exponential");
MQ3.setA(4.8387); MQ3.setB(-2.68); // Configurate the ecuation values to get Benzene concentration 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); MQ3.setRL(10);
*/ */
/***************************** MQ Init ********************************************/
//Remarks: Configure the pin of arduino as input.
/************************************************************************************/
MQ3.init(); 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); MQ3.serialDebug(true);
//pinMode(calibration_button, INPUT); //pinMode(calibration_button, INPUT);
} }
void loop() { void loop() {
MQ3.update(); // Update data, the arduino will be read the voltage on the analog pin 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 MQ3.serialDebug(); // Will print the table on the serial port
/* delay(500); //Sampling frequency
//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("***************************");
*/
} }

View File

@ -99,7 +99,7 @@ float MQUnifiedsensor::readSensor()
//if(_PPM > 10000) _PPM = 99999999; //No negative values accepted or upper datasheet recomendation. //if(_PPM > 10000) _PPM = 99999999; //No negative values accepted or upper datasheet recomendation.
return _PPM; return _PPM;
} }
float MQUnifiedsensor::calibrate() { float MQUnifiedsensor::calibrate(float ratioInCleanAir) {
//More explained in: https://jayconsystems.com/blog/understanding-a-gas-sensor //More explained in: https://jayconsystems.com/blog/understanding-a-gas-sensor
/* /*
V = I x R V = I x R
@ -116,7 +116,7 @@ float MQUnifiedsensor::calibrate() {
float R0; //Define variable for R0 float R0; //Define variable for R0
RS_air = ((_VOLT_RESOLUTION*_RL)/_sensor_volt)-_RL; //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. 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. if(R0 < 0) R0 = 0; //No negative values accepted.
return R0; return R0;
} }

View File

@ -27,7 +27,7 @@ class MQUnifiedsensor
void serialDebug(bool onSetup = false); //Show on serial port information about sensor void serialDebug(bool onSetup = false); //Show on serial port information about sensor
//user functions //user functions
float calibrate(); float calibrate(float ratioInCleanAir);
float readSensor(); float readSensor();
//get function for info //get function for info
@ -42,7 +42,6 @@ class MQUnifiedsensor
byte _pin; byte _pin;
byte _firstFlag = false; byte _firstFlag = false;
byte _VOLT_RESOLUTION = 5.0; // if 3.3v use 3.3 byte _VOLT_RESOLUTION = 5.0; // if 3.3v use 3.3
byte _ratioInCleanAir;
byte _RL = 10; //Value in KiloOhms byte _RL = 10; //Value in KiloOhms
byte _ADC_Bit_Resolution = 10; byte _ADC_Bit_Resolution = 10;