Raspberry Pi模拟读取

时间:2015-01-28 17:39:54

标签: python tkinter arduino raspberry-pi

试图从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()

1 个答案:

答案 0 :(得分:0)

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()

所以我的猜测可能是你没有做那个设置部分