sharp GP2Y1014AU
參考用 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
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)
# 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)
print "Read Data : ",pm_data,"volts : ",pm_volts," (",pm_value*1000,"ug/m3)"
# Delay seconds
time.sleep(delay)