当前位置:   article > 正文

树莓派4b测量PM2.5

树莓派4b测量PM2.5

1.GP2Y1010AU0F粉尘传感器连接图 

2. GP2Y1010AU0F工作原理

工作原理

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

3.源代码

main.py

  1. # coding=UTF-8
  2. import RPi.GPIO as GPIO
  3. from ADC import ADS1015
  4. from ADC import ADS_POINTER_CONFIG
  5. import time
  6. import math
  7. import smbus
  8. # import serial
  9. COV_RATIO = 0.2 # //ug/mmm / mv
  10. NO_DUST_VOLTAGE = 400 # //mv
  11. SYS_VOLTAGE = 5000
  12. density = 0.0
  13. voltage = 0.0
  14. # int adcvalue=0
  15. def SendVideo():
  16. ads1015 = ADS1015()
  17. state = ads1015._read_u16(ADS_POINTER_CONFIG) & 0x8000 # 气体传感器连接确立
  18. if (state != 0x8000):
  19. print("\nADS1015 Error\n")
  20. # 收集气体数据
  21. GPIO.setmode(GPIO.BOARD)
  22. IN1 = 16
  23. GPIO.setwarnings(False)
  24. GPIO.setup(IN1, GPIO.OUT) # 初始化二极管灯
  25. GPIO.output(IN1, GPIO.LOW) # 关闭二级管灯
  26. # ser = serial.Serial("/dev/ttyAMA0",9600)
  27. # def Filter(m):
  28. while 1:
  29. GPIO.output(IN1, GPIO.HIGH) # 启动二极管灯
  30. # AIN2_DATA=ads1015.ADS1015_SINGLE_READ(2)
  31. time.sleep(0.00028) # 等待0.28ms
  32. AIN2_DATA = ads1015.ADS1015_SINGLE_READ(2)
  33. # AIN0_DATA=((AIN0_DATA*2-64)/2000.00+0.02)*2
  34. time.sleep(0.00004) # 持续采集0.04ms
  35. GPIO.output(IN1, GPIO.LOW) # 关闭二级管灯
  36. time.sleep(0.00986)
  37. voltage = (5000 / 1024.0) * AIN2_DATA * 1.1 * 2 # 计算气体浓度
  38. if (voltage >= NO_DUST_VOLTAGE):
  39. voltage -= NO_DUST_VOLTAGE
  40. density = voltage * COV_RATIO
  41. else:
  42. density = 0
  43. FG = "The current dust concentration is:" + str(round(density, 2)) + " ug/m3"
  44. # FG=voltage
  45. print(FG)
  46. time.sleep(0.100)
  47. # F6=FG.encode('utf-8')
  48. # sock.send(str.encode(str(len(F6)).ljust(16)));
  49. # sock.send(F6)
  50. if __name__ == '__main__':
  51. SendVideo()

 ADC.py

  1. #!/usr/bin/python
  2. # -*- coding:utf-8 -*-
  3. import time
  4. import smbus
  5. #i2c address
  6. ADS_I2C_ADDRESS = 0x48
  7. #Pointer Register
  8. ADS_POINTER_CONVERT = 0x00 # 指针_转换
  9. ADS_POINTER_CONFIG = 0x01 # 指针_配置
  10. ADS_POINTER_LOWTHRESH = 0x02 # 低阈值
  11. ADS_POINTER_HIGHTHRESH = 0x03 # 高阈值
  12. #Config Register
  13. ADS_CONFIG_OS_BUSY = 0x0000 #Device is currently performing a conversion 设备当前正在执行转换
  14. ADS_CONFIG_OS_NOBUSY = 0x8000 #Device is not currently performing a conversion 设备当前没有执行转换
  15. ADS_CONFIG_OS_SINGLE_CONVERT = 0x8000 #Start a single conversion (when in power-down state) 开始单次转换(在掉电状态下)
  16. ADS_CONFIG_OS_NO_EFFECT = 0x0000 #No effect 没有效果
  17. ADS_CONFIG_MUX_MUL_0_1 = 0x0000 #Input multiplexer,AINP = AIN0 and AINN = AIN1(default 系统默认值) 输入复用器
  18. ADS_CONFIG_MUX_MUL_0_3 = 0x1000 #Input multiplexer,AINP = AIN0 and AINN = AIN3 输入复用器
  19. ADS_CONFIG_MUX_MUL_1_3 = 0x2000 #Input multiplexer,AINP = AIN1 and AINN = AIN3 输入复用器
  20. ADS_CONFIG_MUX_MUL_2_3 = 0x3000 #Input multiplexer,AINP = AIN2 and AINN = AIN3 输入复用器
  21. ADS_CONFIG_MUX_SINGLE_0 = 0x4000 #SINGLE,AIN0
  22. ADS_CONFIG_MUX_SINGLE_1 = 0x5000 #SINGLE,AIN1
  23. ADS_CONFIG_MUX_SINGLE_2 = 0x6000 #SINGLE,AIN2
  24. ADS_CONFIG_MUX_SINGLE_3 = 0x7000 #SINGLE,AIN3
  25. ADS_CONFIG_PGA_6144 = 0x0000 #Gain= +/- 6.144V
  26. ADS_CONFIG_PGA_4096 = 0x0200 #Gain= +/- 4.096V
  27. ADS_CONFIG_PGA_2048 = 0x0400 #Gain= +/- 2.048V(default) 偏差正负2
  28. ADS_CONFIG_PGA_1024 = 0x0600 #Gain= +/- 1.024V
  29. ADS_CONFIG_PGA_512 = 0x0800 #Gain= +/- 0.512V
  30. ADS_CONFIG_PGA_256 = 0x0A00 #Gain= +/- 0.256V
  31. ADS_CONFIG_MODE_CONTINUOUS = 0x0000 #Device operating mode:Continuous-conversion mode 设备运行模式:连续转换模式
  32. ADS_CONFIG_MODE_NOCONTINUOUS = 0x0100 #Device operating mode:Single-shot mode or power-down state (default) 设备运行模式:单发模式或掉电状态(默认)
  33. ADS_CONFIG_DR_RATE_128 = 0x0000 #Data rate=128SPS 数据率
  34. ADS_CONFIG_DR_RATE_250 = 0x0020 #Data rate=250SPS
  35. ADS_CONFIG_DR_RATE_490 = 0x0040 #Data rate=490SPS
  36. ADS_CONFIG_DR_RATE_920 = 0x0060 #Data rate=920SPS
  37. ADS_CONFIG_DR_RATE_1600 = 0x0080 #Data rate=1600SPS
  38. ADS_CONFIG_DR_RATE_2400 = 0x00A0 #Data rate=2400SPS
  39. ADS_CONFIG_DR_RATE_3300 = 0x00C0 #Data rate=3300SPS
  40. ADS_CONFIG_COMP_MODE_WINDOW = 0x0010 #Comparator mode:Window comparator 比较器模式:窗口比较器
  41. ADS_CONFIG_COMP_MODE_TRADITIONAL = 0x0000 #Comparator mode:Traditional comparator (default) 比较器模式:传统比较器(默认)
  42. ADS_CONFIG_COMP_POL_LOW = 0x0000 #Comparator polarity:Active low (default) 比较器极性:低电平有效(默认)
  43. ADS_CONFIG_COMP_POL_HIGH = 0x0008 #Comparator polarity:Active high
  44. ADS_CONFIG_COMP_LAT = 0x0004 #Latching comparator 锁存比较器
  45. ADS_CONFIG_COMP_NONLAT = 0x0000 #Nonlatching comparator (default) 无锁存
  46. ADS_CONFIG_COMP_QUE_ONE = 0x0000 #Assert after one conversion 一次转换后断言
  47. ADS_CONFIG_COMP_QUE_TWO = 0x0001 #Assert after two conversions 两次转换后断言
  48. ADS_CONFIG_COMP_QUE_FOUR = 0x0002 #Assert after four conversions 四次转换后断言
  49. ADS_CONFIG_COMP_QUE_NON = 0x0003 #Disable comparator and set ALERT/RDY pin to high-impedance (default) 禁用比较器并将ALERT/RDY引脚设置为高阻抗(默认)
  50. Config_Set = 0
  51. class ADS1015(object):
  52. def __init__(self,address=ADS_I2C_ADDRESS):
  53. self._address = address
  54. self._bus = smbus.SMBus(1)
  55. def ADS1015_SINGLE_READ(self,channel): #Read single channel data 读取单通道数据
  56. data=0
  57. Config_Set = ( ADS_CONFIG_MODE_NOCONTINUOUS | #mode:Single-shot mode or power-down state (default) 模式:单触发模式或掉电状态
  58. ADS_CONFIG_PGA_4096 | #Gain= +/- 4.096V (default)
  59. ADS_CONFIG_COMP_QUE_NON | #Disable comparator (default)
  60. ADS_CONFIG_COMP_NONLAT | #Nonlatching comparator (default)
  61. ADS_CONFIG_COMP_POL_LOW | #Comparator polarity:Active low (default)
  62. ADS_CONFIG_COMP_MODE_TRADITIONAL | #Traditional comparator (default)
  63. ADS_CONFIG_DR_RATE_1600 ) #Data rate=1600SPS (default)
  64. if channel == 0:
  65. Config_Set |= ADS_CONFIG_MUX_SINGLE_0
  66. elif channel == 1:
  67. Config_Set |= ADS_CONFIG_MUX_SINGLE_1
  68. elif channel == 2:
  69. Config_Set |= ADS_CONFIG_MUX_SINGLE_2
  70. elif channel == 3:
  71. Config_Set |= ADS_CONFIG_MUX_SINGLE_3
  72. Config_Set |=ADS_CONFIG_OS_SINGLE_CONVERT
  73. self._write_word(ADS_POINTER_CONFIG,Config_Set)
  74. #time.sleep(0.01)
  75. data=self._read_u16(ADS_POINTER_CONVERT)>>4
  76. #print(data)
  77. return data
  78. def _read_u16(self,cmd):
  79. LSB = self._bus.read_byte_data(self._address,cmd)
  80. MSB = self._bus.read_byte_data(self._address,cmd+1)
  81. print((LSB << 8) + MSB,'----------')
  82. return (LSB << 8) + MSB
  83. def _write_word(self, cmd, val):
  84. Val_H=val&0xff
  85. Val_L=val>>8
  86. val=(Val_H<<8)|Val_L
  87. self._bus.write_word_data(self._address,cmd,val)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/587340?site
推荐阅读
相关标签
  

闽ICP备14008679号