PM2.5检测

看了网上做的PM2.5检测,发现代码运行并不是很顺利,就开始自己写了一个PM2.5检测的代码,并且成功运行。

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

设备清单:

  1. Arduino Mega 2560 控制器 点此购买
  2. Mega sensor shield v2.4 点此购买
  3. LCD12864 Shield V1.0 点此购买
  4. Digital Plranha Green Led Module V2
  5. PM2.5激光粉尘传感器 点此购买

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

完整代码:

#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[i];
}
  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 
      }           
    } 
  }

  //PC端串口打印,速率9600
  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();
  }

  //LCD12864打印
  u8g.firstPage();
  drawScreen(); 
  do {
    drawScreen();
  } while( u8g.nextPage() );
  delay(500);

  //PM2.5大于40开始报警
  if(PM2_5Value>40){
    lightAlarm();
  }
  delay(500);
}

实拍1

实拍2

实拍3

代码已经成功运行。PC上的我就不截图了,后面有更精彩的,喜欢的可以关注我的博客喔。

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值