A library for the MQ2 sensor. Based on https://github.com/labay11 and http://sandboxelectronics.com/?p=165

Dependents:   mq2_example mq2_midtermproject ECE595_Group9_FinalProject mq2_example ... more

Revision:
5:8b7ba76a7a76
Parent:
4:48545e508081
Child:
6:c0ebeb4b47b6
--- a/MQ2.cpp	Fri Oct 06 08:34:50 2017 +0000
+++ b/MQ2.cpp	Fri Oct 06 08:57:00 2017 +0000
@@ -43,8 +43,8 @@
          across the load resistor and its resistance, the resistance of the sensor
          could be derived.
 ************************************************************************************/ 
-float MQ2::MQResistanceCalculation(float raw_adc) {
-   return (((float)RL_VALUE*(1-raw_adc)/raw_adc));
+float MQ2::MQResistanceCalculation(int raw_adc) {
+   return (((float)RL_VALUE*(1023-raw_adc)/raw_adc));
 }
 
 /***************************** MQCalibration ****************************************
@@ -58,7 +58,7 @@
 float MQ2::MQCalibration() {                                                            // This should be done in 'clean air'
   float val=0;
   for (int i=0;i<CALIBARAION_SAMPLE_TIMES;i++) {                                        //take multiple samples
-    val += MQResistanceCalculation(_pin.read());
+    val += MQResistanceCalculation(_pin.read_u16()>>6);
     wait_ms(CALIBRATION_SAMPLE_INTERVAL);
   }
   val = val/CALIBARAION_SAMPLE_TIMES;                                                   //calculate the average value
@@ -78,7 +78,7 @@
   int i;
   float rs=0;
   for (i=0;i<READ_SAMPLE_TIMES;i++) {
-    rs += MQResistanceCalculation(_pin.read());
+    rs += MQResistanceCalculation(_pin.read_16>>6());
     wait_ms(READ_SAMPLE_INTERVAL);
   }
   rs = rs/READ_SAMPLE_TIMES;