範例碼(一):煙霧感測器(MQ-2)。

#include <MQUnifiedsensor.h>
/************************Hardware Related Macros************************************/
#define         Board                   ("Arduino UNO")
#define         Pin                     (33)  //Analog input 33(GIOP) of your arduino
/***********************Software Related Macros************************************/
#define         Type                    ("MQ-2") //MQ2
#define         Voltage_Resolution      (5)
#define         ADC_Bit_Resolution      (10) // For arduino UNO/MEGA/NANO
#define         RatioMQ2CleanAir        (9.83) //RS / R0 = 9.83 ppm 

/*****************************Globals***********************************************/
MQUnifiedsensor MQ2(Board, Voltage_Resolution, ADC_Bit_Resolution, Pin, Type);
/*****************************Globals***********************************************/

void setup() {
  Serial.begin(115200);

  // Set math model to calculate the PPM concentration and the value of constants
  MQ2.setRegressionMethod(1); //_PPM =  a*ratio^b

  // Configurate the ecuation values to get LPG concentration
  // 設定方程式的 A 與 B 值, 取得 LPG 濃度值
  MQ2.setA(574.25); 
  MQ2.setB(-2.222); 
  /*
    Exponential regression:
    Gas    | a      | b
    H2     | 987.99 | -2.162
    LPG    | 574.25 | -2.222
    CO     | 36974  | -3.109
    Alcohol| 3616.1 | -2.675
    Propane| 658.71 | -2.168
  */

  /*****************************  MQ Init ********************************************/ 
  //Remarks: Configure the pin of arduino as input.
  /************************************************************************************/ 
  MQ2.init(); 
  /* 
    //If the RL value is different from 10K please assign your RL value with the following method:
    MQ2.setRL(10);
  */
  /*****************************  MQ CAlibration ********************************************/ 
  // Explanation: 
  // In this routine the sensor will measure the resistance of the sensor supposing before was pre-heated
  // and now is on clean air (Calibration conditions), and it will setup R0 value.
  // We recomend execute this routine only on setup or on the laboratory and save on the eeprom of your arduino
  // This routine not need to execute to every restart, you can load your R0 if you know the value
  // Acknowledgements: https://jayconsystems.com/blog/understanding-a-gas-sensor
  Serial.print("Calibrating please wait.");
  
  float calcR0 = 0;
  
  for(int i = 1; i<=10; i ++)
  {
    // Update data, the arduino will be read the voltage on the analog pin
    // 更新數據
    MQ2.update(); 
    
    calcR0 += MQ2.calibrate(RatioMQ2CleanAir);
    
    Serial.print(".");
  }
  
  MQ2.setR0(calcR0/10);
  
  Serial.println("  done!.");
  
  if(isinf(calcR0)) {
    Serial.println("Warning: Conection issue founded, R0 is infite (Open circuit detected) please check your wiring and supply");
    
    while(1);
    }
    
  if(calcR0 == 0){
    Serial.println("Warning: Conection issue founded, R0 is zero (Analog pin with short circuit to ground) please check your wiring and supply"); 
    
    while(1);
    }
  /*****************************  MQ CAlibration ********************************************/ 

  MQ2.serialDebug(true);
}

void loop() {
  // Update data, the arduino will be read the voltage on the analog pin
  // 更新數據
  MQ2.update(); 

  // Sensor will read PPM concentration using the model and a and b values setted before or in the setup
  // 根據方程式設定的 A 與 B 值, 來取得 PPM 濃度
  float PPM = MQ2.readSensor(); 
  Serial.print("PPM = ");Serial.println(PPM);
  // Will print the table on the serial port
  MQ2.serialDebug(); 

  delay(500); 
}
文章標籤
全站熱搜
創作者介紹
創作者 mitblog 的頭像
mitblog

香腸炒章魚

mitblog 發表在 痞客邦 留言(0) 人氣(0)