使用夏普GP2Y1010AU0F灰尘传感器检测空气质量

夏普GP2Y1010AU0F灰尘传感器价格较便宜,能检测出室内空气中的灰尘和烟尘含量。另外还有韩国SYHITECH生产的DSM501A粉尘传感器也有类似功能。

检测原理

其原理如下图,传感器中心有个洞可以让空气自由流过,定向发射LED光,通过检测经过空气中灰尘折射过后的光线来判断灰尘的含量。

电路图

因为数据是通过pin 5的电压模拟信号输出的,而树莓派的引脚不支持模拟信号直接读取(需要增加数模转换芯片),所以先用Arduino来实验。

Arduino 代码

根据电路图, 把Arduino和传感器连接起来:

  1. Sharp pin 1 (V-LED) => 5V 串联1个150欧姆的电阻(最好在电阻一侧和GND之间再串联一个220uf的电容
  2. Sharp pin 2 (LED-GND) => GND
  3. Sharp pin 3 (LED) => Arduino PIN 2 (开关LED)
  4. Sharp pin 4 (S-GND) => GND
  5. Sharp pin 5 (Vo) => Arduino A0 pin (空气质量数据通过电压模拟信号输出)
  6. Sharp pin 6 (Vcc) => 5V
/*
 Interface to Sharp GP2Y1010AU0F Particle Sensor
 Program by Christopher Nafis 
 Written April 2012
 
 http://www.sparkfun.com/datasheets/Sensors/gp2y1010au_e.pdf
 http://sensorapp.net/?p=479
 
 Sharp pin 1 (V-LED)   => 5V (connected to 150ohm resister)
 Sharp pin 2 (LED-GND) => Arduino GND pin
 Sharp pin 3 (LED)     => Arduino pin 2
 Sharp pin 4 (S-GND)   => Arduino GND pin
 Sharp pin 5 (Vo)      => Arduino A0 pin
 Sharp pin 6 (Vcc)     => 5V
 */
#include <SPI.h>
#include <stdlib.h>

int dustPin=0;
int ledPower=2;
int delayTime=280;
int delayTime2=40;
float offTime=9680;

int dustVal=0;
int i=0;
float ppm=0;
char s[32];
float voltage = 0;
float dustdensity = 0;
float ppmpercf = 0;

void setup(){
  Serial.begin(9600);
  pinMode(ledPower,OUTPUT);

  // give the ethernet module time to boot up:
  delay(1000);

  i=0;
  ppm =0;
}

void loop(){
  i=i+1;
  digitalWrite(ledPower,LOW); // power on the LED
  delayMicroseconds(delayTime);
  dustVal=analogRead(dustPin); // read the dust value
  ppm = ppm+dustVal;
  delayMicroseconds(delayTime2);
  digitalWrite(ledPower,HIGH); // turn the LED off
  delayMicroseconds(offTime);

  voltage = ppm/i*0.0049;
  dustdensity = 0.17*voltage-0.1;
  ppmpercf = (voltage-0.0256)*120000;
  if (ppmpercf < 0)
    ppmpercf = 0;
  if (dustdensity < 0 )
    dustdensity = 0;
  if (dustdensity > 0.5)
    dustdensity = 0.5;
  String dataString = "";
  dataString += dtostrf(voltage, 9, 4, s);
  dataString += ",";
  dataString += dtostrf(dustdensity, 5, 2, s);
  dataString += ",";
  dataString += dtostrf(ppmpercf, 8, 0, s);
  i=0;
  ppm=0;
  Serial.println(dataString);
  delay(1000);
}

把传感器和Ardiuno连接好后,可以连续打印出传感器的输出电压值。输出电压大小和灰尘含量的曲线入下图:

通过电压的波形还可以判断是烟还是尘呢。。。

参考链接

  1. Sharp GP2Y1010AU0F Data Sheet
  2. Sharp GP2Y1010AU0F Application Note
  3. http://www.howmuchsnow.com/arduino/airquality/

See also