Push Button > Take Picture…

This tutorial is a request from a visitor to this site. He asked how to take a photo when a button is pressed.

Difficulty = Beginner


  • Raspberry Pi
  • Webcam
  • Push button/breadboard/wires

First thing is to ensure we have the relevant software installed on our Pi. We need to have ‘fswebcam’ to communicate with the camera and wiringPi to communicate with the gpio button. Open a terminal window and type the following:

sudo apt-get install fswebcam

To install wiringPi type the following into the terminal:

git clone git://git.drogon.net/wiringPi

Once downloaded then type:

cd wiringPi

This will install wiringPi on your system.

Single Button gpio

Single Button gpio

Once these have been installed we need to set up the breadboard and button. We will be using wiringPi gpio 16 which is pin 10 on the Raspberry Pi header. Place the button on your breadboard and connect one leg to wiringPi gpio pin 16. Conect the other leg to ground (GND) on the Raspberry Pi. Easy peasy!

We need to make a new file to store the code. Open a terminal window and type:

sudo nano photo.sh

Enter the following code into the file (I have included comments explaining the code):

# Time-lapse capture script
# Andy Ibbitson 17-03-2013

 # Define the wiringPi gpio pin (16) to be used by the button...

 setup ()
   echo Setup
   gpio mode $button in
 # Set a function to check for a button press...
 # Wait for the button to be pressed. Because we have the GPIO
 # pin pulled high, we wait for it to go low to indicate a push.
 waitButton ()
   echo "Press the button to take a photo or [CTRL+C] to quit... "
   while [ `gpio read $button` = 1 ]; do
     sleep 0.5
 beginCapture ()

   # Generate the date to add to the filename...
   today=$(date +"%d-%m-%Y-%H-%M")

   # use fswebcam to take a photo.  You may need to fiddle with the settings, 
   # especially the resolution, to match your webcam details...
   fswebcam -r 1280x720 -S 15 --jpeg 99 -p MJPEG --shadow --title "ENTER TITLE HERE" --subtitle "ENTER SUBTITLE HERE" --info "Author: ENTER NAME HERE" --save photo_"$today".jpg -q
   echo "Your photo has been taken..."

 while true;

Now make the file executable:

sudo chmod +x photo.sh

execute the file by typing:


There you go. Set up the webcam and press the button to take the photo. It will be saved to the same location as the file.

Happy snapping 😀

This entry was posted in bash, Button, GPIO, raspberry pi, Scripting, Tutorial, Webcam. Bookmark the permalink.

15 Responses to Push Button > Take Picture…

  1. arungopal says:

    when iam type sudo apt-get install wiringpi..it shows unable to locate package wiring pi..what iam do?thanks For giving reply.


  2. arungopal says:

    iam copy and paste u r code in raspberry pi.and change the permissions..when iam excute that file it shows
    syntax error:end of file unexpected (expecting “done”)…what iam do?iam installing wiring pi on my raspberry pi..but wiringpi shows error?what iam do?when iam press button after execution of file?Please give me suggestions..


    • drewibbo says:

      I have ammended the tutorial with the correct procedure to install wiringPi.

      There was also a slight error in my code. Please use the ammended script above.

      You should have no problems now. Just follow the instructions above.

      Andy 😀

  3. arungopal says:

    i have one dought…r u did this by using writing kernel module driver..That is gpio interupt with camera capturing photos…R u did this?that is writing gpio kernel module driver and run the camera application in the user level…..That is Peoject..r u did this?


    • drewibbo says:

      This script should work if you follow the instructions above. You should not need to install any other software or kernel drivers…
      When you push the button the current to pin 16 is pulled “low”. This tells the script to activate “fswebcam” software and take the photo.

      What webcam are you using? Sometimes the webcam causes problems. Is it attached to a powered hub?

      You might want to type just the following into a terminal window to see if the camera is recognised by fswebcam:
      fswebcam -r 1280x720 -S 15 --jpeg 99 -p MJPEG --shadow --title "TEST" --subtitle "TEST" --info "Author: TEST" --save testphoto.jpg

      If there are any errors it should tell you. If not, then you should have a lovely photo on your system.


  4. arungopal says:

    I understood u r instructions..iam now using logitech HD 720P web camera..I have one dought when push button is pressed is any need to type this command? “fswebcam -r 1280×720 -S 15 –jpeg 99 -p MJPEG –shadow –title “TEST” –subtitle “TEST” –info “Author: TEST” –save testphoto.jpg”
    This is just knowing of camera working or not ryt?ya iam tested the u r given command in my Desktop….Thanks….Kernel module driver is one procedure…just iam asking…


  5. christopher wood says:

    what language is this code? and yes i’m a NOOB. any suggestions on how i could transfer the image to a remote server via the script?

    • drewibbo says:

      Hi Chris and welcome to the site.

      The code is written in BASH which is a shell script. The files are saved as .sh files.

      To send a file to a remote server use FTP. Install it by typing “sudo apt-get install ftp” into the command line.

      example use would be:
      # Be sure to leave the following as it is. This will ensure that the filename of the log-file is correct.
      logname=$(date +”%d-%m-%Y”)
      ftp -n $HOST <

  6. Achim says:

    Hi there,

    it seems that my USB camera is not found in the drivers. I am getting this error when typing the ‘test’ command:
    — Opening /dev/video0…
    Trying source module v4l2…
    /dev/video0 opened.
    No input was specified, using the first.
    Unable to find a compatible palette format.

    Any idea how I can solve this?

    Thanks and great work so far!


  7. Jake says:

    Hey, I’ve been trying this for an entire hour now, and its a no go.
    I have everything set up exactly (From what I can tell) how you’ve put it and… nothing happens.

    I was hoping to be able to have this set up in a few minutes so i can get a timelapse of a storm that keeps blowing over and coming back but i guess not now *Shrugs* storm has gone.

    But anyways, could you possibly make a Video tutorial? it may help clear any confusion (Especially if people learn easier by watching than just reading)

  8. Hello,
    good tutorial, very well for the last Noobs must be installed v4l2.
    Hofer J.-Claude

  9. rami ferjani says:

    when we launch ./photo.sh error message that appears with the program message is the message Unable to find a compatible palette format…thnks for idea 🙂

  10. Will says:

    This worked perfectly for me. Thank you.

    One question though. Do you know of a good way to display the image after you take it.
    I’m trying to take the photo, display it for a few seconds, and then be ready to take another.

Leave a Reply

Your email address will not be published. Required fields are marked *