需要忽略输入的非锁存 GPIO 输出
编程新手,所以我会尽力解释一下:
我正在尝试在太阳能设置中使用 Pi Zero 来切换 x86 主板上的开机输入(基本上作为电源按钮操作)。这个想法是,当电池电压足够高时,电池电压监视器将 Pi 上的引脚 14 设置为高电平。该脚本读取此输入,然后切换 Pi 上的输出 (26),进而关闭继电器一秒钟,然后激活主板上的开机输入。 问题是我无法在一秒后关闭输出(26)。该脚本不会忽略引脚 14,而只是将输出 (26) 保持为高电平。 (引脚 14 自然会被电池监视器保持为高电平,直到电池电量降至某个阈值以下)
import RPi.GPIO as GPIO
from time import sleep
GPIO.setmode(GPIO.BCM)
GPIO.setup(14,GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(26,GPIO.OUT)
GPIO.output(26, 1)
while True:
if GPIO.input(14)==1:
GPIO.output(26, 0)
sleep(1)
GPIO.output(26, 1)
else:
GPIO.output(26, 1)
GPIO.cleanup()
New to programming, so I'll try my best to explain:
I'm trying to use a Pi Zero in my solar setup to switch the power-on input on an x86 motherboard (basically operating as the power button). The idea is that a battery voltage monitor sets pin 14 on the Pi high when the battery is at a high enough voltage. The script reads this input then switches an output (26) on the Pi, which in turn closes a relay for one second, which will then activate the power-on input on the motherboard.
The problem is that I cannot get the output (26) to turn off after one second. The script won't ignore pin 14, and just keeps the output (26) high. (Pin 14 is naturally held high by the battery monitor until the battery level falls down below a certain threshold)
import RPi.GPIO as GPIO
from time import sleep
GPIO.setmode(GPIO.BCM)
GPIO.setup(14,GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(26,GPIO.OUT)
GPIO.output(26, 1)
while True:
if GPIO.input(14)==1:
GPIO.output(26, 0)
sleep(1)
GPIO.output(26, 1)
else:
GPIO.output(26, 1)
GPIO.cleanup()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一种可能的解决方案,轮询 GPIO14 1/秒的值
并使用变量
power_on
来确定我们如何响应value:
或者,您可以让代码块直到引脚 14 发生变化,从而导致如下情况:
这使用
GPIO.wait_for_edge()
等待 GPIO14 上的上升沿(低 ➡️ 高转换) ,然后触发你的继电器。Here's one possible solution that polls the value of GPIO14 1/second
and uses the variable
power_on
to figure out how we respond to thevalue:
Alternatively, you can have your code block until pin 14 changes, leading to something like this:
This uses
GPIO.wait_for_edge()
to wait for a rising edge on GPIO14 (a low ➡️ high transition), and then triggers your relay.