/*
MQUnifiedsensor Library - calibrating an MQ2 to MQ9
Demonstrates the use a MQ2 sensor.
Library originally added 01 may 2019
by Miguel A Califa, Yersson Carrillo, Ghiordy Contreras, Mario Rodriguez
Added example
modified 27 May 2019
by Miguel Califa
Wiring:
https://github.com/miguel5612/MQSensorsLib_Docs/blob/master/static/img/MQ_Arduino.PNG
Please make sure arduino A0 pin represents the analog input configured on #define pin
This example code is in the public domain.
*/
//Include the library
#include <MQUnifiedsensor.h>
/************************Hardware Related Macros************************************/
#define Board ("ESP 32")
#define Pin2 (A4) //Analog input 2 of your arduino
#define Pin3 (A0) //Analog input 3 of your arduino
#define PWMPin (5) // Pin connected to mosfet
/***********************Software Related Macros************************************/
#define RatioMQ2CleanAir (9.83) //RS / R0 = 9.83 ppm
#define RatioMQ3CleanAir (60) //RS / R0 = 60 ppm
#define RatioMQ4CleanAir (4.4) //RS / R0 = 4.4 ppm
#define RatioMQ5CleanAir (6.5) //RS / R0 = 6.5 ppm
#define RatioMQ6CleanAir (10) //RS / R0 = 10 ppm
#define RatioMQ7CleanAir (27.5) //RS / R0 = 27.5 ppm
#define RatioMQ8CleanAir (70) //RS / R0 = 70 ppm
#define RatioMQ9CleanAir (9.6) //RS / R0 = 9.6 ppm
#define ADC_Bit_Resolution (10) // 10 bit ADC
#define Voltage_Resolution (5) // Volt resolution to calc the voltage
#define Type ("ESP 32") //Board used
/*****************************Globals***********************************************/
//Declare Sensor
MQUnifiedsensor MQ4(Board, Voltage_Resolution, ADC_Bit_Resolution, Pin2, Type);
MQUnifiedsensor MQ7(Board, Voltage_Resolution, ADC_Bit_Resolution, Pin3, Type);
unsigned long oldTime = 0;
void setup() {
//Init serial port
Serial.begin(9600);
//init the sensor
MQ4.init();
MQ4.setRegressionMethod(1); //_PPM = a*ratio^b
MQ4.setA(1012.7); MQ4.setB(-2.786); // Configure the equation to to calculate CH4 concentration
MQ4.setR0(3.86018237);
MQ7.init();
MQ7.setRegressionMethod(1); //_PPM = a*ratio^b
MQ7.setA(99.042); MQ7.setB(-1.518); // Configure the equation to calculate CO concentration value
MQ7.setR0(4);
/***************************** MQ CAlibration ********************************************/
// Explanation:
// In this routine the sensor will measure the resistance of the sensor supposedly before being pre-heated
// and on clean air (Calibration conditions), setting up R0 value.
// We recomend executing this routine only on setup in laboratory conditions.
// This routine does not need to be executed on each restart, you can load your R0 value from eeprom.
// Acknowledgements: https://jayconsystems.com/blog/understanding-a-gas-sensor
Serial.print("Calibrating please wait.");
float MQ4calcR0 = 0;
float MQ7calcR0 = 0;
for(int i = 1; i<=10; i ++)
{
//Update the voltage Values
MQ4.update();
MQ7.update();
MQ4calcR0 += MQ4.calibrate(RatioMQ4CleanAir);
MQ7calcR0 += MQ7.calibrate(RatioMQ7CleanAir);
Serial.print(".");
}
MQ4.setR0(MQ4calcR0/20);
MQ7.setR0(MQ7calcR0/20);
Serial.println(" done!.");
Serial.print("Valores de R0 para cada sensor (MQ2 - MQ9):");
Serial.print(MQ4calcR0/10); Serial.print(" | ");
Serial.print(MQ7calcR0/10); Serial.print(" | ");
//if(isinf(isinf(MQ4calcR0) || isinf(MQ7calcR0)) {Serial.println("Warning: Conection issue founded, R0 is infite (Open circuit detected) please check your wiring and supply"); while(1);}
//if((MQ4calcR0 == 0) || (MQ7calcR0 == 0)){Serial.println("Warning: Conection issue founded, R0 is zero (Analog pin with short circuit to ground) please check your wiring and supply"); while(1);}
/***************************** MQ CAlibration ********************************************/
//Print in serial monitor
Serial.println("MQ2 to MQ9 - lecture");
Serial.println("*************************** Values from MQ-board ***************************");
Serial.println("| LPG | Alcohol | CH4 | Alcohol | CH4 | CO | H2 | LPG |");
Serial.println("| MQ-2 | MQ-3 | MQ-4 | MQ-5 | MQ-6 | MQ-7 | MQ-8 | MQ-9 |");
//pinMode(calibration_button, INPUT);
}
void loop() {
oldTime = millis();
while(millis() - oldTime <= (60*1000))
{
// VH 5 Volts
analogWrite(5, 255); // 255 is DC 5V output
readAllSensors();
delay(500);
}
// 90s cycle
oldTime = millis();
while(millis() - oldTime <= (90*1000))
{
// VH 1.4 Volts
analogWrite(5, 20); // 255 is 100%, 20.4 is aprox 8% of Duty cycle for 90s
readAllSensors();
delay(500);
}
}
void readAllSensors()
{
//Update the voltage Values
MQ4.update();
MQ7.update();
//Read the sensor and print in serial port
float MQ4Lecture = MQ4.readSensor();
float MQ7Lecture = MQ7.readSensor();
Serial.print(" | "); Serial.print(MQ4Lecture);
Serial.print(" | "); Serial.print(MQ7Lecture);
Serial.println("|");
}