問題描述：

main.py

``````# coding=UTF-8
import RPi.GPIO as GPIO
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
def SendVideo():

if(state!=0x8000):
# 收集氣體數據

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) # 啓動二極管燈
time.sleep(0.00028) # 等待0.28ms

#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()
``````

``````#!/usr/bin/python
# -*- coding:utf-8 -*-
import time
import smbus

#Pointer Register

#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_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_COMP_MODE_WINDOW         = 0x0010      #Comparator mode：Window comparator 比較器模式:窗口比較器
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 四次轉換後斷言

Config_Set = 0

self._bus = smbus.SMBus(1)
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_POL_LOW             |   #Comparator polarity：Active low               (default)
if channel == 0:
elif channel == 1:
elif channel == 2:
elif channel == 3:
#time.sleep(0.01)
#print(data)
return data
return (LSB << 8) + MSB
def _write_word(self, cmd, val):
Val_H=val&0xff
Val_L=val>>8
val=(Val_H<<8)|Val_L
``````

main.py代碼中有個time.sleep(0.00986)是我看其他資料說燈有個10ms的週期，也不知道是不是這樣的，最後結果感覺去掉也沒什麼不一樣。

異常數據分析

led.py:

``````# coding=UTF-8
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
IN1 = 16
GPIO.setwarnings(False)
GPIO.setup(IN1,GPIO.OUT)
while 1:
GPIO.output(IN1,GPIO.HIGH)

time.sleep(0.028)
time.sleep(0.004) # 持續採集0.04ms
GPIO.output(IN1,GPIO.LOW) # 關閉二級管燈
time.sleep(0.986)
``````

數據分析：

b.有可能是這個模塊GP2Y1010AU0F不是特別靈敏了，現在淘寶上還有GP2Y1014AU0F甚至GP2Y1015AU0F的。15版本是用串口收發數據了，不需要管led燈的事情，相對比較方便，這個csdn也有了對應的教程與代碼。

bilibili上筆者也找到了這樣一個視頻[教程]70塊錢搞定樹莓派檢測PM2.5和有害氣體