#include <MQUnifiedsensor.h>
#define Board "ESP32"
#define Voltage_Resolution 5
#define pin A0 //Analog input 0 of your esp
#define type "MQ-7" //MQ7
#define ADC_Bit_Resolution 12 // esp
#define RatioMQ7CleanAir 27.5 //RS / R0 = 27.5 ppm
//Declare Sensor
MQUnifiedsensor MQ7(Board, Voltage_Resolution, ADC_Bit_Resolution, pin, type);
void setup() {
Serial.begin(9600);
MQ7.setRegressionMethod(1); //_PPM = a*ratio^b
MQ7.setA(99.042); MQ7.setB(-1.518); // Configure the equation to calculate CO concentration value
/*
Exponential regression:
GAS | a | b
H2 | 69.014 | -1.374
LPG | 700000000 | -7.703
CH4 | 60000000000000 | -10.54
CO | 99.042 | -1.518
Alcohol | 40000000000000000 | -12.35
*/
MQ7.init();
Serial.print("Calibrating please wait.");
float calcR0 = 0;
for(int i = 1; i<=10; i ++)
{
MQ7.update(); // Update data,read the voltage from the analog pin
calcR0 += MQ7.calibrate(RatioMQ7CleanAir);
Serial.print(".");
}
MQ7.setR0(calcR0/10);
Serial.println(" done!.");
MQ7.serialDebug(true);
}
void loop() {
MQ7.update();
int adc = analogRead(pin);
float ppm = MQ7.readSensor(); // Sensor will read PPM concentration using the model, a and b values set previously or from the setup
Serial.print("ADC: ");
Serial.print(adc);
Serial.print("\t");
Serial.print(" CO (ppm): ");
Serial.println(ppm);
delay(500);
}