功能介绍
本项目主要是实现基本的空气质量检测功能:
- PM2.5指数
- 空气质量指数
- 空气湿度
- 空气温度
准备材料
- Arduino UNO *1
- LCD 5110 *1
- DHT11温湿度传感器 *1
- MQ135空气质量检测模块 *1
- PM2.5粉尘检测传感器(GP2Y1010AU0F) *1
- 150欧电阻 *1
- 220uF电解电容 *1
- 面包板 *1
- 跳线若干
就不给出购买链接了,这些材料在某宝上随便一搜就有~
模块接线
-
LCD 5110与Arduino
LCD 5110 | Arduino | |
---|---|---|
RST | -> | 11 |
CE | -> | 12 |
DC | -> | 10 |
DIN | -> | 9 |
CLK | -> | 8 |
VCC | -> | 5V |
BL | -> | GND |
GND | -> | GND |
-
PM2.5粉尘检测传感器与Arduino
GP2Y1010AU0F | Arduino | |
---|---|---|
1 | 150欧电阻 | 5V |
1 | 220uF电解电容 | GND |
2 | -> | GND |
3 | -> | 2 |
4 | -> | GND |
5 | -> | A0 |
6 | -> | 5V |
-
MQ135空气质量检测模块与Arduino
MQ135 | Arduino | |
---|---|---|
VCC | -> | 5V |
AOUT | -> | A1 |
GND | -> | GND |
-
DHT11温湿度传感器与Arduino
DHT11 | Arduino | |
---|---|---|
GND | -> | GND |
VCC | -> | 5V |
OUT | -> | 5 |
第三方库
#include <dht.h>
#include <LCD5110_Basic.h>
#include "MQ135.h"
下载地址:
-
dht.h
->https://arduino-info.wikispaces.com/file/view/DHT-lib.zip/545470280/DHT-lib.zip -
LCD5110_Basic.h
->http://www.rinkydinkelectronics.com/download.php?f=LCD5110_Basic.zip -
MQ135.h
-> https://codeload.github.com/GeorgK/MQ135/zip/master
如果不知道如何手动加载库文件,或者不知道怎么使用库,请参考我之前写的基础教程:
源程序
#include <dht.h>
#include <LCD5110_Basic.h>
#include "MQ135.h"
//空气质量
MQ135 gasSensor = MQ135(1);
//LCD5110
LCD5110 myGLCD(8,9,10,11,12);
extern uint8_t SmallFont[];
extern uint8_t MediumNumbers[];
extern uint8_t BigNumbers[];
//温湿度
dht DHT;
#define DHT11_PIN 5
//PM2.5
int dustPin=0;
float dustVal=0;
int ledPower=2;
int delayTime=280;
int delayTime2=40;
float offTime=9680;
float a=0;
String s0 = " ";
String s1 = " ";
String s2 = " ";
String s3 = " ";
void setup()
{
Serial.begin(115200);
pinMode(ledPower,OUTPUT);
pinMode(dustPin, INPUT);
myGLCD.InitLCD(); //Intializing LCD
myGLCD.setContrast(55);
}
void pm25(){
digitalWrite(ledPower,LOW);
delayMicroseconds(delayTime);
dustVal=analogRead(dustPin);
delayMicroseconds(delayTime2);
digitalWrite(ledPower,HIGH);
delayMicroseconds(offTime);
if (dustVal>36.455)
a = (float(dustVal/1024)-0.0356)*120000*0.035;
s0 = String("PM2.5: "+String(a));
myGLCD.setFont(SmallFont);
myGLCD.print(s0,0,8);
}
void ht(){
Serial.print("DHT11, \t");
int chk = DHT.read11(DHT11_PIN);
switch (chk)
{
case DHTLIB_OK:
Serial.print("OK,\t");
break;
case DHTLIB_ERROR_CHECKSUM:
Serial.print("Checksum error,\t");
break;
case DHTLIB_ERROR_TIMEOUT:
Serial.print("Time out error,\t");
break;
case DHTLIB_ERROR_CONNECT:
Serial.print("Connect error,\t");
break;
case DHTLIB_ERROR_ACK_L:
Serial.print("Ack Low error,\t");
break;
case DHTLIB_ERROR_ACK_H:
Serial.print("Ack High error,\t");
break;
default:
Serial.print("Unknown error,\t");
break;
}
// DISPLAY DATA
Serial.print(DHT.humidity, 1);
Serial.print(",\t");
Serial.println(DHT.temperature, 1);
myGLCD.setFont(SmallFont);
s1 = String("H : " + String(int(DHT.humidity)) + " %");
myGLCD.print(s1,0,24);
myGLCD.setFont(SmallFont);
s2 = String("T : " + String(int(DHT.temperature))+" C");
myGLCD.print(s2,0,32);
}
void ppm(){
float ppm = gasSensor.getPPM();
s3 = String("Air: " + String(ppm)+"ppm");
myGLCD.print(s3,0,16);
}
void loop(){
myGLCD.setFont(SmallFont);
myGLCD.print("Hello World",CENTER,0);
pm25();
ht();
ppm();
delay(2000);
myGLCD.clrScr();
}
源程序下载地址
:https://github.com/wwwxmu/ArduinoAirQuality
附:测试得到的数据和空气质量对照:
3000 + = 很差
1050-3000 = 差
300-1050 = 一般
150-300 = 好
75-150 = 很好
0-75 = 非常好