一、器材
ZH03B
资料:http://资料下载:https://pan.baidu.com/s/1R5SN2tdo6uQ4-5TypvfVdg 提取码:v6b4
需要注意的是上电之后需要等待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
二、接线
ZH03B | arduino uno |
VCC | 5V |
GND | GND |
TX | D11 |
RX | D10 |
三、程序
#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;
}
四、效果