PM检测

2016-12-212万
AI 快速预览详细 收起
本项目使用Arduino Mega 2560和LCD12864屏幕,实现PM2.5数据检测并显示。设备清单包括Arduino Mega 2560、Mega sensor shield v2.4、LCD12864 Shield V1.0、Digital Plranha Green Led Module V2和PM2.5激光粉尘传感器。通过将PM2.5传感器的TX/RX与Arduino的RX/TX交叉连接,确保数据传输正确。当PM2.5大于40ug/m3时,LED灯珠报警。项目代码详细说明了如何读取和显示PM2.5数据。
最近都是大神各种发帖,搞得我这种小白都不好意思了,发个low点的,也希望大神指点指点。:lol:lol:lol


设计目标:检测PM2.5,并将数据结果打印到LCD12864屏幕和PC串口上。同时当PM2.5大于40ug/m3时开始闪烁LED灯珠报警。

设备清单:

接线说明:
PM2.5传感器,Arduino的TX/RX接传感器的RX/TX(注意TX和RX要交叉接线,TX–RX,RX–TX),LED接到数字口20上。

完整代码
[mw_shl_code=c,true]#include <U8glib.h>
#include <Arduino.h>

#define LENG 31   //0x42 + 31 bytes equal to 32 bytes
unsigned char buf[LENG];

//创建lcd对象
U8GLIB_NHD_C12864 u8g(13, 11, 10, 9, 8);

//声明LED灯珠使用的是数字口20
int led = 20;

//初始化变量,用于存储PM1.0, PM2.0, PM10的读书
int PM0_1Value=0;
int PM2_5Value=0;
int PM10Value=0;

//检查送串口获取到的读书
char checkValue(unsigned char *thebuf, char leng)
{  
char receiveflag=0;
int receiveSum=0;

for(int i=0; i<(leng-2); i++){
  receiveSum=receiveSum+thebuf;
}
  receiveSum=receiveSum + 0x42;

if(receiveSum == ((thebuf[leng-2]<<8)+thebuf[leng-1]))  //check the serial data
{
  receiveSum = 0;
  receiveflag = 1;
}
  return receiveflag;
}


int transmitPM0_1(unsigned char *thebuf)
{
  int PM0_1Val;
  PM0_1Val=((thebuf[3]<<8) + thebuf[4]); //count PM1.0 value of the air detector module
  return PM0_1Val;
}

int transmitPM2_5(unsigned char *thebuf)
{
  int PM2_5Val;
  PM2_5Val=((thebuf[5]<<8) + thebuf[6]);//count PM2.5 value of the air detector module
  return PM2_5Val;
}

int transmitPM10(unsigned char *thebuf)
{
  int PM10Val;
  PM10Val=((thebuf[7]<<8) + thebuf[8]); //count PM10 value of the air detector module  
  return PM10Val;
}

void drawTitle(){
  u8g.setFont(u8g_font_unifont);
  u8g.drawStr(0, 18, "PM1.0: ");
  u8g.drawStr(0, 36, "PM2.5: ");
  u8g.drawStr(0, 54, "PM1 0: ");
  u8g.drawStr(80, 18, "ug/m3");
  u8g.drawStr(80, 36, "ug/m3");
  u8g.drawStr(80, 54, "ug/m3");
}

void lightAlarm(){
  digitalWrite(led, HIGH);   //Turn on led
  delay(100);
  digitalWrite(led, LOW);    //Turn off led
  delay(100);
}

void drawData(){
  u8g.setFont(u8g_font_unifont);
  u8g.setPrintPos(45,18);
  u8g.print(PM0_1Value);
  u8g.setPrintPos(45,36);
  u8g.print(PM2_5Value);
  u8g.setPrintPos(45,54);
  u8g.print(PM10Value);
}

void drawScreen(){
  drawTitle();
  drawData();
}

void setup() {
// put your setup code here, to run once:
  u8g.setRot180();// rotate screen
  Serial.begin(9600);
  pinMode(led, OUTPUT);     //Set Pin20 as output
}

void loop() {
// put your main code here, to run repeatedly:
  if(Serial.find(0x42)){    //检测到0x42时,开始读取
    Serial.readBytes(buf,LENG);

    if(buf[0] == 0x4d){
      if(checkValue(buf,LENG)){
        PM0_1Value=transmitPM0_1(buf); //count PM1.0 value of the air detector module
        PM2_5Value=transmitPM2_5(buf);//count PM2.5 value of the air detector module
        PM10Value=transmitPM10(buf); //count PM10 value of the air detector module
      }           
    }
  }

  static unsigned long OledTimer=millis();  
  if (millis() - OledTimer >=1000)
  {
    OledTimer=millis();

    Serial.print("PM1.0: ");  
    Serial.print(PM0_1Value);
    Serial.println("  ug/m3");            

    Serial.print("PM2.5: ");  
    Serial.print(PM2_5Value);
    Serial.println("  ug/m3");     

    Serial.print("PM1 0: ");  
    Serial.print(PM10Value);
    Serial.println("  ug/m3");   
    Serial.println();
  }

  u8g.firstPage();
  drawScreen();
  do {
    drawScreen();
  } while( u8g.nextPage() );
  delay(500);

  if(PM2_5Value>40){
    lightAlarm();
  }
  delay(500);
}[/mw_shl_code]

PM检测图1
PM检测图2
PM检测图3



创作许可协议

本项目采用 None(不开放任何权利,保留所有权利) 进行许可。

评论(5)
20060606
20060606
好高端!!!!!
gray6666
gray6666
实用,好课题
luna
luna
代码写的好工整~
seacowtech
seacowtech
作者
回复luna
嘿嘿,说的我都不好意思了
hnyzcj
hnyzcj
顶起来
dsweiliang
dsweiliang
感谢分享,学习了
seacowtech
seacowtech
作者
回复dsweiliang
一起学咯,这里好多好多可以学的。
- 没有更多了 -