GPIO 瞬时按钮控制继电器
GPIO Momentary button controls relay
我想知道是否可以让我的瞬时按钮在按住按钮的同时控制 Python 中的继电器?
本质上,按下按钮时,继电器打开,松开按钮时,继电器关闭。
目前,我可以用一个小 python 脚本控制继电器,将其打开持续 5 秒:
relay.py:
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(4, GPIO.OUT)
GPIO.output(4, GPIO.LOW)
time.sleep(5)
GPIO.output(4, GPIO.HIGH)
GPIO.cleanup()
print "Done!"
并且能够在按下瞬时按钮时进行跟踪:
button.py:
import os
from time import sleep
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(25, GPIO.IN)
while True:
if (GPIO.input(25) == False):
print "Button being pushed!"
GPIO.output(4, GPIO.LOW)
sleep(0.025)
我试图在这个时尚中将两者结合起来:
import os
from time import sleep
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(25, GPIO.IN)
GPIO.setup(4, GPIO.OUT)
while True:
if (GPIO.input(25) == False):
print "Button being pushed!"
GPIO.output(4, GPIO.LOW)
sleep(0.025)
GPIO.output(4, GPIO.HIGH)
但无济于事。任何帮助将不胜感激!
当然,这应该可以解决问题。我没有对其进行测试,因此可能需要进行一些小的修改。
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
# Added a software pull-up here for increased stability
GPIO.setup(25, GPIO.IN, GPIO.PUD_UP)
GPIO.setup(4, GPIO.OUT)
# The relay seems to be active low. Default to off.
GPIO.output(4, GPIO.HIGH)
# This function is run every time the button state changes
# from low to high or high to low (GPIO.BOTH)
def callback_func(pin):
# The button transitioned to high (released)
if GPIO.input(pin):
# Relay off
GPIO.output(4, GPIO.HIGH)
else:
# The button went low (it was pressed). Relay on.
GPIO.output(4, GPIO.LOW)
# Set up threaded event detection (no constant polling of pin states).
# Fires on both high-to-low and low-to-high state changes.
GPIO.add_event_detect(25, GPIO.BOTH, callback=callback_func)
def main():
while True:
print "Not blocking! You're free to do other stuff here"
time.sleep(5)
if __name__ == "__main__":
main()
如果GPIO.PUD_UP
在释放按钮时没有引起上升沿,则必须在 VCC 上添加一个电阻。无论如何这样做都是个好主意,因为即使软件出现故障,它也能确保继电器保持默认状态。
一个更丑陋的版本,在按下按钮时不断写入继电器 GPIO,但应该可以在没有上拉的情况下工作:
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(25, GPIO.IN, GPIO.PUD_UP)
GPIO.setup(4, GPIO.OUT)
GPIO.output(4, GPIO.HIGH)
def callback_func(pin):
while not GPIO.input(pin):
GPIO.output(4, GPIO.LOW)
GPIO.output(4, GPIO.HIGH)
GPIO.add_event_detect(25, GPIO.FALLING, callback=callback_func, bouncetime=100)
def main():
while True:
print "Not blocking! You're free to do other stuff here"
time.sleep(5)
if __name__ == "__main__":
main()
我想知道是否可以让我的瞬时按钮在按住按钮的同时控制 Python 中的继电器?
本质上,按下按钮时,继电器打开,松开按钮时,继电器关闭。
目前,我可以用一个小 python 脚本控制继电器,将其打开持续 5 秒:
relay.py:
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(4, GPIO.OUT)
GPIO.output(4, GPIO.LOW)
time.sleep(5)
GPIO.output(4, GPIO.HIGH)
GPIO.cleanup()
print "Done!"
并且能够在按下瞬时按钮时进行跟踪:
button.py:
import os
from time import sleep
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(25, GPIO.IN)
while True:
if (GPIO.input(25) == False):
print "Button being pushed!"
GPIO.output(4, GPIO.LOW)
sleep(0.025)
我试图在这个时尚中将两者结合起来:
import os
from time import sleep
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(25, GPIO.IN)
GPIO.setup(4, GPIO.OUT)
while True:
if (GPIO.input(25) == False):
print "Button being pushed!"
GPIO.output(4, GPIO.LOW)
sleep(0.025)
GPIO.output(4, GPIO.HIGH)
但无济于事。任何帮助将不胜感激!
当然,这应该可以解决问题。我没有对其进行测试,因此可能需要进行一些小的修改。
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
# Added a software pull-up here for increased stability
GPIO.setup(25, GPIO.IN, GPIO.PUD_UP)
GPIO.setup(4, GPIO.OUT)
# The relay seems to be active low. Default to off.
GPIO.output(4, GPIO.HIGH)
# This function is run every time the button state changes
# from low to high or high to low (GPIO.BOTH)
def callback_func(pin):
# The button transitioned to high (released)
if GPIO.input(pin):
# Relay off
GPIO.output(4, GPIO.HIGH)
else:
# The button went low (it was pressed). Relay on.
GPIO.output(4, GPIO.LOW)
# Set up threaded event detection (no constant polling of pin states).
# Fires on both high-to-low and low-to-high state changes.
GPIO.add_event_detect(25, GPIO.BOTH, callback=callback_func)
def main():
while True:
print "Not blocking! You're free to do other stuff here"
time.sleep(5)
if __name__ == "__main__":
main()
如果GPIO.PUD_UP
在释放按钮时没有引起上升沿,则必须在 VCC 上添加一个电阻。无论如何这样做都是个好主意,因为即使软件出现故障,它也能确保继电器保持默认状态。
一个更丑陋的版本,在按下按钮时不断写入继电器 GPIO,但应该可以在没有上拉的情况下工作:
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(25, GPIO.IN, GPIO.PUD_UP)
GPIO.setup(4, GPIO.OUT)
GPIO.output(4, GPIO.HIGH)
def callback_func(pin):
while not GPIO.input(pin):
GPIO.output(4, GPIO.LOW)
GPIO.output(4, GPIO.HIGH)
GPIO.add_event_detect(25, GPIO.FALLING, callback=callback_func, bouncetime=100)
def main():
while True:
print "Not blocking! You're free to do other stuff here"
time.sleep(5)
if __name__ == "__main__":
main()