PM2.5监测仪的核心传感器就是 Dust Sensor,它能够检测大于0.8um的灰尘颗粒浓度,使用也比较方便,只需要四个引脚。
准备材料:
- UNO PLUS
- I/O Expansion Shield
- 1.3inch OLED
- 温湿度传感器
- PM2.5传感器
硬件连接:
1. UNO PLUS的电源用跳线帽配置为5V,I/O Expansion Shield的电压同样用跳线帽配置为5V
2. 连接PM2.5传感器
VCC -- VCC GND -- GND AOUT -- A5 ILED -- D3
3. 连接OLED
VCC -- VCC GND -- GND DIN -- D11 CLK -- D13 CS -- D10 D/C -- D9 RES -- D8
4. 连接温湿度传感器
VCC -- VCC GND -- GND DOUT -- D2
示例程序:- #include "U8glib.h"
- #include "dht11.h"
- #define COV_RAtiO 0.2 //ug/mmm / mv
- #define NO_DUST_VOLTAGE 450 //mv
- #define SYS_VOLTAGE 5000
- const int iled = 3; //drive the led of sensor
- const int vout = 5; //analog input
- const int DHT11PIN = 2;
- U8GLIB_SH1106_128X64 u8g(13, 11, 10, 9, 8); // SW SPI Com: SCK = 13, MOSI = 11, CS = 10, A0 = 9,RST = 8
- dht11 DHT11;
- int chk;
- String di***uff;
- float density, voltage;
- int adcvalue;
- int Filter(int m)
- {
- static int flag_first = 0, _buff[10], sum;
- const int _buff_max = 10;
- int i;
- if(flag_first == 0)
- {
- flag_first = 1;
- for(i = 0, sum = 0; i < _buff_max; i++)
- {
- _buff = m;
- sum += _buff;
- }
- return m;
- }
- else
- {
- sum -= _buff[0];
- for(i = 0; i < (_buff_max - 1); i++)
- {
- _buff = _buff[i + 1];
- }
- _buff[9] = m;
- sum += _buff[9];
- i = sum / 10.0;
- return i;
- }
- }
- void setup(void)
- {
- u8g.setFont(u8g_font_6x13);
- u8g.setFontPosTop();
- pinMode(iled, OUTPUT);
- digitalWrite(iled, LOW); //iled default closed
- }
- void loop(void)
- {
- /*
- get humidity and temperature
- */
- chk = DHT11.read(DHT11PIN);
- /*
- get density
- */
- digitalWrite(iled, HIGH);
- delayMicroseconds(280);
- adcvalue = analogRead(vout);
- digitalWrite(iled, LOW);
- adcvalue = Filter(adcvalue);
- voltage = (SYS_VOLTAGE / 1024.0) * adcvalue * 11;
- if(voltage >= NO_DUST_VOLTAGE)
- {
- voltage -= NO_DUST_VOLTAGE;
- density = voltage * COV_RATIO;
- }
- else
- density = 0;
- /*
- display result
- */
- u8g.firstPage();
- do
- {
- di***uff = " H = ";
- di***uff.concat((float)DHT11.humidity);
- di***uff += "%RH";
- u8g.drawStr( 0, 6, di***uff.c_str());
- di***uff = " T = ";
- di***uff.concat((float)DHT11.temperature);
- di***uff += "C";
- u8g.drawStr( 0, 26, di***uff.c_str());
- di***uff = " PM2.5 = ";
- di***uff.concat(density);
- di***uff += "ug/m3";
- u8g.drawStr(0, 46, di***uff.c_str());
- } while( u8g.nextPage() );
- delay(500);
- }
[color=rgb(88, 170, 165) !important]复制代码
|