2017年3月15日 星期三

raspberry pi 用sharp GP2Y1014AU偵測空氣品質.

由於pi 沒有analog input. 所以必須要另外加MCP3008
「raspberry pi GPIO 腳位」的圖片搜尋結果

sharp GP2Y1014AU
21511649203037_699







參考用 gp2y1010au

PIN 1 接150歐姆電阻 電阻另一端接到5V(pi PIN 2)
           接16V 220uF. 負極端接地 (pi PIN 6)
PIN 2/4 接地 (pi PIN 6)
PIN 6 接 5V (pi PIN 2)
PIN 5 接MCP3008 CH0
PIN 3 接raspberry pi GPIO17/PIN 11

MCP 3008

MCP3008
PIN 16, 接pi 5V (pi PIN 2)
PIN 15, 接pi 5V (pi PIN 2)
PIN 14, PIN9 接 pi PIN 6
PIN 10, pi PIN 24 SPI0_CE0(GPIO8)
PIN 11, pi PIN 19 SPI0_MISI (GPIO10)
PIN 12, pi PIN 21 SPI0_MISO(GPIO9)
PIN 13, pi PIN 23 SCLK (GPIO 11)

pi 上確認
  有 sudo pip install spidev
  /boot/config.txt 有加以下兩行
   dtparam=spi=on
   dtparam=i2c_arm=on

測到數值:
  3000+ = very bad
  1050-3000 = bad
  300-1050 = normal
  150-300 = good
  75-150 = very good
  0-75 = best

python code :


import spidev
import time
import os
import RPi.GPIO as GPIO

# open(bus, device) : open(X,Y) will open /dev/spidev-X.Y
spi = spidev.SpiDev()
spi.open(0,0)

# Read SPI data from MCP3008, Channel must be an integer 0-7

#this function should remove " 
def ReadADC(ch):
    if ((ch ">" 7) or (ch "<" 0)):
       return -1
    adc = spi.xfer2([1,(8+ch)"<""<"4,0])
    data = ((adc[1]&3)"<""<"8) + adc[2]
    return data

# Convert data to voltage level
def ReadVolts(data):
  volts = (data * 5.0) / float(1023)
  return volts

# Define sensor channels
pm_ch = 0

# Define delay between readings
delay = 5
R1 = 17

#enable GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(R1,GPIO.OUT)

while True:
  #set GPIO R1 17
  GPIO.output(R1,GPIO.HIGH)

  # Read the temperature sensor data
  pm_data = ReadADC(pm_ch)
  pm_volts = ReadVolts(pm_data)
  pm_value = pm_volts * 0.17 - 0.1

  #disbale GPIO R1 17
  GPIO.output(R1,GPIO.LOW)

  # Print out results
  print "Read Data : ",pm_data,"volts : ",pm_volts," (",pm_value*1000,"ug/m3)"

# Delay seconds
time.sleep(delay)


2017年3月14日 星期二

逆止風門DIY

網路購入
廚房抽油煙機排煙管用逆止風門 : 850
浴廁使用 : 390

使用膠帶纏繞就可以完成. 防止外面風灌進來, 蚊蟲飛入.