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)
請問能不能提供連接的照片?
回覆刪除謝謝
請問 我照你寫的意思sudo pip install spidev
回覆刪除/boot/config.txt ,但是我的樹梅派卻終端機顯示sudo: pi: command not found 可以告訴我該怎麼辦嗎?
sudo apt-get install python-pip
回覆刪除感謝回覆, 目前遇到找不到套件 pip的訊息,想請問要去哪找安裝套件?
回覆刪除可以煩請南犬大大回應解決我遇到的問題 拜託了
回覆刪除sudo apt-get install python-pip
回覆刪除sudo apt-get install python3-pip
兩個都試試, 要先灌pip
目前已經都灌好,但在終端機執行時指令,數值沒辦法跑出來,請問是哪出錯了
回覆刪除煩請南犬大大回應解決我遇到的問題 拜託了
回覆刪除pm_data 先看有沒有讀到data .. 沒有的話就沒辦法!
回覆刪除