簡體   English   中英

Raspberry Pi模擬讀取

[英]Raspberry Pi analog read

試圖從Arduino模擬引腳0獲得值以在Tkinter中顯示為實時值,但是我是Python的新手。

from Tkinter import *
import RPi.GPIO as GPIO
import time
from pyfirmata import Arduino, util
from time import sleep

board = Arduino ('/dev/ttyUSB0')
GPIO.setwarnings(False)

 ------------------------BOTTOM of the code----------------------------------
 root = Tk()
 frame = Frame(root,height=500,width=500)
 frame.pack()
 root.title("Relay Controle")
 # Raspsberry output pin 11 / relay
 relayButton = Button(root, text="Turn RELAY On", command=relay)
 relayButton.place(x=10,y=2,)
 # Arduino maga: led button
 ledButton = Button(root, text="Turn LED pin 13 On", command=led )
 ledButton.place(x=130,y=2,)
 # value print out
 # Quit Button
 quitButton = Button(root, text="Quit", command=func1 )
 quitButton.place(x=444,y=470,)
 root.mainloop()
root = Tk()
frame = Frame(root,height=500,width=500)
frame.pack()
my_label = StringVariable()
label = Label(frame,textvariable=my_label)
label.pack()
def updateLabel():
    my_label.set("%0.2f"%board.analog[0].read()))
    root.after(100,updateLabel)
root.after(100,updateLabel)
root.title("Relay Controle")

這只會每100毫秒調用一次updateLabel ... updateLabel然后使用模擬讀取的新值刷新stringvariable ...

根據文檔讀取模擬引腳

#setup to recieve analog data
it = util.iterator(board)
it.start()
board.analog[0].enable_reporting()
# later to read
board.analog[0].read()

否則您會提早獲得模擬引腳的實例

analog_0 = board.get_pin(’a:0:i’)
#then later you can read it
analog_0.read()

所以我的猜測可能是您沒有在進行設置

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM