gpt4 book ai didi

python - arduino 读入 pyfirmata 给出的输出为 none

转载 作者:行者123 更新时间:2023-12-05 05:21:21 24 4
gpt4 key购买 nike

我在 python3 中使用 pyfirmata 编写了一个用于从模拟引脚 0 读取值的基本代码(我连接了一个光传感器,输出来自模拟引脚 0),但无论如何它都将输出设为 none .我在 arduino IDE 中尝试了相同的代码,给出了正确的答案。请帮忙。

代码是:

from pyfirmata import Arduino, util
import time
board = Arduino('/dev/cu.usbmodem1411')

it = util.Iterator(board)
it.start()

board.analog[0].enable_reporting()

while True :
print (board.analog[0].read())
time.sleep(1)

即使它在几秒钟后给出输出,它也会给出 0.29,这实际上不是串行监视器上的传感器值。该值在 0 到 1023 之间变化,并且相对比这个大很多。

最佳答案

Arduino 的模拟引脚将 0 和 +5V 之间的输入电压线性转换为 0和 1023。但是,在 pyFirmata 中,0 和 +5V 之间的值被线性转换转换为 0 和 1.0 的浮点值。例如,如果模拟引脚的电压为 1V,则Arduino 程序将测量一个大约 204 左右的值,但您将收到在 Python 中使用 pyFirmata 的 read() 方法时将值浮点为 0.2。

关于python - arduino 读入 pyfirmata 给出的输出为 none,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43054936/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com