Grove - Gas Sensor(MQ2) 測試-使用LinkIt Smart 7688 Duo
****注意 LinkIt 7688 Duo 在 Arduino IDE 1.6.9 有問題 ,Arduino IDE 要用1.6.7*****
MQ2氣體感測器模塊能檢測家庭或工業區域的氣體洩漏,檢測的氣體包括異丁烷,液化石油氣,甲烷,乙醇,氣,煙霧等。感測器的響應速度快,版上有可變電阻便於調整輸出精度 。
MQ2氣體感測器模塊能檢測家庭或工業區域的氣體洩漏,檢測的氣體包括異丁烷,液化石油氣,甲烷,乙醇,氣,煙霧等。感測器的響應速度快,版上有可變電阻便於調整輸出精度 。
MQ2規格
MQ-2氣體感測器感測材料是二氧化錫,這在清潔空氣電導率較低。當空氣中有可燃氣體存在時,MQ-2的電導率更隨著可燃氣體濃度上升電導率也會升高。 用簡單的電子電路,就可轉換電導率變化對應氣體濃度的輸出信號。
MQ-2的電氣特性規格
MQ-2 的等效電路
MQ-2 的氣體濃度斜率
Grove - Gas Sensor(MQ2)
SeeedStudio 料號 : 101020055 ,包裝內容 MQ-2模組 X 1, Grove 連接線 X 1。
先把連接線的一頭插在MQ-2模組。
另一頭插在LinkIt Smart 7688 Duo 底座的 A1 ,OLED 顯示器插在I2C。
插入USB與電腦連線
軟體程式
要精準得測量MQ-2需要熱機24小時以上,每個MQ-2零件均須經過校正,本實驗值僅供參考,在網路上有很多Arduino MQ-2 的範例程式,我覺得這個範例是有經過計算且導出PPM值是一個不錯的範例 。
http://sandboxelectronics.com/?p=165
開啟 Arduino IDE 輸入以下程式碼,用複製的也可。
// ******************以下是程式碼***************//******注意這雖然是共享軟體複製時說明須留著*********
/*******************Demo for MQ-2 Gas Sensor Module V1.0*****************************
Support: Tiequan Shao: support[at]sandboxelectronics.com
Lisence: Attribution-NonCommercial-ShareAlike 3.0 Unported (CC BY-NC-SA 3.0)
Note: This piece of source code is supposed to be used as a demostration ONLY. More
sophisticated calibration is required for industrial field application.
Sandbox Electronics 2011-04-25
Story Chen 修改成 OLED 顯示
************************************************************************************/
#include <Wire.h>
#include <SeeedOLED.h>
#define MQ_PIN (A1) //設定 感測器接在 A1
#define RL_VALUE (5) //定義 RL 電阻值 5K
#define RO_CLEAN_AIR_FACTOR (9.83) //RO在清潔空氣中電阻值
#define CALIBARAION_SAMPLE_TIMES (5) //定義較準時採樣時間 ms
#define CALIBRATION_SAMPLE_INTERVAL (50) //定義較準時採樣次數
#define READ_SAMPLE_INTERVAL (10) //定義採樣次數
#define READ_SAMPLE_TIMES (5) //定義採樣間隔時間
#define GAS_LPG (0)
#define GAS_CO (1)
#define GAS_SMOKE (2)
/*****************************Globals***********************************************/
float LPGCurve[3] = {2.3,0.21,-0.47}; //LPG 的斜率
float COCurve[3] = {2.3,0.72,-0.34}; //CO的斜率
float SmokeCurve[3] ={2.3,0.53,-0.44}; //Smoke的斜率
float Ro = 10; //Ro 初始化為10K
void setup()
{
Wire.begin();
SeeedOled.init(); // 初始化OLED
Ro = MQCalibration(MQ_PIN); //校正感測器,開機時請確保空氣是乾淨的
SeeedOled.clearDisplay(); //清除OLED
SeeedOled.setNormalDisplay(); //設定OLED 正常顯示
SeeedOled.setPageMode(); //設定為頁模式
}
void loop()
{
SeeedOled.clearDisplay(); //清除OLED
SeeedOled.setTextXY(0,0); //OLED 游標移至0.0
SeeedOled.putString("LPG: "); //顯示字元 LPG:
SeeedOled.putNumber(MQGetGasPercentage(MQRead(MQ_PIN)/Ro,GAS_LPG) ); //顯示運算後LPG 的值
SeeedOled.putString("ppm"); //顯示字元 ppm
SeeedOled.setTextXY(2,0); //OLED 游標移至2.0
SeeedOled.putString("CO: "); //顯示字元 CO:
SeeedOled.putNumber(MQGetGasPercentage(MQRead(MQ_PIN)/Ro,GAS_CO) ); //顯示運算後CO 的值
SeeedOled.putString("ppm"); //顯示字元 ppm
SeeedOled.setTextXY(4,0); //OLED 游標移至4.0
SeeedOled.putString("SMOKE: "); //顯示字元 SMOKE:
SeeedOled.putNumber(MQGetGasPercentage(MQRead(MQ_PIN)/Ro,GAS_SMOKE) ); //顯示運算後SMOKE 的值
SeeedOled.putString("ppm"); //顯示字元 ppm
delay(1000);
}
/****************** MQResistanceCalculation ****************************************
Input: raw_adc - raw value read from adc, which represents the voltage
Output: the calculated sensor resistance
Remarks: The sensor and the load resistor forms a voltage divider. Given the voltage
across the load resistor and its resistance, the resistance of the sensor
could be derived.
************************************************************************************/
float MQResistanceCalculation(int raw_adc)
{
return ( ((float)RL_VALUE*(1023-raw_adc)/raw_adc));
}
/***************************** MQCalibration ****************************************
Input: mq_pin - analog channel
Output: Ro of the sensor
Remarks: This function assumes that the sensor is in clean air. It use
MQResistanceCalculation to calculates the sensor resistance in clean air
and then divides it with RO_CLEAN_AIR_FACTOR. RO_CLEAN_AIR_FACTOR is about
10, which differs slightly between different sensors.
************************************************************************************/
float MQCalibration(int mq_pin)
{
int i;
float val=0;
for (i=0;i<CALIBARAION_SAMPLE_TIMES;i++) { //take multiple samples
val += MQResistanceCalculation(analogRead(mq_pin));
delay(CALIBRATION_SAMPLE_INTERVAL);
}
val = val/CALIBARAION_SAMPLE_TIMES; //calculate the average value
val = val/RO_CLEAN_AIR_FACTOR; //divided by RO_CLEAN_AIR_FACTOR yields the Ro
//according to the chart in the datasheet
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.
************************************************************************************/
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;
}
/***************************** MQGetGasPercentage **********************************
Input: rs_ro_ratio - Rs divided by Ro
gas_id - target gas type
Output: ppm of the target gas
Remarks: This function passes different curves to the MQGetPercentage function which
calculates the ppm (parts per million) of the target gas.
************************************************************************************/
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;
}
/***************************** MQGetPercentage **********************************
Input: rs_ro_ratio - Rs divided by Ro
pcurve - pointer to the curve of the target gas
Output: ppm of the target gas
Remarks: By using the slope and a point of the line. The x(logarithmic value of ppm)
of the line could be derived if y(rs_ro_ratio) is provided. As it is a
logarithmic coordinate, power of 10 is used to convert the result to non-logarithmic
value.
************************************************************************************/
int MQGetPercentage(float rs_ro_ratio, float *pcurve)
{
return (pow(10,( ((log(rs_ro_ratio)-pcurve[1])/pcurve[2]) + pcurve[0])));
}
//******程式結束*****
確認 LinkIt Smart 7688 Duo 的連接埠。
第一次會問存檔填入MQ-2。
上傳中。
上傳完成。
上傳完成後 OLED 會開始顯示感測值正常都是零, 用打火機噴一下氣體(不可用火)顯示值就會改變。
參考資料 : Sandbox Electronics http://sandboxelectronics.com/?p=165
聯發科創意實驗室 http://home.labs.mediatek.com/
Seeed Technology http://www.seeedstudio.com/
您好! 我不是本科系的,想請你幫我組一個MQ-2氣體偵測器,可以顯示讀值材料費 加工費 運費我都可以付給你!
回覆刪除不知您是否可以幫忙。
作者已經移除這則留言。
回覆刪除请问 这个值 #define RL_VALUE (5) 需要在传感器上的电阻调吗?
回覆刪除