<noscript id="mmkmi"><source id="mmkmi"></source></noscript>
  • <noscript id="mmkmi"><kbd id="mmkmi"></kbd></noscript>
  • <table id="mmkmi"><source id="mmkmi"></source></table>
  • Arduino連接夏普GP2Y1010AU0F空氣質量傳感器檢測PM2.5

    作者:楚風狂 | 更新時間:2016-06-01 | 瀏覽量:6351


    夏普GP2Y1010AU0F圖片



    GP2Y1010AU0F引腳圖

    序號名稱作用
    1V-LEDLED供電
    2LED-GNDLED GND
    3LEDLED控制
    4S-GND傳感器GND
    5VO檢測結果電壓模擬量輸出
    6VCC供電


    GP2Y1010AU0F接線圖



    接線圖.jpg



    程序代碼


    /*
       Arduino連接夏普GP2Y1010AU0F空氣質量傳感器檢測PM2.5
       些程序也適用于用 Arduino IDE 開發 ESP8266
    */
    
    /* 定義引腳 */
    
     #define PIN_DATA_OUT A0 //連接空氣質量傳感器模擬量輸出的IO口
    
    
     #define PIN_LED_VCC 2 //空氣質量傳感器中為內部Led供電的引腳
    
     /* 定義時間 */
     const int DELAY_BEFORE_SAMPLING = 280; //采樣前等待時間
     const int DELAY_AFTER_SAMPLING = 40; //采樣后等待時間
     const int DELAY_LED_OFF = 9680; //間隔時間
    
    /**
       讀取輸出電壓
    */
    double getOutputV() {
      digitalWrite(PIN_LED_VCC, LOW);
      delayMicroseconds(DELAY_BEFORE_SAMPLING);
      double analogOutput = analogRead(PIN_DATA_OUT);
      delayMicroseconds(DELAY_AFTER_SAMPLING);
      digitalWrite(PIN_LED_VCC, HIGH);
      delayMicroseconds(DELAY_LED_OFF);
      //Arduino模擬量讀取值的范圍為0~1023,以下換算為0~5v
      double outputV = analogOutput / 1024 * 5;
      return outputV;
    }
    
    /**
       根據輸出電壓計算灰塵密度
    */
    double getDustDensity(double outputV) {
      //輸出電壓和灰塵密度換算公式: ug/m3 = (V - 0.9) / 5 * 1000
      double ugm3 = (outputV - 0.9) / 5 * 1000;
      //去除檢測不到的范圍
      if (ugm3 < 0) {
        ugm3 = 0;
      }
      return ugm3;
    }
    
    /**
       根據灰塵密度計算AQI
       環境空氣質量指數(AQI)技術規定(試行)](http://kjs.mep.gov.cn/hjbhbz/bzwb/dqhjbh/jcgfffbz/201203/t20120302_224166.htm
    */
    double getAQI(double ugm3) {
      double aqiL = 0;
      double aqiH = 0;
      double bpL = 0;
      double bpH = 0;
      double aqi = 0;
      //根據pm2.5和aqi對應關系分別計算aqi
      if (ugm3 >= 0 && ugm3 <= 35) {
        aqiL = 0;
        aqiH = 50;
        bpL = 0;
        bpH = 35;
      } else if (ugm3 > 35 && ugm3 <= 75) {
        aqiL = 50;
        aqiH = 100;
        bpL = 35;
        bpH = 75;
      } else if (ugm3 > 75 && ugm3 <= 115) {
        aqiL = 100;
        aqiH = 150;
        bpL = 75;
        bpH = 115;
      } else if (ugm3 > 115 && ugm3 <= 150) {
        aqiL = 150;
        aqiH = 200;
        bpL = 115;
        bpH = 150;
      } else if (ugm3 > 150 && ugm3 <= 250) {
        aqiL = 200;
        aqiH = 300;
        bpL = 150;
        bpH = 250;
      } else if (ugm3 > 250 && ugm3 <= 350) {
        aqiL = 300;
        aqiH = 400;
        bpL = 250;
        bpH = 350;
      } else if (ugm3 > 350) {
        aqiL = 400;
        aqiH = 500;
        bpL = 350;
        bpH = 500;
      }
      //公式aqi = (aqiH - aqiL) / (bpH - bpL) * (desity - bpL) + aqiL;
      aqi = (aqiH - aqiL) / (bpH - bpL) * (ugm3 - bpL) + aqiL;
      return aqi;
    }
    
    /**
       根據aqi獲取級別描述
    */
    String getGradeInfo(double aqi) {
      String gradeInfo;
      if (aqi >= 0 && aqi <= 50) {
        gradeInfo = String("Perfect");
      } else if (aqi > 50 && aqi <= 100) {
        gradeInfo = String("Good");
      } else if (aqi > 100 && aqi <= 150) {
        gradeInfo = String("Mild polluted");
      } else if (aqi > 150 && aqi <= 200) {
        gradeInfo = String("Medium polluted");
      } else if (aqi > 200 && aqi <= 300) {
        gradeInfo = String("Heavily polluted");
      } else if (aqi > 300 && aqi <= 500) {
        gradeInfo = String("Severely polluted");
      } else {
        gradeInfo = String("Broken roof!!!");
      }
      return gradeInfo;
    }
    
    void setup() {
      Serial.begin(115200);
      pinMode(PIN_DATA_OUT, INPUT); //定義為輸入(ADC讀取模擬量)
      pinMode(PIN_LED_VCC, OUTPUT); //定義為輸出
    }
    
    void loop() {
      double outputV = getOutputV(); //采樣獲取輸出電壓
      double ugm3 = getDustDensity(outputV); //計算灰塵濃度
      double aqi = getAQI(ugm3); //計算aqi
      String gradeInfo = getGradeInfo(aqi); //計算級別
    
      //打印到串口
      Serial.println(String("outputV=") + outputV + "\tug/m3=" + ugm3 + "\tAQI=" + aqi + "\tgradeInfo=" + gradeInfo);
    
      //間隔1秒執行下次檢測
      delay(1000);
    }


    輸出結果


    outputV=1.05	ug/m3=29.96	AQI=42.80	gradeInfo=Perfect
    outputV=1.36	ug/m3=92.46	AQI=121.83	gradeInfo=Mild polluted
    outputV=1.29	ug/m3=77.81	AQI=103.52	gradeInfo=Mild polluted
    outputV=1.16	ug/m3=51.45	AQI=70.56	gradeInfo=Good
    outputV=1.12	ug/m3=43.63	AQI=60.79	gradeInfo=Good
    outputV=1.26	ug/m3=72.93	AQI=97.41	gradeInfo=Good


    本文大多摘自http://blog.fantasymaker.cn/2016/01/10/pm25-aqi-detector-based-on-nodemcu/

    更多信息請自行查看


    關于聯網請參考網站文檔,這里不在重復。


    關于數據上傳這里給大家分享一個函數格式供大家參考


    void update1(int did, int inputid1, int inputid2,int inputid3,float value1 ,float value2,float value3) // 定義一次傳遞3個參數的函數

    {

    String str1="{\"M\":\"update\",\"ID\":\"";

    str1+=did;

    str1+="\",\"V\":{\"";

    str1+=inputid1;

    str1+="\":\"";

    str1+=value1;

    str1+="\",\"" ;

    str1+=inputid2;

    str1+="\":\"";

    str1+=value2;

    str1+="\",\"" ;

    str1+=inputid3;

    str1+="\":\"";

    str1+=value3;

    str1+="\"" ;

    str1+="}}\n";

     client.print(str1);  

    //下面是串口打印 傳遞到服務器的信息,以便調試

      Serial.print("update:");   

      Serial.print(inputid1);   

      Serial.print("->");   

      Serial.println(value1);   

       Serial.print("update:");   

      Serial.print(inputid2);   

      Serial.print("->");   

      Serial.println(value2); 

       Serial.print("update:");   

      Serial.print(inputid3);   

      Serial.print("->");   

      Serial.println(value3); 

    }


    此函數出自網友 瘋狂的小車 借鑒網友 在路上 的勞動成果并發表的 ”用ESP8266傳遞BMP180的溫度的氣壓到貝殼“一文


    *所有圖片和程序均來源于互連網,本人只是重新整理發布。如有侵權請及時聯系網站管理員或作者。謝謝







    評論:共5條

    z494627 評論于:2016-06-01 16:17:26
    不錯!謝謝分享!
    楚風狂 回復于:2016-06-01 16:34:49
    回復 @z494627:謝謝!還要多向你學習
    貝殼物聯 回復于:2016-06-02 18:45:21
    回復 @楚風狂:不錯的教程。
    activemee 評論于:2016-12-29 23:47:42
    我最近在實驗這個傳感器, 按照你的接線,AQI基本都是0,后來我把C1的位置挪到R1后面, 這樣能測出數值,但是感覺還是很不準。
    出門看天下 評論于:2017-03-21 18:34:36
    感謝分享
    返回頂部
    <noscript id="mmkmi"><source id="mmkmi"></source></noscript>
  • <noscript id="mmkmi"><kbd id="mmkmi"></kbd></noscript>
  • <table id="mmkmi"><source id="mmkmi"></source></table>
  • 三上悠亚在线观看