灰塵感應模組

文章推薦指數: 80 %
投票人數:10人

Faya-Nugget 範例程式(Dust_Sensor_1.ino). // 單元: 模組介紹:faya灰塵感應模組 ... Arduino夏普GP2Y1051AU0F的串口讀取_源代碼_Weesky維天監製. Skiptocontent Allgists BacktoGitHub Signin Signup Sign in Sign up {{message}} Instantlysharecode,notes,andsnippets. hsinghao/Dust_Sensor_1.ino LastactiveAug25,2021 Star 1 Fork 0 Star Code Revisions 3 Stars 1 Embed Whatwouldyouliketodo? Embed Embedthisgistinyourwebsite. Share Copysharablelinkforthisgist. Clonevia HTTPS ClonewithGitorcheckoutwithSVNusingtherepository’swebaddress. LearnmoreaboutcloneURLs DownloadZIP 模組介紹:灰塵感應模組 Raw Dust_Sensor_1.ino ThisfilecontainsbidirectionalUnicodetextthatmaybeinterpretedorcompileddifferentlythanwhatappearsbelow.Toreview,openthefileinaneditorthatrevealshiddenUnicodecharacters. LearnmoreaboutbidirectionalUnicodecharacters Showhiddencharacters //2018/10/19 //Faya-Nugget範例程式(Dust_Sensor_1.ino) //單元:模組介紹:faya灰塵感應模組 //網址:https://fayalab.blogspot.com/2018/10/dust-Sensor.html //目標:(1)灰塵感應模組每0.5秒送出xxxxxxxxxxxxxxxxxxxxxxx //接線:Arduino==>faya模組 //D10==>TxD(灰塵感應模組) //======================原始檔案資訊====================== //Arduino夏普GP2Y1051AU0F的串口讀取_源代碼_Weesky維天監製 //作者ID:Weesky;... //2014年11月23日 //v1.0基礎版本,成功讀取數值,並且算出PM2.5濃度。

//v1.1改進了算法,這樣看起來數值高了些。

對於愛好者操作更容易了。

//請不要在採樣週期裡面添加延遲delay這樣的。

不然串口寄存器只有64Byte,很可能被塞滿。

//========================================================== #include//引入SoftwareSerial軟體模擬串列埠函式庫 intincomeByte[7];//儲存七個字節的資料 intdata;//暫存每個字節的資料 intz=0;//字節計數器數據傳輸共7字節(0~6) intsum;//校驗位=第1~第4字節的總和=Vout(H)+Vout(L)+Vref(H)+Vref(L) intA=390;//A=比例係數(280~500間) constintTX=11;//灰塵感測模組沒有RX腳,但需要設一個沒有用的腳位讓程式順利組譯 constintRX=10;//灰塵感測模組TxD接到Arduino第10腳 SoftwareSerialfayaSerial(RX,TX);//定義Arduino連接到藍芽模組的串列腳位 voidsetup() { Serial.begin(9600);//Arduino串列埠baudRate=9600 fayaSerial.begin(2400);//軟體模擬串列埠baudRate=2400 } voidloop(){ while(fayaSerial.available()>0) { data=fayaSerial.read(); if(data==170)//170=0xaa,起始字節 { z=0;//字節計數歸零 incomeByte[z]=data;//將data存到第0個字節(0Xaa) } else { z++; incomeByte[z]=data;//依序儲存第1字節到第6字節的資料 } if(z==6)//儲存完七個字節後 { sum=incomeByte[1]+incomeByte[2]+incomeByte[3]+incomeByte[4];//加總字節1~4 if(incomeByte[5]==sum&&incomeByte[6]==255)//比對加總後是否和字節5(校驗位)相同,並且字節6是否為結束位元0xff { Serial.print("DataOK!|");//完成6個字節的儲存 for(intk=0;k<7;k++)//印出七個字節 { Serial.print(incomeByte[k]);//起始位|Vout(H)|Vout(L)|Vref(H)|Vref(L)|校驗位|結束位 Serial.print("|");//0xaa|0x??|0x??|0x??|0x??|sum|0xff } Serial.print("Vo="); //感測器輸出電壓=((Vout(H)x256+Vout(L))/1024)x5 floatVout=(incomeByte[1]*256.0+incomeByte[2])*5/1024.0; Serial.print(Vout,3);//Vout取到小數第3位 Serial.print("v|"); floatUd=Vout*A;//粉塵濃度Ud=VoutxA Serial.print("PM2.5="); Serial.print(Ud,2);//Ud取到小數第2位 Serial.print("ug/m3"); Serial.println(); delay(500); } else//完成運算與運算後,將資料歸零 { Serial.flush();//確認傳送緩衝區資料傳送完畢 data='/0'; for(intm=0;m<7;m++){ incomeByte[m]=0;}//七個字節內的資料歸零 } z=0;//Z=6後,Z歸零 } } } Signupforfree tojointhisconversationonGitHub. Alreadyhaveanaccount? Signintocomment Youcan’tperformthatactionatthistime. Yousignedinwithanothertaborwindow.Reloadtorefreshyoursession. Yousignedoutinanothertaborwindow.Reloadtorefreshyoursession.



請為這篇文章評分?