Fixed example calibratioN

This commit is contained in:
miguel5612 2019-05-29 22:13:13 -05:00
parent c2e4ca7164
commit d6530691aa
3 changed files with 50 additions and 75 deletions

View File

@ -17,6 +17,10 @@
modified 28 May 2019 modified 28 May 2019
by Ghiordy contreras, Miguel Califa by Ghiordy contreras, Miguel Califa
Added voltaje to log
modified 29 May 2019
by Miguel Califa
This example code is in the public domain. This example code is in the public domain.
*/ */
@ -44,15 +48,16 @@ LiquidCrystal_I2C lcd(0x27, 16, 2);
//Declare Sensor //Declare Sensor
MQUnifiedsensor MQ2(pin2, 2); MQUnifiedsensor MQ2(pin2, 2);
MQUnifiedsensor MQ3(pin3, 2); MQUnifiedsensor MQ3(pin3, 3);
MQUnifiedsensor MQ4(pin4, 2); MQUnifiedsensor MQ4(pin4, 4);
MQUnifiedsensor MQ5(pin5, 2); MQUnifiedsensor MQ5(pin5, 5);
MQUnifiedsensor MQ6(pin6, 2); MQUnifiedsensor MQ6(pin6, 6);
MQUnifiedsensor MQ7(pin7, 2); MQUnifiedsensor MQ7(pin7, 7);
MQUnifiedsensor MQ8(pin8, 2); MQUnifiedsensor MQ8(pin8, 8);
MQUnifiedsensor MQ9(pin9, 2); MQUnifiedsensor MQ9(pin9, 9);
unsigned long contador = 0; unsigned long contador = 0;
unsigned long time = millis();
void setup() { void setup() {
//Init serial port //Init serial port
@ -66,7 +71,7 @@ void setup() {
lcd.setCursor(0,0); lcd.setCursor(0,0);
lcd.print("MQ2 to MQ9"); lcd.print("MQ2 to MQ9");
lcd.setCursor(0,1); lcd.setCursor(0,1);
lcd.print(" Calibracion"); lcd.print(" Calibrating");
//init the sensor //init the sensor
MQ2.inicializar(); MQ2.inicializar();
@ -77,11 +82,20 @@ void setup() {
MQ7.inicializar(); MQ7.inicializar();
MQ8.inicializar(); MQ8.inicializar();
MQ9.inicializar(); MQ9.inicializar();
MQ2.setVoltResolution(5);
MQ3.setVoltResolution(5);
MQ4.setVoltResolution(5);
MQ5.setVoltResolution(5);
MQ6.setVoltResolution(5);
MQ7.setVoltResolution(5);
MQ8.setVoltResolution(5);
MQ9.setVoltResolution(5);
//Print in serial monitor //Print in serial monitor
Serial.println("MQ2 to MQ9 - Calibracion"); Serial.println("MQ2 to MQ9 - Calibracion");
Serial.println("Note - Make sure you are in a clean room and the sensor has pre-heated almost 4 hours"); Serial.println("Note - Make sure you are in a clean room and the sensor has pre-heated almost 4 hours");
Serial.println("Autonumeric, MQ2(PPM), MQ3(PPM), MQ4(PPM), MQ5(PPM), MQ6(PPM), MQ7(PPM)"); Serial.println("Autonumeric, MQ2(R0), MQ3(R0), MQ4(R0), MQ5(R0), MQ6(R0), MQ7(R0)");
//Wait one second to continue //Wait one second to continue
delay(timeDelay/10); delay(timeDelay/10);
} }
@ -96,6 +110,7 @@ void loop() {
int lecture7 = MQ7.calibrate(); int lecture7 = MQ7.calibrate();
int lecture8 = MQ8.calibrate(); int lecture8 = MQ8.calibrate();
int lecture9 = MQ9.calibrate(); int lecture9 = MQ9.calibrate();
//Print in serial monitor //Print in serial monitor
char out[40]; char out[40];
@ -105,47 +120,11 @@ void loop() {
//Print in LCD //Print in LCD
lcd.clear(); lcd.clear();
lcd.setCursor(0,0); lcd.setCursor(0,0);
lcd.print("MQ2: "); lcd.print("**CALIBRATING***");
lcd.print(lecture2);
lcd.print(" PPM");
lcd.setCursor(0,1); lcd.setCursor(0,1);
lcd.print("MQ3: "); lcd.print("Time: " + String((millis() - time)/60000) + "min");
lcd.print(lecture3);
lcd.print(" PPM");
delay(timeDelay/4);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("MQ4: ");
lcd.print(lecture4);
lcd.print(" PPM");
lcd.setCursor(0,1);
lcd.print("MQ5: ");
lcd.print(lecture5);
lcd.print(" PPM");
delay(timeDelay/4);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("MQ6: ");
lcd.print(lecture6);
lcd.print(" PPM");
lcd.setCursor(0,1);
lcd.print("MQ7: ");
lcd.print(lecture7);
lcd.print(" PPM");
delay(timeDelay/4);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("MQ8: ");
lcd.print(lecture2);
lcd.print(" PPM");
lcd.setCursor(0,1);
lcd.print("MQ8: ");
lcd.print(lecture8);
lcd.print(" PPM");
delay(timeDelay/4);
//Increment counter //Increment counter
contador++; contador++;
//Wait to measure next sample //Wait to measure next sample
delay(timeDelay/4); delay(timeDelay/4);
} }

View File

@ -92,7 +92,7 @@
} }
void MQUnifiedsensor::setVoltResolution(float voltaje) void MQUnifiedsensor::setVoltResolution(float voltaje)
{ {
VOLT_RESOLUTION = voltaje; _VOLT_RESOLUTION = voltaje;
} }
void MQUnifiedsensor::inicializar() void MQUnifiedsensor::inicializar()
{ {
@ -125,8 +125,6 @@ void MQUnifiedsensor::setSensorCharacteristics(String nameLectureRequeired, bool
{ {
//Set default //Set default
setDefaultGas(); setDefaultGas();
//Set ratio in clean air to calc R0
setRatioInCleanAir();
//Put the default into variable internally used //Put the default into variable internally used
nameLectureRequeired = _nameLectureRequeired; nameLectureRequeired = _nameLectureRequeired;
} }
@ -151,7 +149,7 @@ int MQUnifiedsensor::readPPM(int m, int b) {
_sensor_volt = this->getVoltage(); _sensor_volt = this->getVoltage();
double RS_gas; //Define variable for sensor resistance double RS_gas; //Define variable for sensor resistance
RS_gas = ((VOLT_RESOLUTION*RLValue)/_sensor_volt)-RLValue; //Get value of RS in a gas RS_gas = ((_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_gas / this->_R0; // Get ratio RS_gas/RS_air
@ -177,7 +175,7 @@ int MQUnifiedsensor::calibrate() {
float R0; //Define variable for R0 float R0; //Define variable for R0
float sensorValue; //Define variable for analog readings float sensorValue; //Define variable for analog readings
_sensor_volt = this->getVoltage(); //Convert average to voltage _sensor_volt = this->getVoltage(); //Convert average to voltage
RS_air = ((VOLT_RESOLUTION*RLValue)/_sensor_volt)-RLValue; //Calculate RS in fresh air RS_air = ((_VOLT_RESOLUTION*_RLValue)/_sensor_volt)-_RLValue; //Calculate RS in fresh air
R0 = RS_air/_ratioInCleanAir; //Calculate R0 R0 = RS_air/_ratioInCleanAir; //Calculate R0
return R0; return R0;
} }
@ -188,7 +186,7 @@ double MQUnifiedsensor::getVoltage() {
delay(retry_interval); delay(retry_interval);
} }
double voltage = avg * VOLT_RESOLUTION / (pow(2, ADC_RESOLUTION) - 1); double voltage = avg * _VOLT_RESOLUTION / (pow(2, ADC_RESOLUTION) - 1);
return voltage; return voltage;
} }

