Introduction to the Raspberry Pi Camera

Isn’t it excellent—the Raspberry Pi? It’s a fully-functioning computer the size of a credit card. It teaches you how to live in a Linux environment. It has a built-in Python interpreter installed. And to spice things up, it even has its camera module!

In this tutorial, you will learn how to use the Raspberry Pi camera module. You will learn how to take pictures and videos using the terminal and Python. By the end of this article, you should be comfortable with the camera module’s functionalities that you can generate your own ideas yourself.

Raspberry Pi Camera

The Raspberry Pi Camera is an official Pi accessory that allows users to take pictures and videos to use in different applications. Its primary sensor depends on the board version. The 2nd version that I’m using for this tutorial uses an 8-megapixel Sony IMX219 sensor, while the original module sports a 5-megapixel OmniVision OV5647 sensor.

Since these modules are released years apart, the obvious difference is quality. The camera module v2 has more resolution, better image quality, and better low-light performance. It even supports 1080p30, 720p60, and VGA90 video modes, as well as still capture. So unless you have an older version of the module lying around, kindly get v2.

Furthermore, both versions support all models of Raspberry Pi 1, 2, 3, and 4, so your code works with all the boards. You can access the device using the terminal or Python. With Python, you can employ dozens of libraries, including the built-in Picamera library that provides raw screen capture and video recording functions. Some libraries even open the door for more advanced applications like Convolution Neural Networks on Computer Vision and other AI-based image processing.

Now, before we go nuts on this awesome piece of tech, we need to set it up first.

Installing the Camera Module

1. When installing, make sure the board has no power. Pull up on the edges of the camera port’s plastic clip.

2. Insert the ribbon cable to the port. The silver lines on the cable should face the silver lines on the port.

3. Gently push the clip back. The cable should be on tight fit with the port that it won’t slide out when jerked.

Now that the camera is onboard. Time to setup the Raspberry Pi so that it can communicate with the camera module.

Setting up the camera interface

1. First, startup your Raspberry Pi.

2. Open the Raspberry Pi Configuration tool using either main menu or command line. In the main menu, you can find it under Preferences.

Figure 1: Raspiconfig

Otherwise, enter this command on the terminal.

sudo raspi-config

3. Next, select Interfaces. The camera is disabled by default. Enable it then save the changes to raspiconfig.

4. Lastly. restart your Raspberry Pi.

If you’ve done everything above, there’s a 99% chance the camera will now work. If it still doesn’t, then there might be a problem with the item itself. I had issues with this before, and it seems like there’s a batch of Raspberry Pi camera produced that are particular to this problem. The best thing to do is to ask for a store replacement.

Using the terminal to take pictures and videos

The easiest way to use your camera is by the terminal. Just pull the terminal up from the menu bar and enter the following commands.

  • Taking a photo
raspistill -o home/pi/image.jpg

The raspistill command captures the photo after a 5-second preview (enough time to prepare your hair and flash your pearly whites). You must include the path and the target filename of your photo with the command, or else it does not work.

  • Recording a video
raspivid -o home/pi/video.h264

On the other hand, the raspvid command records a 5-second clip and saves it to your target directory. You also need to include the path or else it does not work. Furthermore, you can change the resolution of your image and video by adding -w and -h switches. For instance:

raspistill -o home/pi/image.jpg -w 1024 -h 768
raspivid -o home/pi/video.h264 -w 1024 -h 768

Using Python to take pictures and videos

Another way to control the camera is by using Python. Go on and open your favorite Python editor. Make sure to import the necessary library to make the camera work. In this case, we will use the picamera module. Next, initialize the camera using the PiCamera() function. Your program should look like this:

from picamera import PiCamera
camera = PiCamera()

For the camera to start sending data to your Raspberry Pi, add camera.start_preview(). If your preview is reversed, rotate by 180 degrees using camera.rotation = 180. The value of camera.rotation is changeable. So if you need to orient your preview to an unusual degree, you technically can, but I advise not to because it might mess up your image.

  • Taking a photo

Note that if you’re coding Python using the terminal’s interactive shell, the screen will get replaced by your camera’s output. To exit, press CTRL + Z. Finally, to take a picture, simply use camera.capture('path/path/desiredfilename.jpg').

camera.start_preview()
time.sleep(5)
camera.capture('/home/pi/Desktop/image.jpg')
camera.stop_preview()

You can import the time module to replicate what we did using the terminal. Add a 5-second delay so that you have time to prepare. Always include camera.stop_preview() after you are done with the camera.

  • Taking a video

The way you take a picture and record a video using PiCamera is very similar. You need not remove the start and stop preview functions. You just replace camera.capture by camera.start_recording('path/path/desiredfilename.h264') and camera.stop_recording().

camera.start_preview()
camera.start_recording('path/path/desiredfilename.h264')
time.sleep(5)
camera.stop_recording()
camera.stop_preview()

Use the delay to indicate the duration of your video recording.

Changing the camera settings

PiCamera also provides methods that change how your output images look. These include modifying basic image properties like the resolution to adding camera effects like a sepia filter. We will not discuss them here, but you can visit the official documentation for further details.

Creating a Timelapse

Now for some extra tips, here’s a sample code for your camera module that takes a photo every 30 seconds in 1024 x 768 resolution. Note that the resolution is often modified in time-lapse programs, especially if the time in between pictures is short. If the wait time is short, more photos are taken, and if these pictures are big, they will fill up your storage faster than you could expect.

from picamera import PiCamera
from time import sleep picamera = PiCamera()
timeinbetween = 30 with picamera.PiCamera() as camera: camera.resolution = (1024, 768) for filename in camera.capture_continuous('/home/pi/time-lapse/img{timestamp:%H-%M-%S-%f}.jpg'): sleep(timeinbetween)