0

I am using python, and I am getting an error where it says "GPIO.output(24, False)" in the else statement. I don't know what it means, or how I fix it.

from picamera import PiCamera
from time import sleep
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)#Button to GPIO23
camera = PiCamera()

try:
    while True:
        button_state = GPIO.input(23)
        if button_state == False:
            GPIO.output(24, True)
            print('Button Pressed...')
            camera.start_preview()
            sleep(10)
            time.sleep(1)  
        else:
            GPIO.output(24, False)
            camera.stop_preview()

except KeyboardInterrupt:
    GPIO.cleanup()
2
  • 1
    Python gives clear error messages. This appears to be a Python question, i.e. not Pi specific, so off-topic.
    – joan
    Jun 8, 2022 at 21:15
  • reduce your code to only print a message when the button is pressed ... camera and button are not related, they are two completely separate devices
    – jsotola
    Jun 9, 2022 at 1:26

1 Answer 1

2

You need to setup GPIO24 as output at the start of your code:

GPIO.setup(24, GPIO.OUT)
10
  • The error has gone away, but the camera still doesn't preview when I press the button. That might be a problem with some of the other code though. Thanks
    – Jase Anger
    Jun 8, 2022 at 21:25
  • Now it is saying "This channel is already in use, continuing anyway. Use GPIO.setwarnings(False) to disable warnings. GPIO.setup(24, GPIO.OUT)" Nothing is plugged into GPIO 24 though. –
    – Jase Anger
    Jun 8, 2022 at 21:40
  • Don't worry about the warning. Does your camera actually work if you just use 'from picamera import PiCamera,from time import sleep, camera = PiCamera(), camera.start_preview(),sleep(10)' ?
    – CoderMike
    Jun 8, 2022 at 21:43
  • Yes it does when I do something simple like that.
    – Jase Anger
    Jun 8, 2022 at 21:47
  • Amend your question with your new code and add print(button_state) after you set it. Does the output change when you press your button?
    – CoderMike
    Jun 8, 2022 at 21:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.