Fc-22 Gas sensor Arduino
From wikiluntti
Introduction
MQ-2, Mq2, mq22, mq-22.
Datasheet to approximate the gases
Theory
Connect Vcc, GND and Analog Output to Arduino Analog A0 pin (see https://www.instructables.com/How-to-use-MQ2-Gas-Sensor-Arduino-Tutorial/). Also, there is Digital Output.
#include "MQ135.h"
MQ135 gasSensor = MQ135(A5);
int ppm = gasSensor.getPPM();
void setup() {
Serial.begin(9600);
}
void loop() {
float rzero = gasSensor.getRZero(); #Calibaration. Actually it takes ~24h time.
float ppm = gasSensor.getPPM();
Serial.println(ppm);
delay(1000);
}
See https://github.com/Ravenneo/CO2-sensor
Calibration https://hackaday.io/project/3475-sniffing-trinket/log/12363-mq135-arduino-library
Exercises
Code 1
A better code from (https://projecthub.arduino.cc/m_karim02/arduino-and-mq2-gas-sensor-f3ae33) but the MQGetGasPercentage does not work properly.
#define MQ_PIN (0)
#define RL_VALUE (5) //the load resistance on the board in kilo ohms
#define RO_CLEAN_AIR_FACTOR (9.83) //RO_CLEAR_AIR_FACTOR=(Sensor resistance in clean air) is derived from the chart in datasheet
#define CALIBRATION_SAMPLE_TIMES (50) // the calibration
#define CALIBRATION_SAMPLE_INTERVAL (500) //the time interal(in milisecond) between each samples in the
#define READ_SAMPLE_INTERVAL (50) //
#define READ_SAMPLE_TIMES (5) //
//const int rs = 12;
#define GAS_LPG (0)
#define GAS_CO (1)
#define GAS_SMOKE (2)
float LPGCurve[3] = {2.3,0.21,-0.47}; //From the datasheet
float COCurve[3] = {2.3,0.72,-0.34};
float SmokeCurve[3] = {2.3,0.53,-0.44};
float Ro = 10; //Ro is initialized to 10 kilo ohms
void setup(){
Serial.begin(9600);
Serial.print("Calibrating...");
Ro = MQCalibration(MQ_PIN); //Calibrating the sensor. Please make sure the sensor is in clean air
Serial.print("Calibration is done... ");
Serial.print("Ro=");
Serial.print(Ro);
Serial.print("kohm");
Serial.println("");
}
void loop(){
Serial.print("LPG:");
Serial.print("AAA ");
Serial.print( MQRead(MQ_PIN));
Serial.print(" ");
Serial.print(MQGetGasPercentage(MQRead(MQ_PIN)/Ro,GAS_LPG) );
Serial.println( " ppm" );
/*Serial.print("\t");
Serial.print("CO:");
Serial.print(MQGetGasPercentage(MQRead(MQ_PIN)/Ro,GAS_CO) );
Serial.print( " ppm" );
Serial.print("\t");
Serial.print("SMOKE:");
Serial.print(MQGetGasPercentage(MQRead(MQ_PIN)/Ro,GAS_SMOKE) );
Serial.print( " ppm" );
Serial.println("");
*/
delay(200);
}
float MQResistanceCalculation(int raw_adc){
return ( ((float)RL_VALUE*(1023-raw_adc)/raw_adc));
}
float MQCalibration(int mq_pin){
int i;
float val=0;
for (i=0;i<CALIBRATION_SAMPLE_TIMES;i++) { //take multiple samples
val += MQResistanceCalculation(analogRead(mq_pin));
delay(CALIBRATION_SAMPLE_INTERVAL);
}
val = val/CALIBRATION_SAMPLE_TIMES; // the average value
val = val/RO_CLEAN_AIR_FACTOR; //yields the Ro according to the chart in the datasheet
return val;
}
float MQRead(int mq_pin){
float rs=0;
for (int i=0;i<READ_SAMPLE_TIMES;i++) {
rs += MQResistanceCalculation(analogRead(mq_pin));
delay(READ_SAMPLE_INTERVAL);
}
rs = rs/READ_SAMPLE_TIMES;
return rs;
}
int MQGetGasPercentage(float rs_ro_ratio, int gas_id){
if ( gas_id == GAS_LPG ) {
return MQGetPercentage(rs_ro_ratio, LPGCurve);
} else if ( gas_id == GAS_CO ) {
return MQGetPercentage(rs_ro_ratio, COCurve);
} else if ( gas_id == GAS_SMOKE ) {
return MQGetPercentage(rs_ro_ratio, SmokeCurve);
}
return 0;
}
int MQGetPercentage(float rs_ro_ratio, float *pcurve){
return (pow(10,( ((log(rs_ro_ratio)-pcurve[1])/pcurve[2]) + pcurve[0])));
}