2017년 8월 8일 화요일

MQ-2 가스센서 사용하기

가스센서중에 MQ-2 센서는 메탄, 알콜, CO, 1프로판, LPG 가스는 물론 연기까지 감지가 가능해서 가스 누출 경보기, 화재경보기로

응용이 가능할 듯 합니다. 

다음 링크에 설명이 잘되어 있는데요. http://www.seeedstudio.com/wiki/Grove_-_Gas_Sensor%28MQ2%29

 처음에 자기 집의 깨끗한 환경에서 먼저 센서 평균 R0 값을 구합니다.

void setup() {
  Serial.begin(9600);
}
 
void loop() {
  float sensor_volt; 
  float RS_air; //  Get the value of RS via in a clear air
  float R0;  // Get the value of R0 via in H2
  float sensorValue;
 
/*--- Get a average data by testing 100 times ---*/   
    for(int x = 0 ; x < 100 ; x++)
  {
    sensorValue = sensorValue + analogRead(A0);
  }
  sensorValue = sensorValue/100.0;
/*-----------------------------------------------*/
 
  sensor_volt = sensorValue/1024*5.0;
  RS_air = (5.0-sensor_volt)/sensor_volt; // omit *RL
  R0 = RS_air/10.0; // The ratio of RS/R0 is 10 in a clear air
 
  Serial.print("sensor_volt = ");
  Serial.print(sensor_volt);
  Serial.println("V");
 
  Serial.print("R0 = ");
  Serial.println(R0);
  delay(1000);
 
}
그 다음에 아래 코드의 R0 값을 위에서 구한 값으로 대치합니다.

제 방에서 구한 값이 평균 1.5 정도 나와서 그 값으로 교체했습니다.
void setup() {
  Serial.begin(9600);
}
 
void loop() {
  float R0 = 1.5;//미리 측정한 값
  float sensor_volt;
  float RS_gas; // Get value of RS in a GAS
  float ratio; // Get ratio RS_GAS/RS_air
  int sensorValue = analogRead(A0);
  sensor_volt=(float)sensorValue/1024*5.0;
  RS_gas = (5.0-sensor_volt)/sensor_volt; // omit *RL
 
  /*-Replace the name "R0" with the value of R0 in the demo of First Test -*/
  ratio = RS_gas/R0;  // ratio = RS/R0 
  /*-----------------------------------------------------------------------*/
 
  Serial.print("sensor_volt = ");
  Serial.println(sensor_volt);
  Serial.print("RS_ratio = ");
  Serial.println(RS_gas);
  Serial.print("Rs/R0 = ");
  Serial.println(ratio);
 
  Serial.print("\n\n");
 
  delay(1000);
 
}
센서에는 아나로그 값과 디지털값이 둘다 출력가능하며 아나로그 값은 데이타시트의 아래 그래프처럼 일반적인 공기는 10 언저리를 유지하다가

가스가 감지되면 값이 내려 갑니다.  디지털값은 처음에는  HIGH 값이다가 감지되면 LOW 값이 출력됩니다. 감도는 센서의 저항을 조정하여

조절 가능합니다.





댓글 없음:

댓글 쓰기