adventures with microcomputers prototyping with raspberry pi

11. PiCamera




  1. run the setup script: sudo raspi-config
  2. Enable the camera in Interfacing Options
  3. Locate the socket that the pi camera plugs into on your raspberry pi. Note that there are two similar looking ribbon sockets, one of which is labeled CAMERA. Plug the camera in (the blue tape material poking out of the socket should face towards the headphone jack on the pi)
  4. Install the Python3 library for PiCamera: sudo apt-get install python3-picamera

simple photo

Here one can find the code to take a single photo from Python3 with the PiCamera. A few quick notes:

  • We import the datetime module and use'%Y-%m-%d-%H-%m-%s') to give each photo a unique filename (down to the second). This both minimizes the likelihood of accidentally saving over photos and provides a way to more easily distinguish between photos without looking at them
  • According to the PiCamera docs one is supposed to use camera.start_preview(), followed by a sleep statement, to allow the camera to calibrate/focus (the docs say the camera needs to sleep for at least 2 seconds after .start_preview())
  • camera.capture() stores and saves the photo, so this is also where we specify the full filepath (including the datetime information)
  • camera.stop_preview() and camera.close() are used to cleanup what was setup to take a photo

viewing photos

camera.start_preview() will show the camera feed on any display connected to the pi. When running headless, though, we have two options for viewing photos:

  • scp the file from our Pi to our Macbook
  • setup a way to upload photos from our Pi to a shared cloud service (like Dropbox).

viewing photos (scp)

  1. on your raspberry pi cd to wherever the script saved your photo
  2. back on your Macbook, run ifconfig in the terminal to get your Macbook's IP address
  3. back on your Pi, use scp to send the file to your home directory on your Macbook. The command should look something like this: scp filename.jpg macbookUsername@macbookIPAddress:Path/To/Home. Once you have formatted this correctly hit [ENTER]
  4. you should be prompted to enter the login password for macbookUsername before the file is copied over. After typing it in hit
  5. back on your Macbook, navigate to the home directory and double click on the image to view it in Preview (or whatever)

viewing photos (dropbox sync)

This method relies on Andrea Fabrizi's dropbox uploader.

  1. on your Pi run the following command to clone the dropbox uploader repo: git clone
  2. next on your Macbook navigate to dropbox and, if you do not already have one, set up a free account.
  3. once you have a dropbox account setup, go here to setup an application to interact with dropbox
  4. this next part will probably change from time-to-time, but as of 4/18/2017 the process was as follows:
    • click the Create app button
    • select “Dropbox API” and “Full Dropbox,” give your app a unique name (example: yournamecameraapp) then confirm by clicking Create the app
    • finally, in the settings tab there is a button that reads Generate access token. Click this button and then copy the string of seemingly random letters and numbers (referred to as a token) to your clipboard . This string is how dropbox will identify your raspberry pi
  5. back on the raspberry pi cd into the Dropbox-Uploader folder and make executable by running the following: sudo chmod +x
  6. next run the bash script: ./
  7. you will be prompted to enter your access token, which should still be in your clipboard, so paste () it into the terminal and type y to confirm
  8. try uploading the file in the Dropbox-Uploader folder to dropbox to confirm functionality: ./dropbox_uploader upload /
  9. If successful, you should see something like the following in the terminal: > Uploading "/home/cta/Dropbox-Uploader/" to "/"... DONE
  10. back on your Macbook navigate to dropbox to confirm that is there

To upload photos simply edit this command to suit your needs: ./ upload fileorfoldertoupload /. Notes to keep in mind:

  • you have to be in the Dropbox-Uploader directory for this command to work
  • that the trailing / is an important part of this command
  • this command works for single files as well as a directory full of files

simple video

Here one can find the code to take a video from Python3 with the PiCamera. A few quick notes:

  • we allow the camera to calibrate for 2 seconds after calling camera.start_preview(), which gives the camera time to focus
  • camera.capture() is replaced here with camera.start_recording(), which will record video until we issue the stop command (camera.stop_recording()). Note that the datetime line from simple_photo is reused verbatim here to avoid overwriting files/provide a timeline via filename
  • the sleep command controls how long the video is, so recording video longer than five seconds is simply a matter of increasing the sleep time to the desired duration

converting video

Note: mad props to this article for the conversion process.

simple_video saves video as Raw H264 Video Data, so there are some extra steps to take prior to being able to view it without plugging a display into our Pi:

  1. Download and install gpac: sudo apt-get install -y gpac
  2. cd to the directory where your video lives and, using MP4Box (installed with gpac), convert your video to .mp4 by editing (to suit your needs) and then execute this command: MP4Box -fps 30 -add myvid.h264 myvid.mp4
  3. once the video has been converted, delete the original: rm mywid.h264
  4. finally cd to the Dropbox-Uploader folder, edit (to suit your needs) and then execute the following command: ./ upload ../myvid.mp4 /
  5. back on your Macbook navigate to your dropbox folder and double click the .mp4 to watch