Ferdinand Lucatiere Ferdinand Lucatiere - 1 year ago 325
Python Question

RuntimeError: You must setup() the GPIO channel first

When i execute with sudo python3 program.py and press de switch 1 throws the next exception:

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

I use a raspberry cam library and rpi.gpio library for this project. Anyone knows what happend in my code ?

import RPi.GPIO as GPIO
import time
import picamera

# initial config for gpio ports

# 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)
print ("Picture takeng...")
elif GPIO.input(switch2):
print ("Taking video...")
elif GPIO.input(switch3):
print ("Poweroff...")


Answer Source

The error is telling you that you have not set the pins to work as input and, when you try to access them as so, it fails. I had a similar problem and as far as I see it it should work (you are setting the pins after all).

Try changing GPIO.setmode(GPIO.BCM) to GPIO.setmode(GPIO.BOARD). You will also have to change the pin numbers to the physical ones (yours would be 15, 16 and 18). I still don't know why, but it did the trick on my code.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download