简体   繁体   English

使用Raspberry Pi和Python3进行中继控制

[英]Relay control with Raspberry Pi and Python3

below is a section of code I am using to control a relay to open electric gates via a relay which is controlled via pin 7 on a raspberry pi GPIO. 以下是一段代码,我将用来控制继电器通过一个继电器打开电子门,该继电器是通过树莓派GPIO上的引脚7控制的。 The gates only need a momentary voltage (via the relay contacts) to open. 栅极仅需要瞬时电压(通过继电器触点)即可打开。

My question is, what do I need to add to this code to make the relay only switch on for 0.5 Seconds when pin 7 goes high. 我的问题是,当引脚7变为高电平时,我需要添加什么以使继电器仅在0.5秒内打开。 This would enable relay to return to the off state and then wait for the next time GPIO pin 7 goes high, the gates do not need any commands from the GPIO to close after a certain time, they close under the control of the separate gate control system. 这将使继电器返回到关闭状态,然后等待下一次GPIO引脚7变为高电平时,门在一定时间后不需要来自GPIO的任何命令即可关闭,它们在单独的门控制的控制下关闭系统。

if name=="gate":
GPIO.setmode(GPIO.BOARD) ## Use board pin numbering
GPIO.setup(int(7), GPIO.OUT)   ## Setup GPIO Pin to OUTPUT
GPIO.output(int(7), state) ## State is true/false

Many thanks 非常感谢

Peter 彼得

In this example I will use gpiozero library instead of RPi.GPIO because I like the way, how this library handle events. 在此示例中,我将使用gpiozero库而不是RPi.GPIO因为我喜欢该库处理事件的方式。

from gpiozero import Button, OutputDevice
from time import sleep
from signal import pause

buttonPin = 4
relayPin = 7

button = Button(buttonPin)
button.when_pressed = ButtonPressedCallback
relay = OutputDevice(relayPin)

def ButtonPressedCallback():
    relay.on()
    sleep(0.5)
    relay.off()

pause()

I hope, I understood your question good. 希望我能很好地理解您的问题。

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

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