RuntimeError: You must setup() the GPIO channel first

13,804

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.

Share:
13,804
Admin
Author by

Admin

Updated on June 04, 2022

Comments

  • Admin
    Admin almost 2 years

    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
    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()