Raspberry Pi Camera Setup

I know I’m not the first person to get the Camera working on my Raspberry Pi, and if you are using the default SD card image everything should work just fine out of the box. However if (like me) you have installed Raspbian from scratch then you may find that a bit of googling will be needed to get everything working. There isn’t anything new in this post, I’ve just tried to collect everything together in one place.

Until now I have used a webcam to capture images on my Raspberry Pi, with mixed results. I tried a couple of different webcams but found that the resolution wasn’t that good and it could take a while to capture a frame. The answer seemed to be to use the Raspberry Pi camera module, the resolution is much better than a webcam, and since the camera module is connected directly to the Raspberry Pi and the processing is done by the GPU, performance is much much better. It is possible to capture video at up to 90 fps, which is awesome!

The only thing I found a little disappointing was capturing images in low light conditions. I was hoping to be able to capture successive images showing of the stars tracking across the night sky but wasn’t able to see any stars at all. I guess I will have to play about with the sensitivity and exposure settings a bit to see if I can do better.

Getting started

Connecting up the camera module is quite simple, all you need to do is locate the long thin connector nearest the ethernet port, lift up the tabs at either end and insert the ribbon cable from the camera with the blue strip on one side facing towards the ethernet connector, then push down the tabs to lock the cable in place. The camera module is static sensitive so wear a grounding strap if you have one, or at least make sure you touch the bare metal on a radiator to earth yourself before you start, and obviously make sure the Pi is powered off!

Enabling the Camera

Then you need to ensure that the system loads the necessary firmware to support the camera module when it boots. To do this you need to be running as root.

$ su


$ sudo -i

If you are using one of the standard SD images then you can enable the camera from the configuration menu.

# raspi-config

However, if this isn’t available then you can edit the config file to load the extended firmware with support for the camera module.

# vi /boot/config.txt

Make sure the file includes the following three lines. The first sets the amount of RAM allocated to the GPU (which does all the work capturing images and processing them), and second two load the correct version of the firmware.


If you want to you can disable the camera LED by including the following line as well.


Configure Permisions

By default ordinary users do not have the right permissions to be use the camera module, this isn’t a problem if you are logged in as root but you probably want the permissions to be set automatically when your Raspberry Pi boots. To do this you will need to create a new udev rule, and rather than simply give access to a single user, I’m going to create a new rule that gives anyone who is a member of the ‘video’ group access to the camera, and then make the user ‘pi’ a member of that group.

Note – You need to be logged in a root to make these changes.

# echo 'SUBSYSTEM=="vchiq",GROUP="video",MODE="0660"' \
>  > /etc/udev/rules.d/10-vchiq-permissions.rules
# usermod -a -G video pi

Without the correct permissions users will get error like “failed to open vchiq instance” when you try to use the camera.

For these changes to take effect you need to reboot.

# reboot

Setting up paths

The camera applications and associated libraries are not part of a standard linux distribution, so we need to modify the search paths so that the system knows where to find the camera applications, and the libraries that they depend on. We can do this by adding a couple of lines the ‘.bashrc’ script that is run automatically when you log in.

$ vi $HOME/.bashrc

Then add the following two lines to the end of the file.

export PATH=$PATH:/opt/vc/bin

You will need to logout and log back in again for this change to take effect.

$ exit

Taking Pictures

To capture a single image you can use the ‘raspistill’ utility.

$ raspistill --nopreview --output image.jpg

There are additional parameters that you can specify to change the image resolution, and jpeg image quality.

$ raspistill --quality 75 --nopreview --verbose \
> --width 1280 --height 720 \
> --output image.jpg

You can also specify the how long to wait before capturing and image, the exposure mode, and shutter speed, which can be very useful when trying to capture images in low light conditions.

$ raspistill --nopreview --timeout=10000 \
> exposure sports --shutter 1000000 \
> --width 1280 --height 720 \
> --output image.jpg

You can also use the timelapse feature capture a succession of images at regular intervals that can be used to produce a timelapse video later.

$ raspistill --quality 75 --nopreview --verbose \
> --width 1280 --height 720 \
> --timeout 1800000 --timelapse 4000 \
> --output image%06d.jpg

In this case I’m capturing one frame at a resolution of 1280 x 720 pixels every 4 seconds for half an hour. Once you have captured you raw images you can turn them into a time-lapse video.

Producing a time-lapse video

To turn a succession of images into a time-lapse video you will need to use ‘mencoder’. If it is not already installed you will need to be running as root to install the required packages.

$ su


$ sudo -i

Then just grab the required packages from the repository.

# apt-get install mencoder
Reading package lists... Done
Building dependency tree 
Reading state information... Done
The following extra packages will be installed:
esound-common libaudiofile1 libesd0 libgif4 
liblircclient0 liblzo2-2 libsdl1.2debian 
libsvga1 libvdpau1 libx86-1 mplayer
Suggested packages:
pulseaudio-esound-compat lirc 
nvidia-vdpau-driver vdpau-driver mplayer-doc
ttf-freefont netselect fping
The following NEW packages will be installed:
esound-common libaudiofile1 libesd0 libgif4 
liblircclient0 liblzo2-2 libsdl1.2debian
libsvga1 libvdpau1 libx86-1 mencoder mplayer
0 upgraded, 12 newly installed, 0 to remove and 0 not upgraded.
Need to get 5,251 kB of archives.
After this operation, 11.5 MB of additional disk space will be used.
Do you want to continue [Y/n]? Y
# exit

Once mencoder is installed you can use it to turn the images you have captured into a video. You can adjust the value for the Constant Ratefactor to tune the size of the resulting video – lower values will produce a larger, higher quality output. The default is 23 but I found that using a value of 16 produced a better quality result that was not so large as the be unmanageable. Do not forget that may need to change the values for the height and width if your images are a different size.

$ mencoder -nosound \
> mf://*.jpg -mf w=1280:h=720:type=jpg:fps=20 \
> -ovc x264 -x264encopts pass=1:crf=16 \
> -lavfopts format=mp4 \
> -o video.mp4

Raspberry Pi is a trademark of the Raspberry Pi Foundation

This entry was posted in Raspbian and tagged , , . Bookmark the permalink.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s