How To Set Up And Use A Raspberry Pi Camera


One of the first Raspberry Pi projects I made was a remote-controlled car. After driving it around for a little bit I realized it would be a lot more fun if it had a camera mounted to it. In less than 10 minutes I had the camera set up and ready to go.

How do you set up a camera on a Raspberry Pi? The camera can is attached through a designated CSI interface. After enabling the camera though the config settings, you can access the camera through the Linux command line or a Python script.

Installing The Camera

Connecting a camera module to your Raspberry Pi is a very simple process. First, you will want to make sure you are using the correct module. The module for a standard-sized Raspberry Pi is different than the module for a Raspberry Pi Zero or Zero W. You can find the standard sized module here and the Zero (W) module here.

Attach the ribbon to your Raspberry Pi using the CSI interface. The CSI interface can be found between the Ethernet and HDMI port on larger Raspberry Pi’s. For these make sure that the exposed silver leads on the ribbon are facing the HDMI port. For Raspberry Pi Zero and Zero W, the CSI interface is along the short edge of the board, next to the TV pins. Be sure that the exposed connectors on the ribbon are face down.

After you have connected the camera module, boot up your Raspberry Pi and open the config window with:

sudo raspi-config

From here you will want to select the camera option and enable it. If you are not seeing the camera option, you may need to update your Raspberry Pi with:

sudo apt-get update
sudo apt-get upgrade

Once you have enabled the camera option in the config window, reboot your Raspberry Pi. Congratulations, you have officially connected a camera to your Raspberry Pi. Now that it is connected, let’s learn how to take pictures and video with it.

Take A Picture Or Video With Linux Command Line

Now that your camera is set up, it’s time to start using it. There are 4 different Linux commands you can use to when controlling your Raspberry Pi camera.

Raspistill

This is the most basic camera command and will simply take a picture. The syntax for the raspistill command is:

raspistill -o cam.jpg

You can choose where to store the photo by setting the address before the filename. For example:

raspistill -o /home/myfolder/coolphoto.jpg

The raspistill command also enables you to flip the image vertically or horizontally by adding a -v or -h tag to the command

raspistill -v -h -o /home/myfolder/coolphoto.jpg

Raspiyuv

The raspiyuv command allows photos to be taken and saved in a raw unprocessed format. If you are familiar with photography, you know how much more powerful a raw photo can be when it comes to editing. If your goal is a simple photo, stick with raspistill but if you want to edit your photos after taking them, then raspiyuv is the command you will want to go with.

Raspivid

If you are looking to create a video using the command line, the raspivid command has your back. Before you use the raspivid command, you will probably want to install MP4Box. The raspivid command produces a raw H264 video file which most video players are unable to handle. Instead, you will want to wrap it in an MP4 container. To download MP4Box use the following command:

sudo apt-get install -y gpac

Now it’s time to take that video. You can shoot your video with the following command:

raspivid -o myvid.h264

This will shoot a 5-second long video and name the file myvid.h264. There are a number of tags you can add to the command to change the resolution, length, framerate, and more.

  • -hf flip the video horizontally
  • -vf flip the video vertically
  • -t set the length of the recording in milliseconds (default is 5000)
  • -w set the resolution width
  • -h set the resolution height
  • -fps set the frame rate
  • -b set the bitrate
  • -p show a preview of the video

Once you have created your video file, you will need to wrap it an MP4 container. To do this use the following command:

MP4Box -add myvid.h264 myvid.mp4

You can then delete the h264 video with:

rm myvid.h264

Congratulations, you have now created an MP4 using a Raspberry Pi camera.

Timelapse

While timelapse technically is a tag of the raspistill command, I felt it deserved its own section. Below is an example of a timelapse command.

raspistill -t 60000 -tl 1000 -o image%04d.jpg

The -t tag sets the total length of the timelapse to 1 minute (60,000 milliseconds) and the -tl tag sets the picture interval to 1000 milliseconds. The %04d in the filename is used to generate a new name for each picture by adding a number to each image. The 4 indicates how many digits the number should be, in this case, a 4 digit number with leading zeros would be added to each image. The final file name would be image0001.jpg, image0002.jpg, image0003.jpg, … and so on. If you remove the zero from the command, the leading zeros will be omitted.

Once you have collected all of your pictures you will need to stitch them together. This can take a significant amount of time depending on the number of images and the model of your Raspberry Pi. To stitch the images together you will need to download the package that contains avconv.

sudo apt-get install libav-tools

Once the library is installed, you are ready to convert your JPEG files into an H264 video file. You can do this with the following command.

avconv -r 24 -i image%04d.jpg -vcodec libx264 -vf 1920:1080 mytimelapse.mp4

Let’s break down what each of these tags does.

  • -r set the frame rate
  • -i the image file naming convention used for the input
  • -vcodec tell the command to use the H264 encoder
  • -vf set the resolution

Again, this command could take a significant amount of time depending on your video length and the model of Raspberry Pi that you are using so please be patient as this runs.

Controlling The Camera With Python

Python is the primary language used to control aspects of a Raspberry Pi such as the GPIO. You can also use it to control a camera once it is installed. Here are the 4 basic Python commands to get you started.

Note: Do not save a file as picamera.py as this filename is part of the library that the Raspberry Pi uses.

Preview

The preview command allows you to send a live stream of what the camera sees to a monitor. If your Raspberry Pi is connected via SSH or VNC, it will not display the capture. Here is an example of what the preview command would look like.

from picamera import PiCamera
from time import sleep
camera = PiCamera()
camera.start_preview()
sleep(30)
camera.stop_preview()

When running this code, the camera will display what it sees live for 30 seconds. If your image is upsidedown you can rotate the image by adding:

camera.rotation = 180

right above the preview command.

Capture

The capture command is the Python equivalent to raspistill in Linux. It allows you to take individual pictures with your Raspberry Pi camera.

from picamera import PiCamera
from time import sleep
camera = PiCamera()
camera.start_preview()
sleep(3)

camera.capture('home/pi/Desktop/capture.jpg')
camera.stop_preview()

This will display what the camera sees on your screen for 3 seconds before snapping a picture and saving it to your desktop. There are two things to note in this code. First is that the preview code is not necessary, it is only included to see what you are taking a picture of. Second, a sleep command of at least 2 seconds is highly recommended so that the camera can adjust to the appropriate brightness settings.

Recording

The recording command is what allows you to take a video with your Raspberry Pi camera. The syntax is very similar to that of the preview command.

from picamera import PiCamera
from time import sleep
camera = PiCamera()
camera.start_preview()
camera.start_recording('home/pi/Desktop/video.h264)
sleep(30)
camera.stop_recording()
camera.stop_preview()

This short program will display what the camera sees for 30 seconds as well as capture it all into an H264 video file that is saved on your desktop.

Camera Effects

There are a number of ways that you can manipulate the camera object to alter the files that are produced. Below is a starter list of commands that can be placed before the capture is taken with a sample syntax.

  • camera.resolution = (1920, 1080)
  • camera.framerate = 15
  • camera.brightness = 50
  • camera.contrast = 75
  • camera.image_effect (‘negative’)

This isn’t a comprehensive list of commands, more can be found here.

Related Questions

What is the best camera option for Raspberry Pi?

Sticking with any of the cameras that connected directly to the CSI interface is going to be your best option. All of the drivers are already set up, the resolution is good (5 megapixels or better) at it does not take up any of the other ports on your Raspberry Pi.

What is a NoIR Camera?

The NoIR Camera has the IR filter removed from it. This allows it to take night vision images when there is an infrared light present.

Recent Content