0

I have set up a on my bread board a button and trying to take images when I press it. I also have a led that turns on while pressing the button. All works fine, however, when I run the python script, it does not wait for me to press the button and automatically takes the image with an error

Here are images of my board and Pi.

enter image description here enter image description here enter image description here

Below is my code and the error

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

GPIO.setmode(GPIO.BCM)

sleepTime = 0.1

#GPIO pin components
lightPin = 4
buttonPin = 17

GPIO.setup(lightPin, GPIO.OUT)
GPIO.setup(buttonPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.output(lightPin, False)

try:
    while True:
        GPIO.output(lightPin, not GPIO.input(buttonPin))
        camera = picamera.PiCamera()
        camera.capture('image.jpg')
        sleep(sleepTime)

finally:
    GPIO.output(lightPin, False)
    GPIO.cleanup()

The error

Python 3.5.3 (default, Jan 19 2017, 14:11:04) 
[GCC 6.3.0 20170124] on linux
Type "copyright", "credits" or "license()" for more information.
>>> 
================= RESTART: /home/pi/Desktop/doorbelPhoto.py =================
Traceback (most recent call last):
  File "/home/pi/Desktop/doorbelPhoto.py", line 25, in <module>
    camera = picamera.PiCamera()
  File "/usr/lib/python3/dist-packages/picamera/camera.py", line 433, in __init__
    self._init_preview()
  File "/usr/lib/python3/dist-packages/picamera/camera.py", line 513, in _init_preview
    self, self._camera.outputs[self.CAMERA_PREVIEW_PORT])
  File "/usr/lib/python3/dist-packages/picamera/renderers.py", line 558, in __init__
    self.renderer.inputs[0].connect(source).enable()
  File "/usr/lib/python3/dist-packages/picamera/mmalobj.py", line 2212, in enable
    prefix="Failed to enable connection")
  File "/usr/lib/python3/dist-packages/picamera/exc.py", line 184, in mmal_check
    raise PiCameraMMALError(status, prefix)
picamera.exc.PiCameraMMALError: Failed to enable connection: Out of resources
>>> 

1 Answer 1

1

The error is due to the camera object being re-initialised (camera = picamera.PiCamera()) - move this outside the while loop. There is also nothing waiting for a button press.

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

GPIO.setmode(GPIO.BCM)

sleepTime = 0.1

#GPIO pin components
lightPin = 4
buttonPin = 17
camera = picamera.PiCamera()

GPIO.setup(lightPin, GPIO.OUT)
GPIO.setup(buttonPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.output(lightPin, False)

try:
    while True:
        while True:
            press = GPIO.input(buttonPin)
            if (press == 1):
                break
            GPIO.output(lightPin, not GPIO.input(buttonPin))
            print('capture')
            camera.capture('image.jpg')
            sleep(sleepTime)
        GPIO.output(lightPin, 0)
        sleep(sleepTime)
finally:
    GPIO.output(lightPin, False)
    GPIO.cleanup()
4
  • does not work. keeps on printing capture non stop and does nothing on press of button.
    – tony9099
    Commented Sep 22, 2017 at 15:49
  • Works for me. Can you post a photo of your breadboard setup and connection to the GPIO?
    – CoderMike
    Commented Sep 22, 2017 at 16:00
  • check my updated question
    – tony9099
    Commented Sep 22, 2017 at 16:26
  • It works. I had 2 lines of code indented in a wrong way. my bad. Thanks a lot for your time.
    – tony9099
    Commented Sep 22, 2017 at 16:35

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.