You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
801 B
37 lines
801 B
|
3 months ago
|
# region 继电器
|
||
|
|
|
||
|
|
|
||
|
|
import RPi.GPIO as GPIO
|
||
|
|
|
||
|
|
|
||
|
|
import time
|
||
|
|
# GPIO 代码
|
||
|
|
# 定义继电器引脚
|
||
|
|
EN_Relay = 17
|
||
|
|
|
||
|
|
|
||
|
|
def init_gpio():
|
||
|
|
"""初始化GPIO设置"""
|
||
|
|
GPIO.setmode(GPIO.BCM)
|
||
|
|
GPIO.setup(EN_Relay, GPIO.OUT)
|
||
|
|
# 初始化为高电平(关闭状态)
|
||
|
|
GPIO.output(EN_Relay, GPIO.LOW)
|
||
|
|
time.sleep(1) # 等待稳定
|
||
|
|
|
||
|
|
|
||
|
|
def turn_on_relay():
|
||
|
|
"""打开继电器(设置为LOW)"""
|
||
|
|
try:
|
||
|
|
GPIO.output(EN_Relay, GPIO.HIGH)
|
||
|
|
#print('Relay turned on (LOW)')
|
||
|
|
except Exception as e:
|
||
|
|
print(f"Error turning on relay: {e}")
|
||
|
|
|
||
|
|
|
||
|
|
def turn_off_relay():
|
||
|
|
"""关闭继电器(设置为HIGH)"""
|
||
|
|
try:
|
||
|
|
GPIO.output(EN_Relay, GPIO.LOW)
|
||
|
|
#print('Relay turned off (HIGH)')
|
||
|
|
except Exception as e:
|
||
|
|
print(f"Error turning off relay: {e}")
|