最近入手空气质量模块 ZP07-MP503,用Arduino采样数据进行测试
先上图看看 ZP07-MP503 产品
ZP07-MP503 一共4个管脚,功能如下
5V 电源输入5V
GND 电源输入0V
A 信号输出,以脉冲宽度形式输出
B 厂家备用
信号A 以高低电平脉冲宽度形式输出,对应污染等级参照下表:
ZP07-MP503 模块参数手册 链接:
测试结果
本实例中,我将 ZP07-MP503 的 A号引脚 接在了 Arduino 的 8号引脚上
Arduino 源码如下,测试正常!
1 // 2 // Author: Aiyauto 3 // VERSION: 0.0.1 4 // Date:2017/08/16 5 // PURPOSE: ZP07_MP503 空气质量模块检测 示例 for Arduino 6 // 7 // DATASHEET: 8 // 9 // URL: 10 // 11 // 12 13 14 int airQuality; //空气质量等级 0-10 15 16 void setup() { 17 Serial.begin(9600); 18 } 19 20 void loop() { 21 airQuality = ZP07_MP503(8); 22 Serial.print(" 当前空气质量等级:"); 23 Serial.println(airQuality); 24 } 25 26 27 int ZP07_MP503(int pinA) { 28 29 /* 初始化 */ 30 pinMode(pinA, INPUT); 31 unsigned long millisTimes = millis(); 32 unsigned long startMillisTimes = millisTimes; 33 unsigned long stopMillisTimes; 34 signed long deltaMillisTimes = millisTimes - startMillisTimes; 35 bool turnState = false; 36 bool pinAstate = digitalRead(pinA);; 37 bool pinAstateLast = pinAstate; 38 int result; 39 40 /* 测试读取状态 */ 41 while (true) { 42 pinAstate = digitalRead(pinA); //读取管脚状态 43 44 if (pinAstate != pinAstateLast) { 45 if (turnState == true) { 46 stopMillisTimes = millis(); 47 if (pinAstate == false) { 48 deltaMillisTimes = stopMillisTimes - startMillisTimes; 49 } 50 else { 51 deltaMillisTimes = 98 - stopMillisTimes + startMillisTimes; 52 } 53 result = (deltaMillisTimes + 5) / 10; //运算结果四舍五入 54 break; //跳出循环 55 } 56 if (turnState == false) { 57 startMillisTimes = millis(); //刷新当前时间 58 turnState = true; //更新标记 59 } 60 pinAstateLast = pinAstate; 61 } 62 63 millisTimes = millis(); 64 deltaMillisTimes = millisTimes - startMillisTimes; 65 if ( deltaMillisTimes > 100 ) { //结果判定,超时跳出循环 66 if (pinAstate == true) { //空气质量等级10 67 result = 1; 68 } 69 if (pinAstate == false) { //空气质量等级0 70 result = 0; 71 } 72 break; 73 } 74 } 75 76 // Serial.print(" pinAstate:"); 77 // Serial.print(pinAstate); 78 // Serial.print(" , 空气质量等级:"); 79 // Serial.print(result); 80 // Serial.print(", deltaMillisTimes:"); 81 // Serial.println(deltaMillisTimes); 82 83 return result; //返回空气质量等级结果 84 85 }