Arduino MQ135气体感应器

Arduino MQ135气体感应器

一、接线

    MQ135有4个引脚,分别为VCC、GND、A0、D0,这里只需要使用到VCC、GND、A0.以下图是esp8266接线图。由于测量气体值PPM时,跟空气的温湿度是相关的。所以我们这里加了一个DHT22。DHT22传送门

VCC->3V

GND->G

A0->A0

二、MQ135库

    下载地址:https://github.com/zhao007z4/MQ135

    下载完成后,拷贝到arduino库目录下

三、代码

#define DHT_TYPE     DHT11
#define DHT_PIN      2  //pin d4

#define ANALOGPIN    A0

DHT dht(DHT_PIN, DHT_TYPE);

MQ135 gasSensor = MQ135(ANALOGPIN);

void setup() {
  Serial.begin(9600);
  dht.begin(); 

}

void loop() {
    float h = dht.readHumidity();
    // Read temperature as Celsius (the default)
    float t = dht.readTemperature();
    // Check if any reads failed and exit early (to try again).
    if (isnan(h) || isnan(t))
    {
      Serial.println("Failed to read from DHT sensor!");
      delay(1000);
      return;
    }
    Serial.print("hum=");
    Serial.print(h); // this to display the rzero value continuously, uncomment this to get ppm value
    Serial.println("%");
    Serial.print("temp=");
    Serial.print(t); // this to display the rzero value continuously, uncomment this to get ppm value
    Serial.println("C");
    
    rzero = gasSensor.getRZero(); //this to get the rzero value, uncomment this to get ppm value
    Serial.print("RZero=");
    Serial.println(rzero); // this to display the rzero value continuously, uncomment this to get ppm value
     
    ppm = gasSensor.getPPM(); // this to get ppm value, uncomment this to get rzero value
    Serial.print("PPM=");
    Serial.println(ppm); // this to display the ppm value continuously, uncomment this to get rzero value
    
    ppmbalanced = gasSensor.getCorrectedPPM(t, h); // this to get ppm value, uncomment this to get rzero value
    Serial.print("PPM Corrected=");
    Serial.println(ppmbalanced); // this to display the ppm value continuously, uncomment this to get rzero value

四、总结

从MQ135文档中,我们了解到,MQ135要正常工作,需要预热不少于48小时。中文文档传送门