arduino/Mixly使用ZH03BPM2.5传感器

一、器材

ZH03B

 资料:http://资料下载:https://pan.baidu.com/s/1R5SN2tdo6uQ4-5TypvfVdg 提取码:v6b4

https://wiki.dfrobot.com.cn/_SKU_SEN0177_PM2.5%E6%BF%80%E5%85%89%E7%B2%89%E5%B0%98%E4%BC%A0%E6%84%9F%E5%99%A8

 

 

 

 需要注意的是上电之后需要等待10S之后才有数据

传来的数据11,12字节为PM1.0浓度,13,14字节是PM2.5浓度,15,16字节是PM10浓度

实测5,6字节也是PM1.0浓度,7,8字节也是PM2.5浓度,9,10字节也是PM10浓度

arduino uno

 

 二、接线

ZH03Barduino uno 
VCC5V
GNDGND
TXD11
RXD10

三、程序

#include <Arduino.h>
#include <SoftwareSerial.h>
#define LENG 31   //0x42 + 31 bytes equal to 32 bytes
unsigned char buf[LENG];

int PM01Value=0;          //define PM1.0 value of the air detector module
int PM2_5Value=0;         //define PM2.5 value of the air detector module
int PM10Value=0;         //define PM10 value of the air detector module

SoftwareSerial PMSerial(10, 11); // RX, TX

void setup()
{
  PMSerial.begin(9600);   //使用软串口,10口等效为RX,11口等效为TX
  PMSerial.setTimeout(1500);    //设置超时时间为1500毫秒(大于传感器传送数据周期1秒)
  Serial.begin(9600);
}

void loop()
{
  if(PMSerial.find(0x42)){    //检测到0x42时,开始读取
    PMSerial.readBytes(buf,LENG);
    //值我在这里给*0.0625,也可以不乘
    PM01Value=transmitPM01(buf)*0.0625; //count PM1.0 value of the air detector module
    PM2_5Value=transmitPM2_5(buf)*0.0625;//count PM2.5 value of the air detector module
    PM10Value=transmitPM10(buf)*0.0625; //count PM10 value of the air detector module 
  }

  static unsigned long OledTimer=millis();  //每隔1S打印1次PM2.5,1.0,10的值
    if (millis() - OledTimer >=1000) 
    {
      OledTimer=millis(); 
      
      Serial.print("PM1.0: ");  
      Serial.print(PM01Value);
      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();
    }
  
}
//读取PM1.0
int transmitPM01(unsigned char *thebuf)
{
  int PM01Val;
  PM01Val=((thebuf[3]<<8) + thebuf[4]); //count PM1.0 value of the air detector module
  return PM01Val;
}

//读取PM2.5
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;
  }

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

四、效果

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值