Build a camera with Raspberry Pi and Python

Snap some photos of your cat or something

So today I'm going to be briefly showing you how to use the Raspberry Pi camera to take some snaps using Python. I'm hoping to extend this to building my own version of the GIF camera where it takes a series of photos and stitches them together to make a GIF. I'll hopefully get this code up on Github soon as well.

What do I need?

  • A Raspberry Pi Camera (I'm using version 2)
  • A push button switch
  • A couple of LEDs (not actually necessary)
  • Some jumper wires
  • A breadboard

And if you're using a Raspberry Pi Zero like I am then you're going to want a Pi Zero camera adapter ribbon cable (basically goes from the wider ribbon cable at one end to the smaller end for the Pi Zero at the other).

The circuit

So the circuit is pretty simple again. All we're doing is wiring up two LED's to GPIO pins on the Pi and a Switch to another GPIO on the Pi.

  • Connect a jumper wire from a ground pin on the Pi to the ground rail on your breadboard.
  • Wire up two LEDS (I've used a green and a red) by plugging the long 'leg' into a GPIO pin on the Pi and then a 330 Ohm resistor (again, I've used 560 Ohm) between the shorter 'leg' and the ground rail on the breadboard
  • If you have those small push type switches then straddle one over the middle of your breadboard and plug one end to a GPIO pin on the Pi and for the other leg on the same side of the breadboard plug it into the ground rail of the breadboard

The Camera

So the camera's ribbon cable just slides into the camera connector on your Pi. First, use your finger to slide back the black clip on the outside of the connector on the Pi (and the camera if you need to connect it to your camera as well) and then slide in the ribbon cable. Then push the little black clip back on to the connector to hold the ribbon cable in place. Don't force it, it only needs to go in a short way.


So there's a couple of things you'll want to do to test your camera is working and connected okay. First up we need to activate the camera in Raspian.

  • Click on the Raspberry Pi logo in the top left of your screen.
  • Navigate to 'Preferences > Raspberry Pi Configuration'.
  • Then click on the 'Interfaces' tab.
  • Then click the radio button to enable 'Camera'.
  • You'll then need to restart your Raspberry Pi (it might inform you to do this anyway, I can't remember)

Once you've restarted, open up your terminal and smack in some of these commands to take your first photos and videos!

raspistill -o photo.jpg

So this will simply save a photo with the filename 'photo.jpg' to your current directory.

raspivid -o video.h264 -t 10000

This will save a video with the filename 'video.h264' to your current directory. Note that it uses the h.264 codec. The -t 10000 option simply tells it to record for 10 seconds. But wait, I want to play back my video!

sudo apt-get install -y gpac

To convert the video we need the above package installed.

MP4Box -add video.h264 video.mp4

So to play back your video we first need to convert it, to do this we use the MP4Box command and specify our input and output filenames. This will convert from a raw video in the h.264 codec to a MP4 encoded video.

omxplayer video.mp4

This command will play your video back to you so you can see what your mug looks like on your monitor/TV.

The code

# Import our libraries
from gpiozero import LED, Button
from picamera import PiCamera
from time import sleep
from datetime import datetime

# Setup our inputs
camera = PiCamera()
btn = Button(27)
ledCapture = LED(17)
ledStatus = LED(26)

# Start our camera up

# Loop indefinitely and each time the button is pressed snap a photo
while True:
    dt ='%Y-%m-%d-%H:%M:%S')
    print('Taking photo /home/pi/Desktop/IMG-%s.jpg' % dt)
    camera.capture('/home/pi/Desktop/IMG-%s.jpg' % dt)
  • So the first few lines we are just importing our libraries. We're using GPIO Zero, PiCamera and then sleep and datetime from their respective python libraries.
  • The next few lines we are initialising our inputs using GPIO Zero and PiCamera. So we create our camera object, as well as our button and two LED's (one for status and one for capture).
  • Then we start the camera up when the script runs and wait for two seconds (the minimum time PiCamera say it takes for the camera to calibrate its white balance settings etc. I then activate the status LED to show that we're ready to snap some photos.
  • Then we do our standard while loop which will run until the script is terminated. All we do is use the wait_for_press() function built in to the Button class of GPIO Zero. This will pause the script until a button is pressed and then execute the rest of the script.
  • Then we are just grabbing the current datetime and formatting the string output. We then print a line to the terminal and switch the capture LED on. Then we do the actual capture command and specify the filename including the path. Then we simply turn the capture LED back off again.

And in case you're wondering how to properly close the script, it isn't 'Control + C', it is in fact 'Control + Shift + \' on a Linux computer.

And the result? A half decent photo!

Programming, Electronics