当我使用 sudo python3 program.py 执行并按 de switch 1 时,会引发下一个异常:
Taking picture...
Picture takeng...
Traceback (most recent call last):
File "main.py", line 21, in <module>
if GPIO.input(switch1):
RuntimeError: You must setup() the GPIO channel first
我在这个项目中使用了 raspberry cam 库和 rpi.gpio 库。有人知道我的代码发生了什么吗?
import RPi.GPIO as GPIO
import time
import picamera
# initial config for gpio ports
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
# input switches
switch1 = 22
switch2 = 23
switch3 = 24
# setup
GPIO.setup(switch1, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(switch2, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(switch3, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# main loop
while True:
if GPIO.input(switch1):
print ("Taking picture...")
with picamera.PiCamera() as camera:
camera.resolution = (1280, 720)
camera.start_preview()
time.sleep(0.5)
camera.capture("test.jpg")
print ("Picture takeng...")
elif GPIO.input(switch2):
print ("Taking video...")
elif GPIO.input(switch3):
print ("Poweroff...")
break
GPIO.cleanup()
最佳答案
该错误告诉您尚未将引脚设置为输入,当您尝试这样访问它们时,会失败。我遇到了类似的问题,据我所知它应该可以工作(毕竟您正在设置引脚)。
尝试将GPIO.setmode(GPIO.BCM)
更改为GPIO.setmode(GPIO.BOARD)
。您还必须将密码更改为实际密码(您的密码为 15、16 和 18)。
我仍然不知道为什么,但它对我的代码起了作用。
关于python - 运行时错误: You must setup() the GPIO channel first,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21194666/