1.GP2Y1010AU0F粉尘传感器连接图
2. GP2Y1010AU0F工作原理
工作原理
传感器中心有个洞可以让空气自由流过,定向发射LED光,通过检测经过空气中灰尘折射过后的光线来判断灰尘的含量。
3.源代码
main.py
# coding=UTF-8
import RPi.GPIO as GPIO
from ADC import ADS1015
from ADC import ADS_POINTER_CONFIG
import time
import math
import smbus
# import serial
COV_RATIO = 0.2 # //ug/mmm / mv
NO_DUST_VOLTAGE = 400 # //mv
SYS_VOLTAGE = 5000
density = 0.0
voltage = 0.0
# int adcvalue=0
def SendVideo():
ads1015 = ADS1015()
state = ads1015._read_u16(ADS_POINTER_CONFIG) & 0x8000 # 气体传感器连接确立
if (state != 0x8000):
print("\nADS1015 Error\n")
# 收集气体数据
GPIO.setmode(GPIO.BOARD)
IN1 = 16
GPIO.setwarnings(False)
GPIO.setup(IN1, GPIO.OUT) # 初始化二极管灯
GPIO.output(IN1, GPIO.LOW) # 关闭二级管灯
# ser = serial.Serial("/dev/ttyAMA0",9600)
# def Filter(m):
while 1:
GPIO.output(IN1, GPIO.HIGH) # 启动二极管灯
# AIN2_DATA=ads1015.ADS1015_SINGLE_READ(2)
time.sleep(0.00028) # 等待0.28ms
AIN2_DATA = ads1015.ADS1015_SINGLE_READ(2)
# AIN0_DATA=((AIN0_DATA*2-64)/2000.00+0.02)*2
time.sleep(0.00004) # 持续采集0.04ms
GPIO.output(IN1, GPIO.LOW) # 关闭二级管灯
time.sleep(0.00986)
voltage = (5000 / 1024.0) * AIN2_DATA * 1.1 * 2 # 计算气体浓度
if (voltage >= NO_DUST_VOLTAGE):
voltage -= NO_DUST_VOLTAGE
density = voltage * COV_RATIO
else:
density = 0
FG = "The current dust concentration is:" + str(round(density, 2)) + " ug/m3"
# FG=voltage
print(FG)
time.sleep(0.100)
# F6=FG.encode('utf-8')
# sock.send(str.encode(str(len(F6)).ljust(16)));
# sock.send(F6)
if __name__ == '__main__':
SendVideo()
ADC.py
#!/usr/bin/python
# -*- coding:utf-8 -*-
import time
import smbus
#i2c address
ADS_I2C_ADDRESS = 0x48
#Pointer Register
ADS_POINTER_CONVERT = 0x00 # 指针_转换
ADS_POINTER_CONFIG = 0x01 # 指针_配置
ADS_POINTER_LOWTHRESH = 0x02 # 低阈值
ADS_POINTER_HIGHTHRESH = 0x03 # 高阈值
#Config Register
ADS_CONFIG_OS_BUSY = 0x0000 #Device is currently performing a conversion 设备当前正在执行转换
ADS_CONFIG_OS_NOBUSY = 0x8000 #Device is not currently performing a conversion 设备当前没有执行转换
ADS_CONFIG_OS_SINGLE_CONVERT = 0x8000 #Start a single conversion (when in power-down state) 开始单次转换(在掉电状态下)
ADS_CONFIG_OS_NO_EFFECT = 0x0000 #No effect 没有效果
ADS_CONFIG_MUX_MUL_0_1 = 0x0000 #Input multiplexer,AINP = AIN0 and AINN = AIN1(default 系统默认值) 输入复用器
ADS_CONFIG_MUX_MUL_0_3 = 0x1000 #Input multiplexer,AINP = AIN0 and AINN = AIN3 输入复用器
ADS_CONFIG_MUX_MUL_1_3 = 0x2000 #Input multiplexer,AINP = AIN1 and AINN = AIN3 输入复用器
ADS_CONFIG_MUX_MUL_2_3 = 0x3000 #Input multiplexer,AINP = AIN2 and AINN = AIN3 输入复用器
ADS_CONFIG_MUX_SINGLE_0 = 0x4000 #SINGLE,AIN0
ADS_CONFIG_MUX_SINGLE_1 = 0x5000 #SINGLE,AIN1
ADS_CONFIG_MUX_SINGLE_2 = 0x6000 #SINGLE,AIN2
ADS_CONFIG_MUX_SINGLE_3 = 0x7000 #SINGLE,AIN3
ADS_CONFIG_PGA_6144 = 0x0000 #Gain= +/- 6.144V
ADS_CONFIG_PGA_4096 = 0x0200 #Gain= +/- 4.096V
ADS_CONFIG_PGA_2048 = 0x0400 #Gain= +/- 2.048V(default) 偏差正负2
ADS_CONFIG_PGA_1024 = 0x0600 #Gain= +/- 1.024V
ADS_CONFIG_PGA_512 = 0x0800 #Gain= +/- 0.512V
ADS_CONFIG_PGA_256 = 0x0A00 #Gain= +/- 0.256V
ADS_CONFIG_MODE_CONTINUOUS = 0x0000 #Device operating mode:Continuous-conversion mode 设备运行模式:连续转换模式
ADS_CONFIG_MODE_NOCONTINUOUS = 0x0100 #Device operating mode:Single-shot mode or power-down state (default) 设备运行模式:单发模式或掉电状态(默认)
ADS_CONFIG_DR_RATE_128 = 0x0000 #Data rate=128SPS 数据率
ADS_CONFIG_DR_RATE_250 = 0x0020 #Data rate=250SPS
ADS_CONFIG_DR_RATE_490 = 0x0040 #Data rate=490SPS
ADS_CONFIG_DR_RATE_920 = 0x0060 #Data rate=920SPS
ADS_CONFIG_DR_RATE_1600 = 0x0080 #Data rate=1600SPS
ADS_CONFIG_DR_RATE_2400 = 0x00A0 #Data rate=2400SPS
ADS_CONFIG_DR_RATE_3300 = 0x00C0 #Data rate=3300SPS
ADS_CONFIG_COMP_MODE_WINDOW = 0x0010 #Comparator mode:Window comparator 比较器模式:窗口比较器
ADS_CONFIG_COMP_MODE_TRADITIONAL = 0x0000 #Comparator mode:Traditional comparator (default) 比较器模式:传统比较器(默认)
ADS_CONFIG_COMP_POL_LOW = 0x0000 #Comparator polarity:Active low (default) 比较器极性:低电平有效(默认)
ADS_CONFIG_COMP_POL_HIGH = 0x0008 #Comparator polarity:Active high
ADS_CONFIG_COMP_LAT = 0x0004 #Latching comparator 锁存比较器
ADS_CONFIG_COMP_NONLAT = 0x0000 #Nonlatching comparator (default) 无锁存
ADS_CONFIG_COMP_QUE_ONE = 0x0000 #Assert after one conversion 一次转换后断言
ADS_CONFIG_COMP_QUE_TWO = 0x0001 #Assert after two conversions 两次转换后断言
ADS_CONFIG_COMP_QUE_FOUR = 0x0002 #Assert after four conversions 四次转换后断言
ADS_CONFIG_COMP_QUE_NON = 0x0003 #Disable comparator and set ALERT/RDY pin to high-impedance (default) 禁用比较器并将ALERT/RDY引脚设置为高阻抗(默认)
Config_Set = 0
class ADS1015(object):
def __init__(self,address=ADS_I2C_ADDRESS):
self._address = address
self._bus = smbus.SMBus(1)
def ADS1015_SINGLE_READ(self,channel): #Read single channel data 读取单通道数据
data=0
Config_Set = ( ADS_CONFIG_MODE_NOCONTINUOUS | #mode:Single-shot mode or power-down state (default) 模式:单触发模式或掉电状态
ADS_CONFIG_PGA_4096 | #Gain= +/- 4.096V (default)
ADS_CONFIG_COMP_QUE_NON | #Disable comparator (default)
ADS_CONFIG_COMP_NONLAT | #Nonlatching comparator (default)
ADS_CONFIG_COMP_POL_LOW | #Comparator polarity:Active low (default)
ADS_CONFIG_COMP_MODE_TRADITIONAL | #Traditional comparator (default)
ADS_CONFIG_DR_RATE_1600 ) #Data rate=1600SPS (default)
if channel == 0:
Config_Set |= ADS_CONFIG_MUX_SINGLE_0
elif channel == 1:
Config_Set |= ADS_CONFIG_MUX_SINGLE_1
elif channel == 2:
Config_Set |= ADS_CONFIG_MUX_SINGLE_2
elif channel == 3:
Config_Set |= ADS_CONFIG_MUX_SINGLE_3
Config_Set |=ADS_CONFIG_OS_SINGLE_CONVERT
self._write_word(ADS_POINTER_CONFIG,Config_Set)
#time.sleep(0.01)
data=self._read_u16(ADS_POINTER_CONVERT)>>4
#print(data)
return data
def _read_u16(self,cmd):
LSB = self._bus.read_byte_data(self._address,cmd)
MSB = self._bus.read_byte_data(self._address,cmd+1)
print((LSB << 8) + MSB,'----------')
return (LSB << 8) + MSB
def _write_word(self, cmd, val):
Val_H=val&0xff
Val_L=val>>8
val=(Val_H<<8)|Val_L
self._bus.write_word_data(self._address,cmd,val)