# Python硬件编程示例:使用Raspberry Pi控制LED灯
import RPi.GPIO as GPIO
import time
# 设置GPIO模式为BCM
GPIO.setmode(GPIO.BCM)
# 定义引脚编号
led_pin = 18
# 设置引脚为输出模式
GPIO.setup(led_pin, GPIO.OUT)
try:
while True:
# 点亮LED
GPIO.output(led_pin, GPIO.HIGH)
print("LED is ON")
time.sleep(1) # 持续1秒
# 关闭LED
GPIO.output(led_pin, GPIO.LOW)
print("LED is OFF")
time.sleep(1) # 持续1秒
except KeyboardInterrupt:
# 捕获键盘中断(Ctrl + C)
print("Program stopped by User")
finally:
# 清理GPIO设置
GPIO.cleanup()
上一篇:python字符串转int
下一篇:python编程例子
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站