使用夏普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连接好后,可以连续打印出传感器的输出电压值。输出电压大小和灰尘含量的曲线入下图:

[Read More]

Arduino初试

今天拿到一块Arduino UNO R3板,迫不及待就开始试用了。相比Raspberry Pi是一个全能的电脑,Arduino则是个硬件开源的单片机,因为开源,资料和配件网上就很很多了,也就容易让初学者上手了。

Arduino特点:

  1. 开源,硬件标准化,配套传感器等模块很多;
  2. 结构简单
  3. 实时系统,稳定,启动只要0.5秒

Arduino IDE

下载Arduino IDE

上电测试

用USB线接在电脑USB口,然后在GND和PIN 13上插一个二极管,注意二极管正极插在PIN 13上, 如下图:

(注:还应该串联一个300欧姆的限流电阻才保险!)

上传代码

在Arduino IDE编辑好下面的代码,然后点Upload后就会运行了,会看到LED一闪一闪。

/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.
 
  This example code is in the public domain.
 */
 
// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int led = 13;

// the setup routine runs once when you press reset:
void setup() {                
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);     
}

// the loop routine runs over and over again forever:
void loop() {
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);               // wait for a second
}

参考链接

  1. http://arduino.cc/en/Tutorial/Blink