PM2.5檢測/Arduino UNO R3/PMS5003T/OLED 0.96"

PM2.5檢測/Arduino UNO R3/PMS5003T/OLED 0.96"


這款PM2.5感測器,又多了溫濕度的功能,所以一併把感測資料顯示出來

感測器接腳如下:



SDA 接 A4     SCL 接 A5

這種 OLED,假設線接錯,開機時是完全沒畫面,千萬不要以為壞了,壞了,壞了
請再次檢查線路。



有用擴充板,因為手指較粗,眼睛又老花眼,接個擴充板才不會插錯洞。

不用也可以,請自行對照正確的接腳接上。



完成後如下圖:



還缺少一樣東西,不然顯示不出來。


為了簡化程式,

所以把除錯的程式碼去掉了~


#define OLED_EN
#if defined (OLED_EN)
#include "U8glib.h"
U8GLIB_SH1106_128X64 u8g(U8G_I2C_OPT_NO_ACK);
#endif
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3);
#define PMS_SET 4   
#if defined (OLED_EN)
 long pmat10=0;
 long pmat25=0;
 long pmat100=0;
 long pmt=0;
 long pmh=0;
 char buf[50];
 int sensorVal;
#endif
void draw() {
   u8g.setFont(u8g_font_8x13);
   u8g.drawStr(0, 10, "T=");
   u8g.setPrintPos(17, 10);
   u8g.print(int(pmt));
   u8g.drawStr(33, 10, "(C)");
   u8g.drawStr(60 ,10, "H=");
   u8g.setPrintPos(78, 10);
   u8g.print(int(pmh));
   u8g.drawStr(103, 10, "(%)");
   u8g.drawStr(0,25, "PM2.5=");
   u8g.setFont(u8g_font_fub25);
   u8g.setPrintPos(50, 40);
   u8g.print(int(pmat25));
   u8g.setFont(u8g_font_profont12);
   u8g.drawStr(0, 64, "PM1=");
   u8g.setPrintPos(25, 64);
   u8g.print(int(pmat10));    
   u8g.drawStr(60, 64, "PM10=");
   u8g.setPrintPos(92, 64);
   u8g.print(int(pmat100));
   u8g.setFont(u8g_font_5x8);
   u8g.drawStr(85, 52, "(ug/m3)");
}
void ecoMode(){
   sensorVal = digitalRead(PMS_SET);
 if (sensorVal == LOW){  
   delay(30000);
   digitalWrite(PMS_SET, HIGH);
    delay(30000);  
     }
 else if (sensorVal == HIGH){
    digitalWrite(PMS_SET, HIGH);
 }    
}   
void retrievepm25(){
 int count = 0;
 unsigned char c;
 unsigned char high;
 while (mySerial.available()) {
   c = mySerial.read();
   if((count==0 && c!=0x42) || (count==1 && c!=0x4d)){
     break;
   }
   if(count > 27){
     break;
   }
    else if(count == 10 || count == 12 || count == 14 || count == 24 || count == 26) {
     high = c;
   }
   else if(count == 11){
     pmat10 = 256*high + c;
   }
   else if(count == 13){
     pmat25 = 256*high + c;
   }
   else if(count == 15){
     pmat100 = 256*high + c;
   }
    else if(count == 25){        
     pmt = (256*high + c)/10;
   }
   else if(count == 27){            
     pmh = (256*high + c)/10;
   }   
   count++;
 }
 while(mySerial.available()) mySerial.read();
}
void decidePM25(){
 if(pmat25 >= 40){   
    u8g.firstPage();
    do {
     draw();
     u8g.setFont(u8g_font_5x8);
     u8g.drawStr(0, 52, "(Normal Mode)");     
     } while( u8g.nextPage() );
   digitalWrite(PMS_SET, HIGH);
   sensorVal = digitalRead(PMS_SET);
   delay(2000);
 }
 else {
    u8g.firstPage();
    do {
     draw();
     u8g.setFont(u8g_font_5x8);
     u8g.drawStr(0, 52, "(Economic Mode)");     
     } while( u8g.nextPage() );
   delay(500);
   digitalWrite(PMS_SET, LOW);
   sensorVal = digitalRead(PMS_SET);
 }   
   ecoMode();  
     }
void setup() {
 mySerial.begin(9600);
 pinMode(PMS_SET, OUTPUT);
 digitalWrite(PMS_SET, HIGH);
 for (int i = 0; i < 15; i++) {
   retrievepm25();
   u8g.firstPage();  
   do {
     draw();
     u8g.setFont(u8g_font_5x8);
     u8g.drawStr(0, 52, "(Normal)");
     } while( u8g.nextPage() );
 }
   delay(2000);
}
void loop() {
 retrievepm25();
 u8g.firstPage();  
   do {
     draw();
   } while( u8g.nextPage() );
  decidePM25();
}






留言

  1. 編譯的時候出現
    No such file or directory compilation terminated. exit status 1 U8glib.h: No such file or directory This report would have more information with "Show verbose output during compilation" option enabled in File -> Preferences.

    在這行#include "U8glib.h"
    請問怎麼解決, 謝謝

    回覆刪除
  2. 您好,上面的問題解決了,螢幕也成功顯示,但所有數值都是0,想請問是哪裡出錯了

    回覆刪除

張貼留言

這個網誌中的熱門文章

電腦概論 -- 程式流程控制

製做隨身PM2.5空氣感測器 - 使用Arduino uno R3/OLED 0.96"顯示器/PMS5003T感測器

0.96" OLED 上顯示數字跟文字