Working with the Raspberry Pi camera module

Lead Image © kuco,


The amazing Raspberry Pi camera module opens into a whole new world of useful projects. We'll show you how to use the Pi camera for time-lapse photography, and we'll even help you set up a motion-detecting surveillance camera.


This article was written in partnership with the MagPi magazine:

The Raspberry Pi has seen a number of fantastic improvements since its original release in April 2012, including a new PCB layout, new mounting holes, and a RAM upgrade to 512MB. Following these improvements was the introduction of the low-price model A Raspberry Pi, which was intended as a cheaper model for education and applications that require lower power. Then, in May 2013 (just over a year after the initial release of the model B Raspberry Pi) and after some slight delays, the Raspberry Pi Foundation officially released its first add-on board: the Raspberry Pi camera module (Figure 1).

Figure 1: Raspberry Pi's powerful camera module.

Before the camera module, it was of course possible to access a camera feed on the Raspberry Pi using a suitable webcam. Camera functionality had already worked its way into a variety of projects, from live weather monitoring to robotics. However, the clever folks at the Raspberry Pi Foundation were aware of the fact that, if the highly enthusiastic Raspberry Pi community would repurpose their existing webcams, an add-on camera module stood a strong chance of success.

The camera module, designed specifically for the Raspberry Pi boards, brought with it a fair number of features that make it superior to many sophisticated webcams on the market (see the box titled "Camera Specs").

It is easy to see why this camera is now the preferred choice for almost every application that requires a dedicated camera for still photography or video capture with the Raspberry Pi.

This article will guide you through the process of setting up and using the Raspberry Pi camera module.

Camera Specs

  • Omnivision 5647 CMOS Sensor in a fixed-focus package.
  • Capable of 5MP (2592x1944 pixels) images and video recording in 3 modes – 1080p at 30fps, 720p at 60fps, and 640x480 at either 60 or 90 fps.
  • Connection using a 15-pin ribbon cable via the CSI connector (just behind the Ethernet port on the model B). CSI stands for Camera Serial Interface, which is for use especially with cameras.
  • Very small form factor and weight – at just 25mm x 20mm x 9mm and 3g.
  • Native support in Raspbian, the Raspberry Pi Foundation's preferred OS.
  • Lots of adjustments available – exposure, white balance, image effects, metering modes, and many more.
  • All for a price of just over £16!

Setting Up the Camera

To attach the camera module to the Raspberry Pi, first make sure your Raspberry Pi is completely disconnected from any power source. Before removing the camera module from its grey anti-static bag, make sure you touch a radiator, a tap, or any other grounded metal object to get rid of any static that you might have built up. (The camera module is fairly sensitive and static electricity can actually damage it.)

The camera board CSI connector is located between the Ethernet and HDMI ports on the Model B Raspberry Pi. The Ethernet port does not exist on a Model A board, but the camera connector is in the same place.

Open the connector by pulling the tabs on either side of the connector upwards and towards the Ethernet port. You should then be able to insert the ribbon cable on the camera module (with silver connectors facing towards the HDMI port) into the connector. Hold the cable firmly in place, and close the CSI connector by pushing the top part towards the HDMI port and down. It should click into place nicely and hold the camera ribbon cable securely. To avoid damage, take care not to bend the ribbon cable at too much of an angle. Figure 2 shows a picture of the camera module connected to the Raspberry Pi.

Figure 2: The camera module connected to a Model A Raspberry Pi.

Next, you need to power up the Raspberry Pi and log in to Raspbian. The camera module options were not available in early versions of Raspbian, so to start with, you need to make sure that Raspbian is completely up to date. In a terminal window, type:

$ sudo apt-get update
$ sudo apt-get upgrade

These commands might take a few minutes. Updating your system will ensure you have a version of the raspi-config module with support for the Raspberry Pi camera. Now, you can start raspi-config with the command:

$ sudo raspi-config

You will see a screen that looks like Figure 3.

Figure 3: Enabling the camera module.

Use the keyboard to move down to the camera option, then hit enter. The following screens ask you to confirm and reboot. Once you reboot, the camera module will be enabled and ready to start snapping pictures!

Basic Operation

Now that you have the camera module enabled, I bet you are wondering how you can start taking pictures and videos with it? Again, this is quite a simple process that can be done either from the terminal or within a program.

The basic commands are raspivid and raspistill, which (as the names suggest) are for capturing video and still images, respectively. Each of these commands can accept a number of parameters after them. Several parameters are available, but the most basic are:

  • -o or -output, for setting the output filename
  • -t or -timeout, for setting the time in milliseconds for which a preview will display (the default is 5 seconds, and when using raspistill, it will capture the last frame of the preview and save it to the specified filename. When using raspivid, the -t parameter defines the capture time.)

Therefore, to display a 5 second preview and then save a jpeg picture called test.jpeg, the command would be:

$ raspistill -o test.jpeg

To take a 30-second video and save it in h264 format, use the command:

$ raspivid -o test.h264 -t 30000

for a list of other parameters, type either of the following commands:

$ raspistill | less
$ raspivid | less

Have a play with all of these settings and see how they change the picture (Figure 4). You'll find that the little camera is quite powerful. If you want to take a video that cycles through all the available image modes, you can append -d or -demo to the end of the raspivid command. See the Raspberry Pi Foundation's camera documentation for an in-depth look at all of the functionality available from the camera module [2].

Figure 4: Examples of the different image effects available, from top left to right: cartoon, sketch, solarize, negative, emboss, and colorswap.

Buy this article as PDF

Express-Checkout as PDF

Pages: 2

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Using the RPi Cam Web Interface

    You can access and control the Raspberry Pi camera module from your favorite browser using the RPi Cam Web Interface software.

  • Snapshot

    The remarkable Raspberry Pi has spawned a myriad of supporting projects – Android apps, program libraries, specialized Linux distributions, and an assortment of hardware accessories. The rapid changes within these projects is testament to the excitement and enthusiasm that developers around the world have given to the Raspberry Pi.

  • Building a Motion Detector using Scratch GPIO server

    As of the Raspbian Jessie release, Scratch provides easy access to the Raspberry Pi's GPIO pins. This project incorporates the new GPIO server to build a motion detector.

  • Build your own infrared camera

    The human eye is sensitive to a narrow range of wavelengths that make up the visible light spectrum, but not to the infrared range. However, a Rasp Pi outfitted with an IR camera module offers a low-cost solution for taking photographs using infrared light.

  • SunRover Part 4 – Adding a Pi Camera and Diagnostics System

    A Raspberry Pi Camera module and a diagnostics system allows SunRover to see and check that all systems are go.