I'm a total newbie taking a coding class in High School. I have been trying to get a portable PiCamera up and running, but I have encountered some bugs, that I do not know how to fix. Here is the code:

from gpiozero import Buttonfrom gpiozero import Button
from gpiozero import LED
from picamera import PiCamera
from datetime import datetime
from time import sleep

right_button = Button(17) #GPIO17 --- pin 11
light = LED(23) #GPIO14 --- pin 16

camera = PiCamera()

def capture():

    stamp = datetime.now().isoformat()
    camera.capture('/home/pi/%s.jpg' % stamp)

def loop():
    While True:
      right_button.when_pressed = capture()

except KeyboardInterrupt:  # When 'Ctrl+C' is pressed, the child program destroy() will be  executed.

I have currently 2 problems with running this code on my Raspberry Pi Zero. The first is that the LED does not light up, despite my efforts to plug in the ends correctly. The second is that once I press the button, the camera keeps taking pictures in an infinite loop onto the folder, until I kill the process. I can provide a picture if needed.

Anyone got any suggestions or tips? If I'm making any really dumb mistakes, I'm really sorry.

Thank you!

  • Alright, I just needed to adjust the LED and button around different pins. The picamera program now successfully works, thank you Chad!
    – Lenny
    Apr 24, 2019 at 18:51

1 Answer 1


Using this:

right_button.when_pressed = capture()

Sets value of right_button.when_pressed equal to what capture RETURNS, which is nothing, when really you want to set it to the function itself

You are continually setting the callback function to be None. And because of this, its just a while loop on capture, which means that the light.on() and the light.off() are only seperated by the time it takes to go around the loop, (instantly basicly) so you are never seeing the light come on. (and it will continue to take pictures)

If right_button.when_pressed is expecting a callback function it would look like this

def loop()
    right_button.when_pressed=capture  #sets the callback function, no "()"
    while True:
        time.sleep(1)   #do nothing in the loop, as the callback function will get called when pressed
  • Ah, I thought that you needed the parenthesis after the capture! So the reason why the light won't work is because there is no delay in between, and because of that delay, it doesn't ever stop for pictures? Thanks, I'll edit the code today and see what happens.
    – Lenny
    Apr 24, 2019 at 12:12
  • Kind of.... when you include the parenthesis it is actually going to call the function and set the variable to what it returns. and since you had that inside the loop it just kept calling the function as fast as it could. Not using the parenthesis sets the variable to the function its self, and then you wait for it to be called as a callback function for the action. That's why you set the callback and THEN go into the waiting loop. The light was working, just only on for such a small amount of time that your eye couldn't see it turn on. At least that's my guess from looking at the code.
    – Chad G
    Apr 24, 2019 at 15:15
  • If the light.off and light.on were switched, there would still be the 1 second pause in between and you you would have seen it come on. Something to keep in mind while debugging. Also adding some print statements in there would have helped you see what was going on.
    – Chad G
    Apr 24, 2019 at 15:18

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.