View File

@ -8,14 +8,6 @@
//Count of posible lectures //Count of posible lectures
#define lecturesAvailable 19 #define lecturesAvailable 19
/************************Global vars************************************/
int VOLT_RESOLUTION = 5.0; // if 3.3v use 3.3
int RLValue = 10; //Value in KiloOhms
int _ratioInCleanAir, _PPM, _sensor_volt;
double RS_air, _m, _b, _ratio;
/************************Hardware Related Macros************************************/
//Index in the nameLecture vector //Index in the nameLecture vector
#define defaultMQ2 "LPG" // LPG #define defaultMQ2 "LPG" // LPG
#define defaultMQ3 "Alcohol" // Alcohol #define defaultMQ3 "Alcohol" // Alcohol
@ -68,18 +60,18 @@ double RS_air, _m, _b, _ratio;
/**********************Application Related Macros**********************************/ /**********************Application Related Macros**********************************/
/* Gas, Value of m (Slope) and b (Cut on x axis) points */ /* Gas, Value of m (Slope) and b (Cut on x axis) points */
//Values consolidated //Values consolidated
const String PROGMEM _MQ2[18] = {"H2","-2.2459","2.9845","LPG","-2.2879","2.7901","CO","-2.6208","3.6075","Alcohol","-3.1157","4.5134","Propane","-2.7028","3.5595","Benzene","-2.2879","2.7901"}; const String _MQ2[18] = {"H2","-2.2459","2.9845","LPG","-2.2879","2.7901","CO","-2.6208","3.6075","Alcohol","-3.1157","4.5134","Propane","-2.7028","3.5595","Benzene","-2.2879","2.7901"};
const String PROGMEM _MQ3[18] = {"LPG","-3.1851","4.7048","CH4","-17.531","28.785","CO","-4.339","6.4432","Alcohol","-1.435","0.4103","Benzene","-2.7009","0.632","Hexane","-2.7268","3.6299"}; const String _MQ3[18] = {"LPG","-3.1851","4.7048","CH4","-17.531","28.785","CO","-4.339","6.4432","Alcohol","-1.435","0.4103","Benzene","-2.7009","0.632","Hexane","-2.7268","3.6299"};
const String PROGMEM _MQ4[15] = {"LPG","-2.5818","3.6303","CH4","0.9873","2.6386","CO","-5.5945","5.6693","Alcohol","-11.89","9.0375","smoke","-11.189","9.0375"}; const String _MQ4[15] = {"LPG","-2.5818","3.6303","CH4","0.9873","2.6386","CO","-5.5945","5.6693","Alcohol","-11.89","9.0375","smoke","-11.189","9.0375"};
const String PROGMEM _MQ5[15] = {"H2","-4.368","2.9667","LPG","-2.5723","1.8943","CH4","-2.4438","2.3044","CO","-4.8188","5.2023","Alcohol","-4.419","4.8044"}; const String _MQ5[15] = {"H2","-4.368","2.9667","LPG","-2.5723","1.8943","CH4","-2.4438","2.3044","CO","-4.8188","5.2023","Alcohol","-4.419","4.8044"};
const String PROGMEM _MQ6[15] = {"H2","-3.6775","5.0286","LPG","-1.6567","2.8775","CH4","-1","3.301","CO","-12.791","14.523","Alcohol","-5.8057","7.5292"}; const String _MQ6[15] = {"H2","-3.6775","5.0286","LPG","-1.6567","2.8775","CH4","-1","3.301","CO","-12.791","14.523","Alcohol","-5.8057","7.5292"};
const String PROGMEM _MQ7[15] = {"H2","-1.329","1.8864","LPG","-7.8626","9.1056","CH4","-5.4878","8.8387","CO","-1.4065","2.0162","Alcohol","-6.3219","9.924"}; const String _MQ7[15] = {"H2","-1.329","1.8864","LPG","-7.8626","9.1056","CH4","-5.4878","8.8387","CO","-1.4065","2.0162","Alcohol","-6.3219","9.924"};
const String PROGMEM _MQ8[15] = {"H2","-0.7152","2.9891","LPG","-3.419","7.3513","CH4","-7.5609","15.243","CO","-7.0753","15.396","Alcohol","-1.7459","4.7575"}; const String _MQ8[15] = {"H2","-0.7152","2.9891","LPG","-3.419","7.3513","CH4","-7.5609","15.243","CO","-7.0753","15.396","Alcohol","-1.7459","4.7575"};
const String PROGMEM _MQ9[9] = {"LPG","-2.2535","2.9855","CH4","-1.6012","3.1476","CO","-1.749","2.827"}; const String _MQ9[9] = {"LPG","-2.2535","2.9855","CH4","-1.6012","3.1476","CO","-1.749","2.827"};
const String PROGMEM _MQ131[9] = {"Nox","-2.7245","3.3004","CL2","-1.0333","1.7117","O3","-1.2037","1.6455"}; const String _MQ131[9] = {"Nox","-2.7245","3.3004","CL2","-1.0333","1.7117","O3","-1.2037","1.6455"};
const String PROGMEM _MQ135[18] = {"CO","-2.7268","2.301","Alcohol","-2.8608","1.8627","CO2","-3.2819","1.9903","Tolueno","-5.7015","1.1612","NH4","-2.2119","2.0473","Acetona","-5.9682","1.0175"}; const String _MQ135[18] = {"CO","-2.7268","2.301","Alcohol","-2.8608","1.8627","CO2","-3.2819","1.9903","Tolueno","-5.7015","1.1612","NH4","-2.2119","2.0473","Acetona","-5.9682","1.0175"};
const String PROGMEM _MQ303A[9] = {"Iso-butano","-2.3543","1.144","Hydrogeno","-2.4338","0.7558","Ethanol","-2.5597","0.4436"}; const String _MQ303A[9] = {"Iso-butano","-2.3543","1.144","Hydrogeno","-2.4338","0.7558","Ethanol","-2.5597","0.4436"};
const String PROGMEM _MQ309A[12] = {"H2","-2.1311","3.0886","CH4","-1.6554","2.985","CO","-4.7623","6.7413","Alcohol","-3.7686","5.6744"}; const String _MQ309A[12] = {"H2","-2.1311","3.0886","CH4","-1.6554","2.985","CO","-4.7623","6.7413","Alcohol","-3.7686","5.6744"};
class MQUnifiedsensor class MQUnifiedsensor
{ {
@ -103,8 +95,14 @@ class MQUnifiedsensor
private: private:
int _pin, _type, _R0; /************************Private vars************************************/
int _pin, _type, _R0, _lecturePosInArray;
String _MQ[19], _nameLectureRequeired; String _MQ[19], _nameLectureRequeired;
int _VOLT_RESOLUTION = 5.0; // if 3.3v use 3.3
int _RLValue = 10; //Value in KiloOhms
int _ratioInCleanAir, _PPM, _sensor_volt;
float RS_air, _m, _b, _ratio;
}; };
#endif //MQUnifiedsensor_H #endif //MQUnifiedsensor_H