本帖最后由 Ketose 于 2015-12-31 01:34 编辑
使用K22F读取夏普光学灰尘传感器(GP2Y1010AU0F)PM2.5的值
1、夏普光学灰尘传感器(GP2Y1010AU0F)介绍
Sharp's GP2Y1010AU0F 是一款光学空气质量传感器,设计用来感应空气中的尘埃粒子,其内部对角安放着红外线发光二极管和光电晶体管,使得其能够探测到空气中尘埃反射光,即使非常细小的如烟草烟雾颗粒也能够被检测到,通常在空气净化系统中应用。该装置中,一个红外发光二极管和光电晶体管,对角布置成允许其检测到在空气中的灰尘反射光。
2、规范
电源电压:5-7V
工作温度:-10-65摄氏度
消耗电流:20mA最大
最小粒子检出值:0.8微米
灵敏度:0.5V/(0.1mg/m3)
清洁空气中电压:0.9V 典型值
工作温度:-10~65℃
存储温度:-20~80℃
使用寿命:5年
尺寸大小:46mm×30mm×17.6mm
重量大小:15g
3、检测原理
其原理如下图,传感器中心有个洞可以让空气自由流过,定向发射LED光,通过检测经过空气中灰尘折射过后的光线来判断灰尘的含量。
4、项目原理图
我们使用开发板的PTB1作为传感器的控制开关,用来关闭和打开传感器的测试,把PTC1设置为ADC0来采集传感器的电压。
5、代码
根据LED驱动周期(脉冲周期:T (ms) ),LED驱动时间(脉冲:宽度Pw(ms) )输出电压会变动,规格书特性的规格值是脉冲周期T:10ms,脉冲宽度Pw:0.32ms,取样时间:0.28ms,根据此条件变动,规格书上规定的特性值(无尘时输出电压、检出感度)也随之变动。在微机编程上,不能以此条件设定的情况下,请在规格书的推荐范围内操作。
另外,根据电源电压,输出电压也会变动。
- /**
- ****************************************************************************************
- *
- * [url=home.php?mod=space&uid=288409]@file[/url] gp2y.h
- *
- * [url=home.php?mod=space&uid=247401]@brief[/url] GP2Y1010AU0F传感器操作模块头文件
- *
- * Copyright (C) sunsjw 2015
- *
- * $Rev: 1.0 $
- *
- ****************************************************************************************
- */
- #ifndef __GP2Y_H
- #define __GP2Y_H
- #include "MK22F51212.h"
- #include "fsl_port_hal.h"
- #include "fsl_gpio_hal.h"
- #include "fsl_adc16_driver.h"
- #include "fsl_adc16_hal.h"
- #include "fsl_debug_console.h"
- #include <stdint.h>
- #define GP2Y_ON GPIO_HAL_ClearPinOutput(PTB,1)
- #define GP2Y_OFF GPIO_HAL_SetPinOutput(PTB,1)
- void GP2Y_init(void);
- float GP2Y_Read();
- #endif
- /**
- ****************************************************************************************
- *
- * @file gp2y.c
- *
- * @brief GP2Y1010AU0F传感器操作模块文件
- *
- * Copyright (C) sunsjw 2015
- *
- * $Rev: 1.0 $
- *
- ****************************************************************************************
- */
- #include "gp2y.h"
- adc16_converter_config_t g_adcConfig;
- adc16_calibration_param_t g_adcCalibraitionParam;
- adc16_chn_config_t g_chnConfig;
- void GP2Y_init()
- {
- PORT_HAL_SetMuxMode(PORTC,1U,kPortPinDisabled);
- PORT_HAL_SetMuxMode(PORTB,1U,kPortMuxAsGpio);
-
- GPIO_HAL_SetPinDir(PTB,1,kGpioDigitalOutput);
-
- ADC16_DRV_StructInitUserConfigDefault(&g_adcConfig);
- ADC16_DRV_Init(0,&g_adcConfig);
-
- ADC16_DRV_GetAutoCalibrationParam(0,&g_adcCalibraitionParam);
- ADC16_DRV_SetCalibrationParam(0,&g_adcCalibraitionParam);
-
- g_adcConfig.continuousConvEnable = true;
- ADC16_DRV_Init(0,&g_adcConfig);
-
- g_chnConfig.chnIdx = kAdc16Chn15;
- g_chnConfig.diffConvEnable = false;
- g_chnConfig.convCompletedIntEnable = false;
-
- ADC16_DRV_ConfigConvChn(0,0U,&g_chnConfig);
- }
- float GP2Y_Read()
- {
- uint16_t adcData;
- float voltRead;
- // Get ADC input
- ADC16_DRV_WaitConvDone(0, 0U);
- adcData = ADC16_DRV_GetConvValueSigned(0, 0U);
- //PRINTF("ADC:%d\n",adcData);
- // Convert ADC value to a voltage based on 3.3V VREFH on board
- voltRead = (float)adcData * (3.3 / 4096.0);
- return voltRead;
- }
我们在主程序里添加如下代码:
- float aqi = 0;
- int i =0;
- while(1)
- {
- temp = humi = 0;
- if(i++ >9)
- {
- int x = round(pm * 100);
- PRINTF("PM2.5:%d\r\n",x);
- pm =0;
- i = 0;
- }
- GP2Y_ON;
- DelayUs(280);
- float AD_PM = GP2Y_Read();
- DelayUs(40);
- GP2Y_OFF;
- DelayUs(9680);
- aqi += (0.2*AD_PM-0.18); //电压-灰尘转换
- }
计算出AQI指数,如果计算PM2.5浓度:xx微克/立方米,刚参考下面的图换算出来。
好了,今天已经很晚了,休息一下先。明天再继续。。。。
|