简体   繁体   English

Raspberry Pi模拟读取

[英]Raspberry Pi analog read

Trying to get the value from Arduino analog pin 0 to be shown as a live value in Tkinter but I'm new to Python. 试图从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")

this just calls updateLabel every 100 miliseconds ... updateLabel then refreshes the stringvariable with the new value from the analog read ... 这只会每100毫秒调用一次updateLabel ... updateLabel然后使用模拟读取的新值刷新stringvariable ...

per the docs to read an analog pin 根据文档读取模拟引脚

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

or you get an instance of the analog pin early 否则您会提早获得模拟引脚的实例

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

so my guess is maybe you arenot doing that setup part 所以我的猜测可能是您没有在进行设置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM