Arduino Code
Arduino Code
/****************************Globals**********************************************/
float LPGCurve[3] = {2.3,0.21,-0.47}; //two points are taken from the
curve.
//with these two points, a
line is formed which is "approximately equivalent"
//to the original curve.
//data format:{ x, y, slope};
point1: (lg200, 0.21), point2: (lg10000, -0.59)
float COCurve[3] = {2.3,0.72,-0.34}; //two points are taken from the
curve.
//with these two points, a
line is formed which is "approximately equivalent"
//to the original curve.
//data format:{ x, y, slope};
point1: (lg200, 0.72), point2: (lg10000, 0.15)
float SmokeCurve[3] ={2.3,0.53,-0.44}; //two points are taken from the
curve.
//with these two points, a
line is formed which is "approximately equivalent"
//to the original curve.
//data format:{ x, y, slope};
point1: (lg200, 0.53), point2: (lg10000, -0.22)
void setup()
{
Serial.begin(9600); //UART setup, baudrate =
9600bps
Serial.print("Calibrating...\
");
Ro = MQCalibration(MQ_PIN); //Calibrating the sensor.
Please make sure the sensor is in clean air
lcd.begin(16, 2); //when you
perform the calibration
Serial.print("Calibration is done...\
");
Serial.print("Ro=");
Serial.print(Ro);
Serial.print("kohm");
Serial.print("\
");
lcd.print("Calibration is done...\
");
lcd.print("Ro=");
lcd.print(Ro);
lcd.print("kohm");
lcd.print("\
");
}
void loop()
{
Serial.print("LPG:");
Serial.print(MQGetGasPercentage(MQRead(MQ_PIN)/Ro,GAS_LPG) );
Serial.print( "ppm" );
Serial.print(" ");
Serial.print("CO:");
Serial.print(MQGetGasPercentage(MQRead(MQ_PIN)/Ro,GAS_CO) );
Serial.print( "ppm" );
Serial.print(" ");
Serial.print("SMOKE:");
Serial.print(MQGetGasPercentage(MQRead(MQ_PIN)/Ro,GAS_SMOKE) );
Serial.print( "ppm" );
Serial.print("\
");
lcd.setCursor(0, 0);
lcd.print("LPG:");
lcd.print(MQGetGasPercentage(MQRead(MQ_PIN)/Ro,GAS_LPG) );
//lcd.print( "ppm" );
lcd.print(" ");
lcd.setCursor(9, 0);
lcd.print("CO:");
lcd.print(MQGetGasPercentage(MQRead(MQ_PIN)/Ro,GAS_CO) );
//lcd.print( "ppm" );
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print("SMOKE:");
lcd.print(MQGetGasPercentage(MQRead(MQ_PIN)/Ro,GAS_SMOKE) );
//lcd.print( "ppm" );
lcd.print(" ");
delay(200);
}
return val;
}
/*************************** MQRead *******************************************
Input: mq_pin - analog channel
Output: Rs of the sensor
Remarks: This function use MQResistanceCalculation to caculate the sensor
resistenc (Rs).
The Rs changes as the sensor is in the different consentration of the
target
gas. The sample times and the time interval between samples could be
configured
by changing the definition of the macros.
**********************************************************************************/
for (i=0;i<READ_SAMPLE_TIMES;i++) {
rs += MQResistanceCalculation(analogRead(mq_pin));
delay(READ_SAMPLE_INTERVAL);
}
rs = rs/READ_SAMPLE_TIMES;
return rs;
}
return 0;
}