Arduino系列教程八:PM2.5灰尘传感器
来自丢石头百科
本教程实现功能:通过传感器获取当前空气PM2.5的值,并通过串口发送给上位机显示。
PM2.5传感器概述:
可以检测直径大于0.8um的灰尘颗粒浓度,作为一名创客,想了解空气质量?想DIY一个PM2.5检测仪?想设计一个空气净化器?,那么这款传感器就是你的必备神器
硬件准备:
硬件连接:
1. 将IO Expansion 扩展板接入UNO PLUS,用跳线帽将两个板子的电压都配置为 5V
2. Dust Sensor 与IO Expansion的连接
VCC -- VCC
GND -- GND
AOUT -- A5
ILED -- D2
3. 连接效果如下图
程序:
#define COV_RATIO 0.2 //ug/mmm / mv
#define NO_DUST_VOLTAGE 400 //mv
#define SYS_VOLTAGE 5000 //ADC参考电压
/*
I/O define
*/
const int iled = 2; //drive the led of sensor
const int vout = 5; //analog input
/*
variable
*/
float density, voltage;
int adcvalue;
/*
private function
*/
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[i] = m;
sum += _buff[i];
}
return m;
}
else
{
sum -= _buff[0];
for(i = 0; i < (_buff_max - 1); i++)
{
_buff[i] = _buff[i + 1];
}
_buff[9] = m;
sum += _buff[9];
i = sum / 10.0;
return i;
}
}
void setup(void)
{
pinMode(iled, OUTPUT);
digitalWrite(iled, LOW); //iled default closed
Serial.begin(9600); //send and receive at 9600 baud
Serial.print("*********************************** WaveShare ***********************************\n");
}
void loop(void)
{
/*
get adcvalue
*/
digitalWrite(iled, HIGH);
delayMicroseconds(280);
adcvalue = analogRead(vout);
digitalWrite(iled, LOW);
adcvalue = Filter(adcvalue);
/*
covert voltage (mv)
*/
voltage = (SYS_VOLTAGE / 1024.0) * adcvalue * 11;
/*
voltage to density
*/
if(voltage >= NO_DUST_VOLTAGE)
{
voltage -= NO_DUST_VOLTAGE;
density = voltage * COV_RATIO;
}
else
density = 0;
/*
display the result
*/
Serial.print("The current dust concentration is: ");
Serial.print(density);
Serial.print(" ug/m3\n");
delay(1000);
}
实现输出:
程序代码下载:
<a class="attach" href="portal.php?mod=attachment&id=33" target="_blank">DustSensor.zip