MQ8 gas sensor

parent 8eebe9c6
#define MQ_PIN (0)
#define RL_VALUE (10)
#define RO_CLEAN_AIR_FACTOR (9.21)
#define CALIBARAION_SAMPLE_TIMES (50)
#define CALIBRATION_SAMPLE_INTERVAL (500)
#define READ_SAMPLE_INTERVAL (50)
#define READ_SAMPLE_TIMES (5)
#define GAS_H2 (0)
float H2Curve[3] = {2.3, 0.93,-1.44};
float Ro = 10;
void setup() {
Serial.begin(9600);
Serial.print("Calibrating...\n");
Ro = MQCalibration(MQ_PIN);
Serial.print("Calibration is done...\n");
Serial.print("Ro=");
Serial.print(Ro);
Serial.print("kohm");
Serial.print("\n");
}
void loop() {
Serial.print("H2 Concentration:");
Serial.print(MQGetGasPercentage(MQRead(MQ_PIN)/Ro,GAS_H2) );
Serial.print( "ppm" );
Serial.print("\n");
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<CALIBARAION_SAMPLE_TIMES;i++) {
val += MQResistanceCalculation(analogRead(mq_pin));
delay(CALIBRATION_SAMPLE_INTERVAL);
}
val = val/CALIBARAION_SAMPLE_TIMES;
val = val/RO_CLEAN_AIR_FACTOR;
return val;
}
float MQRead(int mq_pin) {
int i;
float rs=0;
for (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_H2) {
return MQGetPercentage(rs_ro_ratio,H2Curve);
}
return 0;
}
int MQGetPercentage(float rs_ro_ratio, float *pcurve) {
return (pow(10,( ((log(rs_ro_ratio)-pcurve[1])/pcurve[2]) + pcurve[0])));
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment