LED不闪烁,每次运行python程序时都会收到此错误。
blink.py:4:运行时警告:此通道已在使用中,无论如何仍在继续。 使用 GPIO.setwarnings(False( 禁用警告。 GPIO.setup(16,GPIO.出(
blink.py:5:运行时警告:此通道已在使用中,无论如何仍在继续。 使用 GPIO.setwarnings(False( 禁用警告。 GPIO.setup(18,GPIO.出(
我已经对这个问题做了一些研究,但没有一个解决方案有效
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(16,GPIO.OUT)
GPIO.setup(18,GPIO.OUT)
while True:
GPIO.output(16, GPIO.HIGH)
GPIO.output(18, GPIO.LOW)
time.sleep(1)
GPIO.output(16, GPIO.LOW)
GPIO.output(18, GPIO.HIGH)
time.sleep(1)
有人有解决方案吗?
您是否尝试过在while循环中使用try/except/finally 块来处理错误?(如果您不熟悉尝试/例外/最终,请告诉我(。
下面是一个你可以看的例子 http://raspi.tv/2013/rpi-gpio-basics-3-how-to-exit-gpio-programs-cleanly-avoid-warnings-and-protect-your-pi
祝你好运!
这是因为 GPIO 引脚已在使用中。在此之前,它们是在另一个脚本中设置的吗?每次使用后都应执行 GPIO 清理。正如@Tommi在他们的回答中提到的,尝试/例外/最终块对于之后触发清理很有用。这是一个演示这一点的示例,改编自本网站。
import RPi.GPIO as GPIO
import time
# Consider calling GPIO.cleanup() first
GPIO.setmode(GPIO.BCM)
GPIO.setup(16,GPIO.OUT)
GPIO.setup(18,GPIO.OUT)
try:
# Your code here
while True:
GPIO.output(16, GPIO.HIGH)
GPIO.output(18, GPIO.LOW)
time.sleep(1)
GPIO.output(16, GPIO.LOW)
GPIO.output(18, GPIO.HIGH)
time.sleep(1) # This line should be here so it is part of the while loop
except KeyboardInterrupt:
# Here you put any code you want to run before the program
# exits when you press CTRL+C
print("Keyboard interrupt")
except:
# This catches ALL other exceptions including errors.
# You won't get any error messages for debugging
# so only use it once your code is working
print("Other error or exception occurred!")
finally:
GPIO.cleanup() # This ensures a clean exit
我发现树莓派 3B GPIO 引脚不按顺序排列,我使用它们的地方说它是有序的,但事实并非如此。因此,在修复一盏灯闪烁并且一盏灯持续亮起后,我需要两个灯都闪烁,我会进一步查看。